Table of Contents
Throughout this manual we have presented the concepts and mechanisms that you need to provide transactional protection for your application. In this chapter, we summarize these mechanisms, and we provide a complete example of a multi-threaded transactional JE application.
Transactional applications are characterized by performing the following activities:
Create your environment handle.
Open your environment, specifying that the transactional subsystem is to be used.
If you are using the base API, open your database handles, indicating that they are to support transactions. Otherwise, open your store such that it is configured for transactions.
Spawn off worker threads. How many of these you need and how they split their JE workload is entirely up to your application's requirements. However, any worker threads that perform write operations will do the following:
Begin a transaction.
Perform one or more read and write operations.
Commit the transaction if all goes well.
Abort and retry the operation if a deadlock is detected.
Abort the transaction for most other errors.
On application shutdown:
Make sure there are no opened cursors.
Make sure there are no active transactions. Either abort or commit all transactions before shutting down.
Close your databases.
Close your environment.
Robust JE applications should monitor their worker threads to make sure they have not died unexpectedly. If a thread does terminate abnormally, you must shutdown all your worker threads and then run normal recovery (you will have to reopen your environment to do this). This is the only way to clear any resources (such as a lock or a mutex) that the abnormally exiting worker thread might have been holding at the time that it died.
Failure to perform this recovery can cause your still-functioning worker threads to eventually block forever while waiting for a lock that will never be released.
In addition to these activities, which are entirely handled by code within your application, you also need to periodically back up your log files. This is required in order to obtain the durability guarantee made by JE's transaction ACID support. See Backing up and Restoring Berkeley DB, Java Edition Applications for more information.