public class LocalTransaction
extends java.lang.Object
This instance is thread safe.
Begin a transaction by begin() or begin(TransactionIsolationLevel) and end
the transaction always by commit() or rollback().
LocalTransaction tx = DbConfig.singleton().getLocalTransaction();
try {
tx.begin();
Employee employee = dao.selectById(1);
employee.setName("SMITH");
employee.setJobType(JobType.PRESIDENT);
dao.update(employee);
tx.commit();
} finally {
tx.rollback();
}
Same instance can handle multiple transactions sequentially.
Db
LocalTransaction tx = DbConfig.singleton().getLocalTransaction();
// transaction 1
try {
tx.begin();
...
tx.commit();
} finally {
tx.rollback();
}
// transaction 2
try {
tx.begin();
...
tx.commit();
} finally {
tx.rollback();
}
Any exceptions that are thrown from this class methods roll back the transaction.| Modifier and Type | Field and Description |
|---|---|
protected java.lang.String |
className |
protected javax.sql.DataSource |
dataSource |
protected TransactionIsolationLevel |
defaultTransactionIsolationLevel |
protected JdbcLogger |
jdbcLogger |
protected java.lang.ThreadLocal<LocalTransactionContext> |
localTxContextHolder |
| Modifier | Constructor and Description |
|---|---|
protected |
LocalTransaction(javax.sql.DataSource dataSource,
java.lang.ThreadLocal<LocalTransactionContext> localTxContextHolder,
JdbcLogger jdbcLogger) |
protected |
LocalTransaction(javax.sql.DataSource dataSource,
java.lang.ThreadLocal<LocalTransactionContext> localTxContextHolder,
JdbcLogger jdbcLogger,
TransactionIsolationLevel defaultTransactionIsolationLevel) |
| Modifier and Type | Method and Description |
|---|---|
void |
begin()
Begin this transaction.
|
void |
begin(TransactionIsolationLevel transactionIsolationLevel)
Begin this transaction with the specified transaction isolation level.
|
protected void |
beginInternal(TransactionIsolationLevel transactionIsolationLevel,
java.lang.String callerMethodName) |
protected void |
closeConnection(java.sql.Connection connection) |
void |
commit()
Commits this transaction.
|
protected void |
end(java.lang.String callerMethodName) |
protected void |
endInternal(LocalTransactionContext context,
java.lang.String callerMethodName) |
protected LocalTransactionContext |
getLocalTransactionContext() |
boolean |
hasSavepoint(java.lang.String savepointName)
Whether this transaction has the specified save point.
|
boolean |
isActive()
Whether this transaction is active.
|
protected boolean |
isActiveInternal(LocalTransactionContext context) |
boolean |
isRollbackOnly()
Whether this transaction is marked to be undone.
|
protected void |
release(LocalTransactionContext context,
java.lang.String callerMethodName)
Releases the transaction context.
|
void |
releaseSavepoint(java.lang.String savepointName)
Removes the specified save point and subsequent save points from this transaction.
|
void |
resume(LocalTransactionContext context)
Resumes this transaction.
|
void |
rollback()
Undoes all changes made in this transaction.
|
void |
rollback(java.lang.String savepointName)
Undoes all changes made after the given save point.
|
protected void |
rollbackInternal(java.lang.String callerMethodName) |
void |
setRollbackOnly()
Marks this transaction to undo in the end of the transaction.
|
void |
setSavepoint(java.lang.String savepointName)
Creates a save point with the specified name.
|
LocalTransactionContext |
suspend()
Suspends this transaction.
|
java.lang.String |
toString()
Returns an unique string to identify this transaction.
|
protected final javax.sql.DataSource dataSource
protected final java.lang.ThreadLocal<LocalTransactionContext> localTxContextHolder
protected final JdbcLogger jdbcLogger
protected final TransactionIsolationLevel defaultTransactionIsolationLevel
protected final java.lang.String className
protected LocalTransaction(javax.sql.DataSource dataSource,
java.lang.ThreadLocal<LocalTransactionContext> localTxContextHolder,
JdbcLogger jdbcLogger)
protected LocalTransaction(javax.sql.DataSource dataSource,
java.lang.ThreadLocal<LocalTransactionContext> localTxContextHolder,
JdbcLogger jdbcLogger,
TransactionIsolationLevel defaultTransactionIsolationLevel)
public void begin()
TransactionAlreadyBegunException - if this transaction is already begunJdbcException - if a JDBC related error occurspublic void begin(TransactionIsolationLevel transactionIsolationLevel)
transactionIsolationLevel - the transaction isolation levelDomaNullPointerException - if the transactionIsolationLevel is nullTransactionAlreadyBegunException - if this transaction is already begunJdbcException - if a JDBC related error occursprotected void beginInternal(TransactionIsolationLevel transactionIsolationLevel, java.lang.String callerMethodName)
protected LocalTransactionContext getLocalTransactionContext()
public void commit()
TransactionNotYetBegunException - if this transaction is not yet begunJdbcException - if a JDBC related error occurspublic LocalTransactionContext suspend()
TransactionNotYetBegunException - if this transaction is not yet begunpublic void resume(LocalTransactionContext context)
This method does not throw any exceptions.
context - the transaction context that is returned from suspend()public void rollback()
If this transaction is not begun, this method does nothing.
This method does not throw any exceptions.
protected void rollbackInternal(java.lang.String callerMethodName)
public void setSavepoint(java.lang.String savepointName)
savepointName - the name of the save pointDomaNullPointerException - if the savepointName is nullTransactionNotYetBegunException - if this transaction is not yet begunSavepointAlreadyExistsException - if the save point already existsJdbcException - if a JDBC related error occurspublic boolean hasSavepoint(java.lang.String savepointName)
savepointName - the name of the save pointtrue if this transaction has the save pointDomaNullPointerException - if the savepointName is nullTransactionNotYetBegunException - if this transaction is not yet begunpublic void releaseSavepoint(java.lang.String savepointName)
savepointName - the name of the save pointDomaNullPointerException - if the savepointName is nullTransactionNotYetBegunException - if this transaction is not yet begunJdbcException - if a JDBC related error occurspublic void rollback(java.lang.String savepointName)
savepointName - the name of the save pointDomaNullPointerException - if the savepointName is nullSavepointNotFoundException - if the save point is not foundTransactionNotYetBegunException - if this transaction is not yet begunJdbcException - if a JDBC related error occursprotected void end(java.lang.String callerMethodName)
protected void endInternal(LocalTransactionContext context, java.lang.String callerMethodName)
protected void release(LocalTransactionContext context, java.lang.String callerMethodName)
This method does not throw any exceptions.
context - the transaction contextcallerMethodName - the caller method nameprotected void closeConnection(java.sql.Connection connection)
public java.lang.String toString()
toString in class java.lang.Objectpublic boolean isActive()
true if this transaction is activeprotected boolean isActiveInternal(LocalTransactionContext context)
public void setRollbackOnly()
public boolean isRollbackOnly()
true if the current transaction is marked.