### Berkeley DB Reference Guide: Upgrading Berkeley DB Applications |

### Release 4.1: DB->associate, DB->open, DB->remove, DB->rename

Historic releases of Berkeley DB transaction-protected the DB->open, DB->remove and DB->rename methods, but did it in an implicit way, that is, applications did not specify the DB_TXN handles associated with the operations. This approach had a number of problems, the most significant of which was there was no way to group operations that included database creation, removal or rename. For example, applications wanting to maintain a list of the databases in an environment in a well-known database had no way to update the well-known database and create a database within a single transaction, and so there was no way to guarantee the list of databases was correct for the environment after system or application failure. Another example might be the creation of both a primary database and a database intended to serve as a secondary index, where again there was no way to group the creation of both databases in a single atomic operation.

In the 4.1 release of Berkeley DB, this is no longer the case. The DB->open and DB->associate methods now take a DB_TXN handle returned by DB_ENV->txn_begin as an optional argument. New DB_ENV->dbremove and DB_ENV->dbrename methods taking a DB_TXN handle as an optional argument have been added.

To upgrade, applications must add a DB_TXN parameter in the appropriate location for the DB->open method calls, and the DB->associate method calls (in both cases, the second argument for the C API, the first for the C++ or Java APIs).

Applications wanting to transaction-protect their DB->open and DB->associate method calls can add a NULL DB_TXN argument and specify the DB_AUTO_COMMIT flag to the two calls, which wraps the operation in an internal Berkeley DB transaction. Applications wanting to transaction-protect the remove and rename operations must rewrite their calls to the DB->remove and DB->rename methods to be, instead, calls to the new DB_ENV->dbremove and DB_ENV->dbrename methods. Applications not wanting to transaction-protect any of the operations can add a NULL argument to their DB->open and DB->associate method calls and require no further changes.

For example, an application currently opening and closing a database as follows:

>     DB *dbp;
>     DB_ENV *dbenv;
>     int ret;

>     if ((ret = db_create(&dbp, dbenv, 0)) != 0)
>     	goto err_handler;

>     if ((ret = dbp->open(dbp, "file", NULL, DB_BTREE, DB_CREATE, 0664)) != 0) {
>     	(void)dbp->close(dbp);
>     	goto err_handler;
>     }

could transaction-protect the DB->open call as follows:

>     DB *dbp;
>     DB_ENV *dbenv;
>     int ret;

>     if ((ret = db_create(&dbp, dbenv, 0)) != 0)
>     	goto err_handler;

>     if ((ret = dbp->open(dbp,
>         NULL, "file", NULL, DB_BTREE, DB_CREATE | DB_AUTO_COMMIT, 0664)) != 0) {
>     	(void)dbp->close(dbp);
>     	goto err_handler;
>     }

An application currently removing a database as follows:

>     DB *dbp;
>     DB_ENV *dbenv;
>     int ret;

>     if ((ret = db_create(&dbp, dbenv, 0)) != 0)
>     	goto err_handler;

>     if ((ret = dbp->remove(dbp, "file", NULL, 0)) != 0)
>     	goto err_handler;

could transaction-protect the database removal as follows:

>     DB *dbp;
>     DB_ENV *dbenv;
>     int ret;

>     if ((ret =
>         dbenv->dbremove(dbenv, NULL, "file", NULL, DB_AUTO_COMMIT)) != 0)
>     	goto err_handler;

An application currently renaming a database as follows:

>     DB *dbp;
>     DB_ENV *dbenv;
>     int ret;

>     if ((ret = db_create(&dbp, dbenv, 0)) != 0)
>     	goto err_handler;

>     if ((ret = dbp->rename(dbp, "file", NULL, "newname", 0)) != 0)
>     	goto err_handler;

could transaction-protect the database renaming as follows:

>     DB *dbp;
>     DB_ENV *dbenv;
>     int ret;

>     if ((ret = dbenv->dbrename(
>         dbenv, NULL, "file", NULL, "newname", DB_AUTO_COMMIT)) != 0)
>     	goto err_handler;

These examples are the simplest possible translation, and will result in behavior matching that of previous releases. For further discussion on how to transaction-protect DB->open method calls, see Opening the databases.

DB handles that will later be used for transaction-protected operations must be opened within a transaction. Specifying a transaction handle to operations using handles not opened within a transaction will return an error. Similarly, not specifying a transaction handle to operations using handles that were opened within a transaction will also return an error.

Copyright (c) 1996-2004 Sleepycat Software, Inc. - All rights reserved.