Forum Stats

  • 3,837,410 Users
  • 2,262,256 Discussions
  • 7,900,273 Comments

Discussions

Dbc::get() has no need to commit ?

529558
529558 Member Posts: 8
edited Aug 30, 2006 9:23AM in Berkeley DB
m_pEvironment->txn_begin (NULL,&m_pTransaction,0);
nRes=m_pDb->cursor (m_pTransaction,&m_pCursor,0);
try
{
Dbt dbKey;
Dbt dbData;
int nRes;
dbKey.set_flags (DB_DBT_MALLOC);
dbData.set_flags (DB_DBT_MALLOC);

nRes=m_pCursor->get (&dbKey,&dbData,DB_NEXT);
m_pTransaction->commit (0); // <---------exception catched ,jump to catch() block.
// Dbc::get() is one of a access operation ,in theory it need a commitment.
}
catch (DbException &dbe)
{

}

sorry to disturb you as I really have no information to refer to.

Comments

  • 512799
    512799 Member Posts: 494
    First, you need to close the cursor before commiting or aborting the transaction. Here is a sample used in the Berkeley DB Transaction Guide:

    #include <stdio.h>
    #include <stdlib.h>

    #include "db.h"

    int
    main(void)
    {
    DBT key, data;
    DBC *cursorp;
    DB_TXN *txn = NULL;
    int ret, c_ret;
    char *replacementString = "new string";

    ...
    /* environment and db handle creation omitted */
    ...

    /* Get the txn handle */
    txn = NULL;
    ret = envp->txn_begin(envp, NULL, &txn, 0);
    if (ret != 0) {
    envp->err(envp, ret, "Transaction begin failed.");
    goto err;
    }

    /* Get the cursor, supply the txn handle at that time */
    ret = dbp->cursor(dbp, txn, &cursorp, 0);
    if (ret != 0) {
    envp->err(envp, ret, "Cursor open failed.");
    txn->abort(txn);
    goto err;
    }

    /*
    * Now use the cursor. Note that we do not supply any txn handles to these
    * methods.
    */
    /* Prepare the DBTs */
    memset(&key, 0, sizeof(DBT));
    memset(&data, 0, sizeof(DBT));
    while (cursor->c_get(&key, &data, DB_NEXT) == 0) {
    data->data = (void *)replacementString;
    data->size = (strlen(replacementString) + 1) * sizeof(char);
    c_ret = cursor->c_put(cursor, &key, &data, DB_CURRENT);
    if (c_ret != 0) {
    /* abort the transaction and goto error */
    envp->err(envp, ret, "Cursor put failed.");
    cursorp->c_close(cursorp);
    cursorp = NULL;
    txn->abort(txn);
    goto err;
    }
    }

    /*
    * Commit the transaction. Note that the transaction handle
    * can no longer be used.
    */
    ret = cursorp->c_close(cursorp);
    if (ret != 0) {
    envp->err(envp, ret, "Cursor close failed.");
    txn->abort(txn);
    goto err;
    }
    ret = txn->commit(txn, 0);
    if (ret != 0) {
    envp->err(envp, ret, "Transaction commit failed.");
    goto err;
    }

    err:
    /* Close the cursor (if the handle is not NULL)
    * and perform whatever other cleanup is required */

    /* Close the database */

    /* Close the environment */

    ...

    if (c_ret != 0)
    ret = c_ret;

    return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
    }

    Ron
This discussion has been closed.