Class JdbcTransactionManager
- All Implemented Interfaces:
- Serializable, InitializingBean, ConfigurableTransactionManager, PlatformTransactionManager, ResourceTransactionManager, TransactionManager
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 (for example, 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(TransactionStatus)
and possibly also from AbstractPlatformTransactionManager.rollback(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.
- Since:
- 5.3
- Author:
- Juergen Hoeller, Sebastien Deleuze
- See Also:
- 
Nested Class SummaryNested classes/interfaces inherited from class AbstractPlatformTransactionManagerAbstractPlatformTransactionManager.SuspendedResourcesHolder
- 
Field SummaryFields inherited from class AbstractPlatformTransactionManagerlogger, SYNCHRONIZATION_ALWAYS, SYNCHRONIZATION_NEVER, SYNCHRONIZATION_ON_ACTUAL_TRANSACTION
- 
Constructor SummaryConstructorsConstructorDescriptionCreate a newJdbcTransactionManagerinstance.JdbcTransactionManager(DataSource dataSource) Create a newJdbcTransactionManagerinstance.
- 
Method SummaryModifier and TypeMethodDescriptionvoidEagerly initialize the exception translator, if demanded, creating a default one for the specified DataSource if none set.Return the exception translator to use for this instance, creating a default if necessary.booleanReturn whether to lazily initialize the SQLExceptionTranslator for this transaction manager.voidsetDatabaseProductName(String dbName) Specify the database product name for theDataSourcethat this transaction manager operates on.voidsetExceptionTranslator(SQLExceptionTranslator exceptionTranslator) Set the exception translator for this transaction manager.voidsetLazyInit(boolean lazyInit) Set whether to lazily initialize the SQLExceptionTranslator for this transaction manager, on first encounter of an SQLException.protected RuntimeExceptiontranslateException(String task, SQLException ex) This implementation attempts to use theSQLExceptionTranslator, falling back to aTransactionSystemException.Methods inherited from class DataSourceTransactionManagerdoBegin, doCleanupAfterCompletion, doCommit, doGetTransaction, doResume, doRollback, doSetRollbackOnly, doSuspend, getDataSource, getResourceFactory, isEnforceReadOnly, isExistingTransaction, obtainDataSource, prepareTransactionalConnection, setDataSource, setEnforceReadOnlyMethods inherited from class AbstractPlatformTransactionManagercommit, determineTimeout, getDefaultTimeout, getTransaction, getTransactionExecutionListeners, getTransactionSynchronization, invokeAfterCompletion, isFailEarlyOnGlobalRollbackOnly, isGlobalRollbackOnParticipationFailure, isNestedTransactionAllowed, isRollbackOnCommitFailure, isValidateExistingTransaction, prepareForCommit, prepareSynchronization, registerAfterCompletionWithExistingTransaction, resume, rollback, setDefaultTimeout, setFailEarlyOnGlobalRollbackOnly, setGlobalRollbackOnParticipationFailure, setNestedTransactionAllowed, setRollbackOnCommitFailure, setTransactionExecutionListeners, setTransactionSynchronization, setTransactionSynchronizationName, setValidateExistingTransaction, shouldCommitOnGlobalRollbackOnly, suspend, triggerBeforeCommit, triggerBeforeCompletion, useSavepointForNestedTransactionMethods inherited from class Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ConfigurableTransactionManageraddListenerMethods inherited from interface PlatformTransactionManagercommit, getTransaction, rollback
- 
Constructor Details- 
JdbcTransactionManagerpublic JdbcTransactionManager()Create a newJdbcTransactionManagerinstance. ADataSourcehas to be set to be able to use it.- See Also:
 
- 
JdbcTransactionManagerCreate a newJdbcTransactionManagerinstance.- Parameters:
- dataSource- the JDBC DataSource to manage transactions for
 
 
- 
- 
Method Details- 
setDatabaseProductNameSpecify the database product name for theDataSourcethat this transaction manager operates on. This allows for initializing aSQLErrorCodeSQLExceptionTranslatorwithout obtaining aConnectionfrom theDataSourceto get the meta-data.- Parameters:
- dbName- the database product name that identifies the error codes entry
- See Also:
 
- 
setExceptionTranslatorSet the exception translator for this transaction manager.A SQLErrorCodeSQLExceptionTranslatorused by default if a user-provided `sql-error-codes.xml` file has been found in the root of the classpath. Otherwise,SQLExceptionSubclassTranslatorserves as the default translator as of 6.0.- See Also:
 
- 
getExceptionTranslatorReturn the exception translator to use for this instance, creating a default if necessary.- See Also:
 
- 
setLazyInitpublic void setLazyInit(boolean lazyInit) Set whether to lazily initialize the SQLExceptionTranslator for this transaction manager, on first encounter of an SQLException. Default is "true"; can be switched to "false" for initialization on startup.Early initialization just applies if afterPropertiesSet()is called.- See Also:
 
- 
isLazyInitpublic boolean isLazyInit()Return whether to lazily initialize the SQLExceptionTranslator for this transaction manager.- See Also:
 
- 
afterPropertiesSetpublic void afterPropertiesSet()Eagerly initialize the exception translator, if demanded, creating a default one for the specified DataSource if none set.- Specified by:
- afterPropertiesSetin interface- InitializingBean
- Overrides:
- afterPropertiesSetin class- DataSourceTransactionManager
 
- 
translateExceptionThis implementation attempts to use theSQLExceptionTranslator, falling back to aTransactionSystemException.- Overrides:
- translateExceptionin class- DataSourceTransactionManager
- Parameters:
- task- the task description (commit or rollback)
- ex- the SQLException thrown from commit/rollback
- Returns:
- the translated exception to throw, either a
DataAccessExceptionor aTransactionException
- See Also:
 
 
-