Managing Transaction Rollbacks
Prev | Chapter 3. Transaction Management | Next

## Managing Transaction Rollbacks

In the event that a new Master is elected, it is possible for a Replica to find that some of its logs are ahead of the logs held by the Master. While this is unlikely to occur, your code must still be ready to deal with the situation. When it happens, you must roll back the transactions represented by the logs that are ahead of the Master.

You do this by simply closing all your ReplicatedEnvironment handles, and then reopening. During the handshaking process that occurs when the Replica joins the replication group, the discrepancy in log files are resolved for you.

JE HA lets your application know that a transaction must be rolled back by throwing RollbackException. This exception can by thrown by any operation that is performing routine database access.

        ReplicatedEnvironment repEnv = new ReplicatedEnvironment(...);
        boolean doWork = true;

        while doWork {
            try {
                // performSomeDBWork is the method that
                // performs your database access.
                doWork = performSomeDBWork();
            } catch (RollbackException rb) {
                if (repEnv != null) {
                    repEnv.close();
                    repEnv = new ReplicatedEnvironment(...);
            }
        }

Prev | Up | Next
Consistency and Durability Use Cases | Home | Example Run Transaction Class