DbEnv class for C++ and Java
Prev | Chapter 14. Upgrading Berkeley DB 2.X applications to Berkeley DB 3.0 | Next

## DbEnv class for C++ and Java

The DbEnv::appinit() method and two constructors for the DbEnv class are gone. There is now a single way to create and initialize the environment. The way to create an environment is to use the new DbEnv constructor with one argument. After this call, the DbEnv can be configured with various set_XXX methods. Finally, a call to DbEnv::open is made to initialize the environment.

Here's a C++ example creating a Berkeley DB environment using the 2.X interface

    int dberr;
    DbEnv *dbenv = new DbEnv();

    dbenv->set_error_stream(&cerr);
    dbenv->set_errpfx("myprog");

    if ((dberr = dbenv->appinit("/database/home",
    	NULL, DB_CREATE | DB_INIT_LOCK | DB_INIT_MPOOL)) != 0) {
        cerr << "failure: " << strerror(dberr);
        exit (1);
    }

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

    int dberr;
    DbEnv *dbenv = new DbEnv(0);

    dbenv->set_error_stream(&cerr);
    dbenv->set_errpfx("myprog");

    if ((dberr = dbenv->open("/database/home",
    	NULL, DB_CREATE | DB_INIT_LOCK | DB_INIT_MPOOL, 0)) != 0) {
        cerr << "failure: " << dbenv->strerror(dberr);
        exit (1);
    }

Here's a Java example creating a Berkeley DB environment using the 2.X interface:

    int dberr;
    DbEnv dbenv = new DbEnv();

    dbenv.set_error_stream(System.err);
    dbenv.set_errpfx("myprog");

    dbenv.appinit("/database/home",
        null, Db.DB_CREATE | Db.DB_INIT_LOCK | Db.DB_INIT_MPOOL);

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

    int dberr;
    DbEnv dbenv = new DbEnv(0);

    dbenv.set_error_stream(System.err);
    dbenv.set_errpfx("myprog");

    dbenv.open("/database/home",
        null, Db.DB_CREATE | Db.DB_INIT_LOCK | Db.DB_INIT_MPOOL, 0);

In the Berkeley DB 2.X release, DbEnv had accessors to obtain "managers" of type DbTxnMgr, DbMpool, DbLog, DbTxnMgr. If you used any of these managers, all their methods are now found directly in the DbEnv class.

Prev | Up | Next
db_value_set | Home | Db class for C++ and Java