Opening the databases

Next, we open three databases (“color” and “fruit” and “cats”), in the database environment. Again, our DB database handles are declared to be free-threaded using the DB_THREAD flag, and so may be used by any number of threads we subsequently create.

  1. int
  2. main(int argc, char *argv[])
  3. {
  4. extern int optind;
  5. DB *db_cats, *db_color, *db_fruit;
  6. DB_ENV *dbenv;
  7. int ch;
  8. while ((ch = getopt(argc, argv, "")) != EOF)
  9. switch (ch) {
  10. case '?':
  11. default:
  12. usage();
  13. }
  14. argc -= optind;
  15. argv += optind;
  16. env_dir_create();
  17. env_open(&dbenv);
  18. ...
  19. /* Open database: Key is fruit class; Data is specific type. */
  20. if (db_open(dbenv, &db_fruit, "fruit", 0))
  21. return (1);
  22. /* Open database: Key is a color; Data is an integer. */
  23. if (db_open(dbenv, &db_color, "color", 0))
  24. return (1);
  25. /*
  26. * Open database:
  27. * Key is a name; Data is: company name, cat breeds.
  28. */
  29. if (db_open(dbenv, &db_cats, "cats", 1))
  30. return (1);
  31. ...
  32. return (0);
  33. }
  34. int
  35. db_open(DB_ENV *dbenv, DB **dbp, char *name, int dups)
  36. {
  37. DB *db;
  38. int ret;
  39. /* Create the database handle. */
  40. if ((ret = db_create(&db, dbenv, 0)) != 0) {
  41. dbenv->err(dbenv, ret, "db_create");
  42. return (1);
  43. }
  44. /* Optionally, turn on duplicate data items. */
  45. if (dups && (ret = db->set_flags(db, DB_DUP)) != 0) {
  46. (void)db->close(db, 0);
  47. dbenv->err(dbenv, ret, "db->set_flags: DB_DUP");
  48. return (1);
  49. }
  50. /*
  51. * Open a database in the environment:
  52. * create if it doesn't exist
  53. * free-threaded handle
  54. * read/write owner only
  55. */
  56. if ((ret = db->open(db, NULL, name, NULL, DB_BTREE,
  57. DB_AUTO_COMMIT | DB_CREATE | DB_THREAD, S_IRUSR | S_IWUSR)) != 0) {
  58. (void)db->close(db, 0);
  59. dbenv->err(dbenv, ret, "db->open: %s", name);
  60. return (1);
  61. }
  62. *dbp = db;
  63. return (0);
  64. }

After opening the database, we can use the db_stat utility to display information about a database we have created:

  1. prompt> db_stat -h TXNAPP -d color
  2. 53162 Btree magic number.
  3. 8 Btree version number.
  4. Flags:
  5. 2 Minimum keys per-page.
  6. 8192 Underlying database page size.
  7. 1 Number of levels in the tree.
  8. 0 Number of unique keys in the tree.
  9. 0 Number of data items in the tree.
  10. 0 Number of tree internal pages.
  11. 0 Number of bytes free in tree internal pages (0% ff).
  12. 1 Number of tree leaf pages.
  13. 8166 Number of bytes free in tree leaf pages (0.% ff).
  14. 0 Number of tree duplicate pages.
  15. 0 Number of bytes free in tree duplicate pages (0% ff).
  16. 0 Number of tree overflow pages.
  17. 0 Number of bytes free in tree overflow pages (0% ff).
  18. 0 Number of pages on the free list.

The database open must be enclosed within a transaction in order to be recoverable. The transaction will ensure that created files are re-created in recovered environments (or do not appear at all). Additional database operations or operations on other databases can be included in the same transaction, of course. In the simple case, where the open is the only operation in the transaction, an application can set the DB_AUTO_COMMIT flag instead of creating and managing its own transaction handle. The DB_AUTO_COMMIT flag will internally wrap the operation in a transaction, simplifying application code.

The previous example is the simplest case of transaction protection for database open. Obviously, additional database operations can be done in the scope of the same transaction. For example, an application maintaining a list of the databases in a database environment in a well-known file might include an update of the list in the same transaction in which the database is created. Or, an application might create both a primary and secondary database in a single transaction.

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