Opening the environment

Creating transaction-protected applications using the Berkeley DB library is quite easy. Applications first use DB_ENV->open() to initialize the database environment. Transaction-protected applications normally require all four Berkeley DB subsystems, so the DB_INIT_MPOOL, DB_INIT_LOCK, DB_INIT_LOG, and DB_INIT_TXN flags should be specified.

Once the application has called DB_ENV->open(), it opens its databases within the environment. Once the databases are opened, the application makes changes to the databases inside of transactions. Each set of changes that entails a unit of work should be surrounded by the appropriate DB_ENV->txn_begin(), DB_TXN->commit() and DB_TXN->abort() calls. The Berkeley DB access methods will make the appropriate calls into the Lock, Log and Memory Pool subsystems in order to guarantee transaction semantics. When the application is ready to exit, all outstanding transactions should have been committed or aborted.

Databases accessed by a transaction must not be closed during the transaction. Once all outstanding transactions are finished, all open Berkeley DB files should be closed. When the Berkeley DB database files have been closed, the environment should be closed by calling DB_ENV->close().

The following code fragment creates the database environment directory then opens the environment, running recovery. Our DB_ENV database environment handle is declared to be free-threaded using the DB_THREAD flag, and so may be used by any number of threads that we may subsequently create.

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <errno.h>
  4. #include <stdarg.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <db.h>
  9. #define ENV_DIRECTORY "TXNAPP"
  10. void env_dir_create(void);
  11. void env_open(DB_ENV **);
  12. ...
  13. int
  14. main(int argc, char *argv[])
  15. {
  16. extern int optind;
  17. DB_ENV *dbenv;
  18. int ch;
  19. while ((ch = getopt(argc, argv, "")) != EOF)
  20. switch (ch) {
  21. case '?':
  22. default:
  23. usage();
  24. }
  25. argc -= optind;
  26. argv += optind;
  27. env_dir_create();
  28. env_open(&dbenv);
  29. ...
  30. return (0);
  31. }
  32. ...
  33. void
  34. env_dir_create()
  35. {
  36. struct stat sb;
  37. /*
  38. * If the directory exists, we're done. We do not further check
  39. * the type of the file, DB will fail appropriately if it's the
  40. * wrong type.
  41. */
  42. if (stat(ENV_DIRECTORY, &sb) == 0)
  43. return;
  44. /* Create the directory, read/write/access owner only. */
  45. if (mkdir(ENV_DIRECTORY, S_IRWXU) != 0) {
  46. fprintf(stderr,
  47. "txnapp: mkdir: %s: %s\n", ENV_DIRECTORY, strerror(errno));
  48. exit (1);
  49. }
  50. }
  51. void
  52. env_open(DB_ENV **dbenvp)
  53. {
  54. DB_ENV *dbenv;
  55. int ret;
  56. /* Create the environment handle. */
  57. if ((ret = db_env_create(&dbenv, 0)) != 0) {
  58. fprintf(stderr,
  59. "txnapp: db_env_create: %s\n", db_strerror(ret));
  60. exit (1);
  61. }
  62. /* Set up error handling. */
  63. dbenv->set_errpfx(dbenv, "txnapp");
  64. dbenv->set_errfile(dbenv, stderr);
  65. /*
  66. * Open a transactional environment:
  67. * create if it doesn't exist
  68. * free-threaded handle
  69. * run recovery
  70. * read/write owner only
  71. */
  72. if ((ret = dbenv->open(dbenv, ENV_DIRECTORY,
  73. DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG |
  74. DB_INIT_MPOOL | DB_INIT_TXN | DB_RECOVER | DB_THREAD,
  75. S_IRUSR | S_IWUSR)) != 0) {
  76. (void)dbenv->close(dbenv, 0);
  77. fprintf(stderr, "dbenv->open: %s: %s\n",
  78. ENV_DIRECTORY, db_strerror(ret));
  79. exit (1);
  80. }
  81. *dbenvp = dbenv;
  82. }

After running this initial program, we can use the db_stat utility to display the contents of the environment directory:

  1. prompt> db_stat -e -h TXNAPP
  2. 3.2.1 Environment version.
  3. 120897 Magic number.
  4. 0 Panic value.
  5. 1 References.
  6. 6 Locks granted without waiting.
  7. 0 Locks granted after waiting.
  8. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  9. Mpool Region: 4.
  10. 264KB Size (270336 bytes).
  11. -1 Segment ID.
  12. 1 Locks granted without waiting.
  13. 0 Locks granted after waiting.
  14. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  15. Log Region: 3.
  16. 96KB Size (98304 bytes).
  17. -1 Segment ID.
  18. 3 Locks granted without waiting.
  19. 0 Locks granted after waiting.
  20. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  21. Lock Region: 2.
  22. 240KB Size (245760 bytes).
  23. -1 Segment ID.
  24. 1 Locks granted without waiting.
  25. 0 Locks granted after waiting.
  26. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  27. Txn Region: 5.
  28. 8KB Size (8192 bytes).
  29. -1 Segment ID.
  30. 1 Locks granted without waiting.
  31. 0 Locks granted after waiting.