|
Once the environment has been created, database handles may be created and then opened within the environment. This is done by calling the db_create interface and specifying the appropriate environment as an argument.
File naming, database operations, and error handling will all be done as specified for the environment. For example, if the DB_INIT_LOCK or DB_INIT_CDB flags were specified when the environment was created or joined, database operations will automatically perform all necessary locking operations for the application.
The following is a simple example of opening two databases within a database environment:
DB_ENV *dbenv; DB *dbp1, *dbp2; int ret;
/* Open an environment with just a memory pool. */ if ((ret = dbenv->open(dbenv, home, DB_CREATE | DB_INIT_MPOOL, 0)) != 0) { dbenv->err(dbenv, ret, "environment open: %s", home); return (ret); }/* Open database #1. */ if ((ret = db_create(&dbp1, dbenv, 0)) != 0) { dbenv->err(dbenv, ret, "database create"); return (ret); } if ((ret = dbp1->open(dbp1, NULL, DATABASE1, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) { dbenv->err(dbenv, ret, "DB->open: %s", DATABASE1); return (ret); }
/* Open database #2. */ if ((ret = db_create(&dbp2, dbenv, 0)) != 0) { dbenv->err(dbenv, ret, "database create"); return (ret); } if ((ret = dbp2->open(dbp2, NULL, DATABASE2, NULL, DB_HASH, DB_CREATE, 0664)) != 0) { dbenv->err(dbenv, ret, "DB->open: %s", DATABASE2); return (ret); }