Top Description Fields Constructors Methods
org.springframework.jdbc.support

public Class JdbcTransactionManager

extends DataSourceTransactionManager
Class Inheritance
Annotations
@SuppressWarnings:serial
Imports
java.sql.SQLException, javax.sql.DataSource, org.springframework.dao.DataAccessException, org.springframework.jdbc.datasource.DataSourceTransactionManager, org.springframework.lang.Nullable

JdbcAccessor-aligned subclass of the plain DataSourceTransactionManager, adding common JDBC exception translation for the commit and rollback step. Typically used in combination with org.springframework.jdbc.core.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 org.springframework.dao.ConcurrencyFailureException to callers instead of org.springframework.transaction.TransactionSystemException.

Analogous to HibernateTransactionManager and JpaTransactionManager, this transaction manager may throw DataAccessException from commit and possibly also from rollback. Calling code should be prepared for handling such exceptions next to org.springframework.transaction.TransactionException, which is generally sensible since TransactionSynchronization implementations may also throw such exceptions in their flush and beforeCommit phases.

Authors
Juergen Hoeller, Sebastien Deleuze
Since
5.3
See Also
DataSourceTransactionManager, setDataSource, setExceptionTranslator

Field Summary

Modifier and TypeField and Description
private volatile SQLExceptionTranslator
private boolean

Constructor Summary

AccessConstructor and Description
public
JdbcTransactionManager()

Create a new JdbcTransactionManager instance.

public
JdbcTransactionManager(DataSource
the JDBC DataSource to manage transactions for
dataSource
)

Create a new JdbcTransactionManager instance.

Method Summary

Modifier and TypeMethod and Description
public void
afterPropertiesSet()

Overrides org.springframework.jdbc.datasource.DataSourceTransactionManager.afterPropertiesSet.

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Eagerly initialize the exception translator, if demanded, creating a default one for the specified DataSource if none set.

public SQLExceptionTranslator
getExceptionTranslator()

Return the exception translator to use for this instance, creating a default if necessary.

public boolean
isLazyInit()

Return whether to lazily initialize the SQLExceptionTranslator for this transaction manager.

public void
setDatabaseProductName(String
the database product name that identifies the error codes entry
dbName
)

Specify the database product name for the DataSource that this transaction manager operates on.

public void
setExceptionTranslator(SQLExceptionTranslator exceptionTranslator)

Set the exception translator for this transaction manager.

public 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
the task description (commit or rollback)
task
,
SQLException
the SQLException thrown from commit/rollback
ex
)

Overrides org.springframework.jdbc.datasource.DataSourceTransactionManager.translateException.

This implementation attempts to use the SQLExceptionTranslator, falling back to a org.springframework.transaction.TransactionSystemException.

Inherited from org.springframework.jdbc.datasource.DataSourceTransactionManager:
doBegindoCleanupAfterCompletiondoCommitdoGetTransactiondoResumedoRollbackdoSetRollbackOnlydoSuspendgetDataSourcegetResourceFactoryisEnforceReadOnlyisExistingTransactionobtainDataSourceprepareTransactionalConnectionsetDataSourcesetEnforceReadOnly

Field Detail

exceptionTranslatorback to summary
private volatile SQLExceptionTranslator exceptionTranslator
Annotations
@Nullable
lazyInitback to summary
private boolean lazyInit

Constructor Detail

JdbcTransactionManagerback to summary
public JdbcTransactionManager()

Create a new JdbcTransactionManager instance. A DataSource has to be set to be able to use it.

See Also
setDataSource
JdbcTransactionManagerback to summary
public JdbcTransactionManager(DataSource dataSource)

Create a new JdbcTransactionManager instance.

Parameters
dataSource:DataSource

the JDBC DataSource to manage transactions for

Method Detail

afterPropertiesSetback to summary
public void afterPropertiesSet()

Overrides org.springframework.jdbc.datasource.DataSourceTransactionManager.afterPropertiesSet.

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Eagerly initialize the exception translator, if demanded, creating a default one for the specified DataSource if none set.

Annotations
@Override
getExceptionTranslatorback to summary
public SQLExceptionTranslator getExceptionTranslator()

Return the exception translator to use for this instance, creating a default if necessary.

See Also
setExceptionTranslator
isLazyInitback to summary
public boolean isLazyInit()

Return whether to lazily initialize the SQLExceptionTranslator for this transaction manager.

See Also
getExceptionTranslator()
setDatabaseProductNameback to summary
public void setDatabaseProductName(String dbName)

Specify the database product name for the DataSource that this transaction manager operates on. This allows for initializing a SQLErrorCodeSQLExceptionTranslator without obtaining a Connection from the DataSource to get the meta-data.

Parameters
dbName:String

the database product name that identifies the error codes entry

See Also
setExceptionTranslator, SQLErrorCodeSQLExceptionTranslator#setDatabaseProductName, java.sql.DatabaseMetaData#getDatabaseProductName(), JdbcAccessor#setDatabaseProductName
setExceptionTranslatorback to summary
public void setExceptionTranslator(SQLExceptionTranslator exceptionTranslator)

Set the exception translator for this transaction manager.

A SQLErrorCodeSQLExceptionTranslator used by default if a user-provided `sql-error-codes.xml` file has been found in the root of the classpath. Otherwise, SQLExceptionSubclassTranslator serves as the default translator as of 6.0.

See Also
org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator, org.springframework.jdbc.support.SQLExceptionSubclassTranslator, JdbcAccessor#setExceptionTranslator
setLazyInitback to summary
public 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
getExceptionTranslator(), afterPropertiesSet()
translateExceptionback to summary
protected RuntimeException translateException(String task, SQLException ex)

Overrides org.springframework.jdbc.datasource.DataSourceTransactionManager.translateException.

This implementation attempts to use the SQLExceptionTranslator, falling back to a org.springframework.transaction.TransactionSystemException.

Parameters
task:String

Doc from org.springframework.jdbc.datasource.DataSourceTransactionManager.translateException.

the task description (commit or rollback)

ex:SQLException

Doc from org.springframework.jdbc.datasource.DataSourceTransactionManager.translateException.

the SQLException thrown from commit/rollback

Returns:RuntimeException

Doc from org.springframework.jdbc.datasource.DataSourceTransactionManager.translateException.

the translated exception to throw, either a org.springframework.dao.DataAccessException or a org.springframework.transaction.TransactionException

Annotations
@Override
See Also
getExceptionTranslator(), DataSourceTransactionManager#translateException