Auto Commit
Prev | Chapter 3. Transaction Basics | Next

## Auto Commit

While transactions are frequently used to provide atomicity to multiple database or store operations, it is sometimes necessary to perform a single database or store 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 databases or store so that they support transactions. See Enabling Transactions for details.

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

Note that auto commit is not available for cursors. You must always open your cursor using a transaction if you want the cursor's operations to be transactional protected. See Transactional Cursors for details on using transactional cursors.

### Note

Never have more than one active transaction in your thread at a time. This is especially a problem if you mix an explicit transaction with another operation that uses auto commit. Doing so can result in undetectable deadlocks.

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

    package je.txn;

    import com.sleepycat.je.Database;
    import com.sleepycat.je.DatabaseConfig;
    import com.sleepycat.je.DatabaseEntry;
    import com.sleepycat.je.DatabaseException;
    import com.sleepycat.je.Environment;
    import com.sleepycat.je.EnvironmentConfig;

    import java.io.File;

    ...

    Database myDatabase = null;
    Environment myEnv = null;
    try {
        EnvironmentConfig myEnvConfig = new EnvironmentConfig();
        myEnvConfig.setTransactional(true);
        myEnv = new Environment(new File("/my/env/home"),
                                  myEnvConfig);

        // Open the database. Create it if it does not already exist.
        DatabaseConfig dbConfig = new DatabaseConfig();
        dbConfig.setTransactional(true);
        myDatabase = myEnv.openDatabase(null,
                                        "sampleDatabase",
                                        dbConfig);

        String keyString = "thekey";
        String dataString = "thedata";
        DatabaseEntry key =
            new DatabaseEntry(keyString.getBytes("UTF-8"));
        DatabaseEntry data =
            new DatabaseEntry(dataString.getBytes("UTF-8"));

        // Perform the write. Because the database was opened to
        // support transactions, this write is performed using auto commit.
        myDatabase.put(null, key, data);

    } catch (DatabaseException de) {
        // Exception handling goes here
    }

Prev | Up | Next
Aborting a Transaction | Home | Transactional Cursors