Opening a Transactional Environment and Container

Opening Berkeley DB Databases

To enable transactions for your environment, you must initialize the transactional subsystem. Note that doing this also initializes the logging subsystem. In addition, you must initialize the memory pool (in-memory cache). You must also initialize the locking subsystem. For example:

Notice in the following example that you first create the environment handle, and then you provide the handle to the XmlManager constructor. You do this because you cannot use transactions with the XmlManager instance's default internal environment.

package dbxml.txn;
                                                                                                                                   
import com.sleepycat.db.DatabaseException;
import com.sleepycat.db.Environment;
import com.sleepycat.db.EnvironmentConfig;
                                                                                                                                   
import com.sleepycat.dbxml.XmlManager;
import com.sleepycat.dbxml.XmlManagerConfig;
                                                                                                                                   
import java.io.File;
import java.io.FileNotFoundException;
                                                                                                                                   
...
                                                                                                                                   
Environment myEnv = null;
File envHome = new File("/export1/testEnv");
XmlManager myManager = null;
try {
    EnvironmentConfig envConf = new EnvironmentConfig();
    envConf.setAllowCreate(true);         // If the environment does not
                                          // exits, create it.
    envConf.setInitializeCache(true);     // Turn on the shared memory
                                          // region.
    envConf.setInitializeLocking(true);   // Turn on the locking subsystem.
    envConf.setInitializeLogging(true);   // Turn on the logging subsystem.
    envConf.setTransactional(true);       // Turn on the transactional
                                          // subsystem.
                                                                                                                                   
    myEnv = new Environment(envHome, envConf);
                                                                                                                                   
    XmlManagerConfig managerConfig = new XmlManagerConfig();
    myManager = new XmlManager(myEnv, managerConfig);
                                                                                                                                   
} catch (DatabaseException de) {
    // Exception handling goes here
} catch (FileNotFoundException fnfe) {
    // Exception handling goes here
} catch (Exception e) {
    // Exception handling goes here
} finally {
    try {
        if (myManager != null) {
            myManager.close();
        }
        if (myEnv != null) {
            myEnv.close();
        }
    } catch (Exception ce) {
        // Exception handling goes here
    } catch (DatabaseException de) {
        // Exception handling goes here
    }
} 

You then create and/or open your containers as normal. The only difference is that you must set XmlContainerConfig.setTransactional() to true and pass that object to the openContainer() or createContainer() method. For example:

package dbxml.txn;
                                                                                                                                   
import com.sleepycat.db.DatabaseException;
import com.sleepycat.db.Environment;
import com.sleepycat.db.EnvironmentConfig;
                                                                                                                                   
import com.sleepycat.dbxml.XmlContainer;
import com.sleepycat.dbxml.XmlContainerConfig;
import com.sleepycat.dbxml.XmlManager;
import com.sleepycat.dbxml.XmlManagerConfig;
                                                                                                                                   
import java.io.File;
import java.io.FileNotFoundException;
                                                                                                                                   
...
                                                                                                                                   
Environment myEnv = null;
File envHome = new File("/export1/testEnv");
XmlManager myManager = null;
XmlContainer myContainer = null;
try {
    EnvironmentConfig envConf = new EnvironmentConfig();
    envConf.setAllowCreate(true);         // If the environment does not
                                          // exits, create it.
    envConf.setInitializeCache(true);     // Turn on the shared memory
                                          // region.
    envConf.setInitializeLocking(true);   // Turn on the locking subsystem.
    envConf.setInitializeLogging(true);   // Turn on the logging subsystem.
    envConf.setTransactional(true);       // Turn on the transactional
                                          // subsystem.
                                                                                                                                   
    myEnv = new Environment(envHome, envConf);
                                                                                                                                   
    XmlManagerConfig managerConfig = new XmlManagerConfig();
    myManager = new XmlManager(myEnv, managerConfig);

    XmlContainerConfig containerConf = new XmlContainerConfig();
    containerConf.setTransactional(true);
    containerConf.setAllowCreate(true);
    String containerName = "myContainer.dbxml";
    myContainer = myManager.openContainer(containerName, containerConf);

                                                                                                                                   
} catch (DatabaseException de) {
    // Exception handling goes here
} catch (FileNotFoundException fnfe) {
    // Exception handling goes here
} catch (Exception e) {
    // Exception handling goes here
} finally {
    try {
        if (myManager != null) {
            myManager.close();
        }
        if (myEnv != null) {
            myEnv.close();
        }
    } catch (Exception ce) {
        // Exception handling goes here
    } catch (DatabaseException de) {
        // Exception handling goes here
    }
} 

Opening Berkeley DB Databases

It is possible to use Berkeley DB databases along side of BDB XML containers. When you do this, you will typically use both the databases and the containers from within the same environment so that you can combine operations to both using transactions.

There is no difference between opening a Berkeley DB database in an environment that uses containers and opening a database in an environment that does not use containers (see the Berkeley DB Getting Started with Transaction Processing guide for details on how to do this). You simply share the same environment handle between the two when you open the database(s) and container(s). For example:

package dbxml.txn;
                                                                                                                                   
import com.sleepycat.db.Database;
import com.sleepycat.db.DatabaseType;
import com.sleepycat.db.DatabaseConfig;
import com.sleepycat.db.DatabaseException;
import com.sleepycat.db.Environment;
import com.sleepycat.db.EnvironmentConfig;
                                                                                                                                   
import com.sleepycat.dbxml.XmlContainer;
import com.sleepycat.dbxml.XmlContainerConfig;
import com.sleepycat.dbxml.XmlManager;
import com.sleepycat.dbxml.XmlManagerConfig;
                                                                                                                                   
import java.io.File;
import java.io.FileNotFoundException;
                                                                                                                                   
...
                                                                                                                                   
Database myDatabase = null;
Environment myEnv = null;
File envHome = new File("/export1/testEnv");
XmlManager myManager = null;
XmlContainer myContainer = null;
try {
    EnvironmentConfig envConf = new EnvironmentConfig();
    envConf.setAllowCreate(true);         // If the environment does not
                                          // exits, create it.
    envConf.setInitializeCache(true);     // Turn on the shared memory
                                          // region.
    envConf.setInitializeLocking(true);   // Turn on the locking subsystem.
    envConf.setInitializeLogging(true);   // Turn on the logging subsystem.
    envConf.setTransactional(true);       // Turn on the transactional
                                          // subsystem.
                                                                                                                                   
    myEnv = new Environment(envHome, envConf);

    // Open the database.
    DatabaseConfig dbConfig = new DatabaseConfig();
    dbConfig.setTransactional(true);
    dbConfig.setType(DatabaseType.BTREE);
    myDatabase = myEnv.openDatabase(null,              // txn handle
                                    "sampleDatabase",  // db file name
                                    null,              // db name
                                    dbConfig);
                                                                                                                                   
    XmlManagerConfig managerConfig = new XmlManagerConfig();
    myManager = new XmlManager(myEnv, managerConfig);

    XmlContainerConfig containerConf = new XmlContainerConfig();
    containerConf.setTransactional(true);
    containerConf.setAllowCreate(true);
    String containerName = "myContainer.dbxml";
    myContainer = myManager.openContainer(containerName, containerConf);

                                                                                                                                   
} catch (DatabaseException de) {
    // Exception handling goes here
} catch (FileNotFoundException fnfe) {
    // Exception handling goes here
} catch (Exception e) {
    // Exception handling goes here
} finally {
    try {
        if (myManager != null) {
            myManager.close();
        }

        if (myDatabase != null) {
            myDatabase.close(0);
        }

        if (myEnv != null) {
            myEnv.close();
        }
    } catch (Exception ce) {
        // Exception handling goes here
    } catch (DatabaseException de) {
        // Exception handling goes here
    }
} 

Note

Never close a database that has active transactions. Make sure all transactions are resolved (either committed or aborted) before closing the database.