java.lang.Object
org.seasar.doma.jdbc.tx.LocalTransaction
org.seasar.doma.jdbc.tx.KeepAliveLocalTransaction
A local transaction that keeps a JDBC connection open until the transaction is explicitly
destroyed.
But the connection is closed, if any exceptions are thrown.
This instance is thread safe.
KeepAliveLocalTransaction tx = DbConfig.singleton().getKeepAliveLocalTransaction();
tx.init();
try {
try {
tx.begin();
Employee employee = dao.selectById(1);
employee.setName("SMITH");
employee.setJobType(JobType.PRESIDENT);
dao.update(employee);
tx.commit();
} finally {
tx.rollback();
}
try {
tx.begin();
Employee employee = dao.selectById(2);
employee.setName("foo");
employee.setJobType(JobType.SALESMAN);
dao.update(employee);
tx.commit();
} finally {
tx.rollback();
}
} finally {
tx.destroy();
}
-
Field Summary
Fields inherited from class org.seasar.doma.jdbc.tx.LocalTransaction
className, dataSource, defaultTransactionIsolationLevel, jdbcLogger, localTxContextHolder -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedKeepAliveLocalTransaction(DataSource dataSource, ThreadLocal<LocalTransactionContext> localTxContextHolder, JdbcLogger jdbcLogger) Creates an instance.protectedKeepAliveLocalTransaction(DataSource dataSource, ThreadLocal<LocalTransactionContext> localTxContextHolder, JdbcLogger jdbcLogger, TransactionIsolationLevel defaultTransactionIsolationLevel) Creates an instance with the specified transaction isolation level. -
Method Summary
Modifier and TypeMethodDescriptionvoiddestroy()Destroy the transaction context.protected voidendInternal(LocalTransactionContext context, String callerMethodName) Ends the transaction, but does not close the JDBC connection.protected LocalTransactionContextvoidinit()Initializes a transaction context.protected booleanisActiveInternal(LocalTransactionContext context) Methods inherited from class org.seasar.doma.jdbc.tx.LocalTransaction
begin, begin, beginInternal, closeConnection, commit, end, hasSavepoint, isActive, isRollbackOnly, release, releaseSavepoint, resume, rollback, rollback, rollbackInternal, setRollbackOnly, setSavepoint, suspend, toString
-
Constructor Details
-
KeepAliveLocalTransaction
protected KeepAliveLocalTransaction(DataSource dataSource, ThreadLocal<LocalTransactionContext> localTxContextHolder, JdbcLogger jdbcLogger) Creates an instance.- Parameters:
dataSource- the data sourcelocalTxContextHolder- the holder of the transaction contextjdbcLogger- the logger
-
KeepAliveLocalTransaction
protected KeepAliveLocalTransaction(DataSource dataSource, ThreadLocal<LocalTransactionContext> localTxContextHolder, JdbcLogger jdbcLogger, TransactionIsolationLevel defaultTransactionIsolationLevel) Creates an instance with the specified transaction isolation level.- Parameters:
dataSource- the data sourcelocalTxContextHolder- the holder of the transaction contextjdbcLogger- the loggerdefaultTransactionIsolationLevel- the default transaction isolation level
-
-
Method Details
-
init
public void init()Initializes a transaction context.This method establishes a JDBC connection.
If you invoke
LocalTransaction.begin()before invoking this method, theLocalTransaction.begin()method establishes a JDBC connection.- Throws:
JdbcException- if a JDBC related error occurs
-
getLocalTransactionContext
- Overrides:
getLocalTransactionContextin classLocalTransaction
-
destroy
public void destroy()Destroy the transaction context.This method closes the JDBC connection.
This method does not throw any exceptions.
-
endInternal
Ends the transaction, but does not close the JDBC connection.- Overrides:
endInternalin classLocalTransaction
-
isActiveInternal
- Overrides:
isActiveInternalin classLocalTransaction
-