### Berkeley DB Reference Guide: Transaction Protected Applications |

# Introduction

Creating transaction-protected applications using the Berkeley DB access methods is usually quite easy. Applications first use DBENV->open to initialize all of the Berkeley DB subsystems. Transaction-protected applications normally require all five Berkeley DB subsystems, so the DB_INIT_MPOOL, DB_INIT_LOCK, DB_INIT_LOG and DB_INIT_TXN flags should all be specified.

Once the application has called DBENV->open, it uses DB->open to open the databases it will use. Once the databases are opened, the application makes changes to the databases inside of transactions. Each set of changes that entail a unit of work should be surrounded by the appropriate txn_begin, txn_commit and txn_abort calls. Operations not surrounded by these calls will not be recoverable.

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 DBENV->close.

It is not necessary to transaction-protect read-only operations, unless those operations require repeatable reads. However, if there are update transactions present in the application, the reading transactions must still use locking, and should be prepared to repeat any operation (possibly closing and reopening a cursor) which fails with a return value of DB_LOCK_DEADLOCK.

Copyright Sleepycat Software