database open/close
Prev | Chapter 22. Upgrading Berkeley DB 2.X applications to Berkeley DB 3.0 | Next

## database open/close

Database opens were changed in the Berkeley DB 3.0 release in a similar way to environment opens.

To upgrade your application, first find each place your application opens a database, that is, calls the db_open function. Each of these calls should be replaced with calls to db_create() and DB->open().

Here's an example creating a Berkeley DB database using the 2.X interface:

    DB *dbp;
    DB_ENV *dbenv;
    int ret;

    if ((ret = db_open(DATABASE,
        DB_BTREE, DB_CREATE, 0664, dbenv, NULL, &dbp)) != 0)
    	return (ret);

In the Berkeley DB 3.0 release, this code would be written as:

    DB *dbp;
    DB_ENV *dbenv;
    int ret;

    if ((ret = db_create(&dbp, dbenv, 0)) != 0)
    	return (ret);

    if ((ret = dbp->open(dbp,
        DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) {
        (void)dbp->close(dbp, 0);
    	return (ret);
    }

As you can see, the arguments to db_open and to DB->open() are largely the same. There is some re-organization, and note that the enclosing DB_ENV structure is specified when the DB object is created using the db_create() function. There is one additional argument to DB->open(), argument #3. For backward compatibility with the 2.X Berkeley DB releases, simply set that argument to NULL.

There are two additional issues with the db_open call.

First, it was possible in the 2.X releases for an application to provide an environment that did not contain a shared memory buffer pool as the database environment, and Berkeley DB would create a private one automatically. This functionality is no longer available, applications must specify the DB_INIT_MPOOL flag if databases are going to be opened in the environment.

The final issue with upgrading the db_open call is that the DB_INFO structure is no longer used, having been replaced by individual methods on the DB handle. That change is discussed in detail later in this chapter.

Prev | Up | Next
DB_ENV structure | Home | db_xa_open