Auto Commit

While transactions are frequently used to provide atomicity to multiple container operations, it is sometimes necessary to perform a single container operation under the control of a transaction. Rather than force you to obtain a transaction, perform the single write operation, and then either commit or abort the transaction, you can automatically group this sequence of events using auto commit.

To use auto commit:

  1. Open your environment and your containers so that they support transactions. See Enabling Transactions for details.

  2. Do not provide a transactional handle to the method that is performing the container write operation.

For example, the following uses auto commit to perform the container write operation:

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.XmlException;
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);


    String file = "doc1.xml";
    try {
        // Need an update context for the put.
        XmlUpdateContext theContext = myManager.createUpdateContext();
                                                                                                                                     
        // Get the input stream.
        XmlInputStream theStream =
            myManager.createLocalFileInputStream(file1);
                                                                                                                                     
        // Put the document. Because the container was opened to 
        // support transactions, this write is performed using 
        // auto commit.
        myContainer.putDocument(file,       // The document's name
                                theStream,   // The actual document.
                                theContext,  // The update context
                                             // (required).
                                0);          // Put flags.
                                                                                                                                     
    } catch (XmlException e) {
        System.out.println("Error in document write: ");
        System.out.println(e.what());
    } 
} 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
    }
}