Chapter 2. Enabling Transactions

Table of Contents

Opening a Transactional Environment and Store or Database

In order to use transactions with your application, you must turn them on. To do this you must:

Opening a Transactional Environment and Store or Database

To enable transactions for your environment, you must initialize the transactional subsystem. For example, do this with the DPL:

package persist.txn;

import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;

import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.StoreConfig;

import java.io.File;

...

Environment myEnv = null;
EntityStore myStore = null;
try {
    EnvironmentConfig myEnvConfig = new EnvironmentConfig();
    StoreConfig storeConfig = new StoreConfig();

    myEnvConfig.setTransactional(true);
    storeConfig.setTransactional(true);

    myEnv = new Environment(new File("/my/env/home"),
                              myEnvConfig);
    myStore = new EntityStore(myEnv, "EntityStore", storeConfig);

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

And when using the base API:

package je.txn;

import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;

import java.io.File;

...

Environment myEnv = null;
try {
    EnvironmentConfig myEnvConfig = new EnvironmentConfig();
    myEnvConfig.setTransactional(true);

    myEnv = new Environment(new File("/my/env/home"),
                              myEnvConfig);

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

You then create and open your database(s) as you would for a non-transactional system. The only difference is that you must set DatabaseConfig.setTransactional() to true. Note that your database open must be transactional-protected. However, if you do not give the openDatabase() method a transaction handle, then the open is automatically protected using auto commit. Typically auto commit is used for this purpose. For example:

package je.txn;

import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
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);

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

Note

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