public class JdbcTransactionManager extends DataSourceTransactionManager
JdbcAccessor
-aligned subclass of the plain DataSourceTransactionManager
,
adding common JDBC exception translation for the commit and rollback step.
Typically used in combination with JdbcTemplate
which applies the same SQLExceptionTranslator
infrastructure by default.
Exception translation is specifically relevant for commit steps in serializable
transactions (e.g. on Postgres) where concurrency failures may occur late on commit.
This allows for throwing ConcurrencyFailureException
to
callers instead of TransactionSystemException
.
Analogous to HibernateTransactionManager
and JpaTransactionManager
,
this transaction manager may throw DataAccessException
from AbstractPlatformTransactionManager.commit(org.springframework.transaction.TransactionStatus)
and possibly also from AbstractPlatformTransactionManager.rollback(org.springframework.transaction.TransactionStatus)
. Calling code should be prepared for handling
such exceptions next to TransactionException
,
which is generally sensible since TransactionSynchronization
implementations
may also throw such exceptions in their flush
and beforeCommit
phases.
DataSourceTransactionManager
,
DataSourceTransactionManager.setDataSource(javax.sql.DataSource)
,
setExceptionTranslator(org.springframework.jdbc.support.SQLExceptionTranslator)
,
Serialized FormAbstractPlatformTransactionManager.SuspendedResourcesHolder
logger, SYNCHRONIZATION_ALWAYS, SYNCHRONIZATION_NEVER, SYNCHRONIZATION_ON_ACTUAL_TRANSACTION
Constructor and Description |
---|
JdbcTransactionManager()
Create a new JdbcTransactionManager instance.
|
JdbcTransactionManager(DataSource dataSource)
Create a new JdbcTransactionManager instance.
|
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Eagerly initialize the exception translator, if demanded,
creating a default one for the specified DataSource if none set.
|
SQLExceptionTranslator |
getExceptionTranslator()
Return the exception translator for this instance.
|
boolean |
isLazyInit()
Return whether to lazily initialize the SQLExceptionTranslator for this transaction manager.
|
void |
setDatabaseProductName(String dbName)
Specify the database product name for the DataSource that this transaction manager
uses.
|
void |
setExceptionTranslator(SQLExceptionTranslator exceptionTranslator)
Set the exception translator for this instance.
|
void |
setLazyInit(boolean lazyInit)
Set whether to lazily initialize the SQLExceptionTranslator for this transaction manager,
on first encounter of an SQLException.
|
protected RuntimeException |
translateException(String task,
SQLException ex)
This implementation attempts to use the
SQLExceptionTranslator ,
falling back to a TransactionSystemException . |
doBegin, doCleanupAfterCompletion, doCommit, doGetTransaction, doResume, doRollback, doSetRollbackOnly, doSuspend, getDataSource, getResourceFactory, isEnforceReadOnly, isExistingTransaction, obtainDataSource, prepareTransactionalConnection, setDataSource, setEnforceReadOnly
commit, determineTimeout, getDefaultTimeout, getTransaction, getTransactionSynchronization, invokeAfterCompletion, isFailEarlyOnGlobalRollbackOnly, isGlobalRollbackOnParticipationFailure, isNestedTransactionAllowed, isRollbackOnCommitFailure, isValidateExistingTransaction, newTransactionStatus, prepareForCommit, prepareSynchronization, prepareTransactionStatus, registerAfterCompletionWithExistingTransaction, resume, rollback, setDefaultTimeout, setFailEarlyOnGlobalRollbackOnly, setGlobalRollbackOnParticipationFailure, setNestedTransactionAllowed, setRollbackOnCommitFailure, setTransactionSynchronization, setTransactionSynchronizationName, setValidateExistingTransaction, shouldCommitOnGlobalRollbackOnly, suspend, triggerBeforeCommit, triggerBeforeCompletion, useSavepointForNestedTransaction
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
commit, getTransaction, rollback
public JdbcTransactionManager()
public JdbcTransactionManager(DataSource dataSource)
dataSource
- the JDBC DataSource to manage transactions forpublic void setDatabaseProductName(String dbName)
dbName
- the database product name that identifies the error codes entryJdbcAccessor.setDatabaseProductName(java.lang.String)
,
SQLErrorCodeSQLExceptionTranslator.setDatabaseProductName(java.lang.String)
,
DatabaseMetaData.getDatabaseProductName()
public void setExceptionTranslator(SQLExceptionTranslator exceptionTranslator)
If no custom translator is provided, a default
SQLErrorCodeSQLExceptionTranslator
is used
which examines the SQLException's vendor-specific error code.
public SQLExceptionTranslator getExceptionTranslator()
Creates a default SQLErrorCodeSQLExceptionTranslator
for the specified DataSource if none set.
public void setLazyInit(boolean lazyInit)
Early initialization just applies if afterPropertiesSet()
is called.
getExceptionTranslator()
,
afterPropertiesSet()
public boolean isLazyInit()
getExceptionTranslator()
public void afterPropertiesSet()
afterPropertiesSet
in interface InitializingBean
afterPropertiesSet
in class DataSourceTransactionManager
protected RuntimeException translateException(String task, SQLException ex)
SQLExceptionTranslator
,
falling back to a TransactionSystemException
.translateException
in class DataSourceTransactionManager
task
- the task description (commit or rollback)ex
- the SQLException thrown from commit/rollbackDataAccessException
or a
TransactionException
getExceptionTranslator()
,
DataSourceTransactionManager.translateException(java.lang.String, java.sql.SQLException)