public class Transaction extends Object
Transaction.begin() is called a new transaction is started for
the current thread. To successfully complete a transaction and save its work,
call Transaction.commit(). To undo a transaction's work call
Transaction.rollback().
All DAO methods that perform their work on behalf of a transaction should
throw RollbackException if there is a problem performing the
work. By convention, the current thread's transaction is rolled back before
(or in the process of) throwing RollbackException. The reason
for rolling back the transaction is described in the message. Should the
transaction have been rolled back due to some underlying exception,
RollbackException will be thrown as a chained exception with the
underlying exception as its cause.
Because transactions can hold locks in the underlying database, it is recommended that user interactions not occur during a transaction. For similar reasons, a method that begins a transaction should ensure that all avenues of departure from the method either commit or roll back the transaction This is typically handled using :
try {
Transaction.begin();
// Application logic, including CRUD calls
Transaction.commit();
} catch (...) {
...
} finally {
if (Transaction.isActive()) Transaction.rollback();
}
Transactions cannot be nested. In other words, calling Transaction.begin() when a transaction is already active in a thread will throw RollbackException.
To write a method that starts a new transaction, unless one is already active, use recursion:
public void exampleSpecialDAOMethod(B bean) throws RollbackException {
if (!Transaction.isActive()) {
try {
Transaction.begin();
exampleSpecialDAOMethod(bean);
Transaction.commit();
return;
} finally {
if (Transaction.isActive())
Transaction.rollback();
}
}
// Application logic, including CRUD calls
}
| Modifier and Type | Method and Description |
|---|---|
static void |
begin()
Begins a new transaction for this thread.
|
static void |
commit()
Commits the work performed by this thread's currently running
transaction.
|
static boolean |
isActive()
Tests whether a transaction is currently running for this thread.
|
static void |
rollback()
Causes the work performed by the current thread's currently running
transaction to be undone.
|
static void |
setDebugOutput(OutputStream out)
Sets an OutputStream to which debugging output will be printed
for the current transaction.
|
public static void begin()
throws RollbackException
RollbackException - if there is some reason the transaction could not be started.
One reason is if you are already in a transaction.public static void commit()
throws RollbackException
RollbackException - if there is some reason the transaction could not be
committed.public static boolean isActive()
public static void rollback()
AssertionError - if not in a transaction.public static void setDebugOutput(OutputStream out)
out - an OutputStream to which debugging messages will be printed or
null to turn off printing of debug messages.Copyright © 2012-2016 Jeffrey L. Eppinger. All rights reserved. Permission granted for educational use only.