java.lang.Object
org.seasar.doma.jdbc.tx.LocalTransaction
- Direct Known Subclasses:
KeepAliveLocalTransaction
A local transaction.
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.-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Stringprotected final DataSourceprotected final TransactionIsolationLevelprotected final JdbcLoggerprotected final ThreadLocal<LocalTransactionContext> -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedLocalTransaction(DataSource dataSource, ThreadLocal<LocalTransactionContext> localTxContextHolder, JdbcLogger jdbcLogger) protectedLocalTransaction(DataSource dataSource, ThreadLocal<LocalTransactionContext> localTxContextHolder, JdbcLogger jdbcLogger, TransactionIsolationLevel defaultTransactionIsolationLevel) -
Method Summary
Modifier and TypeMethodDescriptionvoidbegin()Begin this transaction.voidbegin(TransactionIsolationLevel transactionIsolationLevel) Begin this transaction with the specified transaction isolation level.protected voidbeginInternal(TransactionIsolationLevel transactionIsolationLevel, String callerMethodName) protected voidcloseConnection(Connection connection) voidcommit()Commits this transaction.protected voidprotected voidendInternal(LocalTransactionContext context, String callerMethodName) protected LocalTransactionContextbooleanhasSavepoint(String savepointName) Whether this transaction has the specified save point.booleanisActive()Whether this transaction is active.protected booleanisActiveInternal(LocalTransactionContext context) booleanWhether this transaction is marked to be undone.protected voidrelease(LocalTransactionContext context, String callerMethodName) Releases the transaction context.voidreleaseSavepoint(String savepointName) Removes the specified save point and subsequent save points from this transaction.voidresume(LocalTransactionContext context) Resumes this transaction.voidrollback()Undoes all changes made in this transaction.voidUndoes all changes made after the given save point.protected voidrollbackInternal(String callerMethodName) voidMarks this transaction to undo in the end of the transaction.voidsetSavepoint(String savepointName) Creates a save point with the specified name.suspend()Suspends this transaction.toString()Returns an unique string to identify this transaction.
-
Field Details
-
dataSource
-
localTxContextHolder
-
jdbcLogger
-
defaultTransactionIsolationLevel
-
className
-
-
Constructor Details
-
LocalTransaction
protected LocalTransaction(DataSource dataSource, ThreadLocal<LocalTransactionContext> localTxContextHolder, JdbcLogger jdbcLogger) -
LocalTransaction
protected LocalTransaction(DataSource dataSource, ThreadLocal<LocalTransactionContext> localTxContextHolder, JdbcLogger jdbcLogger, TransactionIsolationLevel defaultTransactionIsolationLevel)
-
-
Method Details
-
begin
public void begin()Begin this transaction.- Throws:
TransactionAlreadyBegunException- if this transaction is already begunJdbcException- if a JDBC related error occurs
-
begin
Begin this transaction with the specified transaction isolation level.- Parameters:
transactionIsolationLevel- the transaction isolation level- Throws:
DomaNullPointerException- if thetransactionIsolationLevelisnullTransactionAlreadyBegunException- if this transaction is already begunJdbcException- if a JDBC related error occurs
-
beginInternal
protected void beginInternal(TransactionIsolationLevel transactionIsolationLevel, String callerMethodName) -
getLocalTransactionContext
-
commit
public void commit()Commits this transaction.- Throws:
TransactionNotYetBegunException- if this transaction is not yet begunJdbcException- if a JDBC related error occurs
-
suspend
Suspends this transaction.- Returns:
- the transaction context that is required to resume this transaction
- Throws:
TransactionNotYetBegunException- if this transaction is not yet begun
-
resume
Resumes this transaction.This method does not throw any exceptions.
- Parameters:
context- the transaction context that is returned fromsuspend()
-
rollback
public void rollback()Undoes all changes made in this transaction.If this transaction is not begun, this method does nothing.
This method does not throw any exceptions.
-
rollbackInternal
-
setSavepoint
Creates a save point with the specified name.- Parameters:
savepointName- the name of the save point- Throws:
DomaNullPointerException- if thesavepointNameisnullTransactionNotYetBegunException- if this transaction is not yet begunSavepointAlreadyExistsException- if the save point already existsJdbcException- if a JDBC related error occurs
-
hasSavepoint
Whether this transaction has the specified save point.- Parameters:
savepointName- the name of the save point- Returns:
trueif this transaction has the save point- Throws:
DomaNullPointerException- if thesavepointNameisnullTransactionNotYetBegunException- if this transaction is not yet begun
-
releaseSavepoint
Removes the specified save point and subsequent save points from this transaction.- Parameters:
savepointName- the name of the save point- Throws:
DomaNullPointerException- if thesavepointNameisnullTransactionNotYetBegunException- if this transaction is not yet begunJdbcException- if a JDBC related error occurs
-
rollback
Undoes all changes made after the given save point.- Parameters:
savepointName- the name of the save point- Throws:
DomaNullPointerException- if thesavepointNameisnullSavepointNotFoundException- if the save point is not foundTransactionNotYetBegunException- if this transaction is not yet begunJdbcException- if a JDBC related error occurs
-
end
-
endInternal
-
release
Releases the transaction context.This method does not throw any exceptions.
- Parameters:
context- the transaction contextcallerMethodName- the caller method name
-
closeConnection
-
toString
Returns an unique string to identify this transaction. -
isActive
public boolean isActive()Whether this transaction is active.- Returns:
trueif this transaction is active
-
isActiveInternal
-
setRollbackOnly
public void setRollbackOnly()Marks this transaction to undo in the end of the transaction. -
isRollbackOnly
public boolean isRollbackOnly()Whether this transaction is marked to be undone.- Returns:
trueif the current transaction is marked.
-