Package org.springframework.jdbc.support

# Class JdbcTransactionManager

java.lang.Object
org.springframework.transaction.support.AbstractPlatformTransactionManager
org.springframework.jdbc.datasource.DataSourceTransactionManager
org.springframework.jdbc.support.JdbcTransactionManager

All Implemented Interfaces:
  `Serializable`, `org.springframework.beans.factory.InitializingBean`, `org.springframework.transaction.ConfigurableTransactionManager`, `org.springframework.transaction.PlatformTransactionManager`, `org.springframework.transaction.support.ResourceTransactionManager`, `org.springframework.transaction.TransactionManager`

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 (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(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.

Since:
  5.3
Author:
  Juergen Hoeller, Sebastien Deleuze
See Also:
  - `DataSourceTransactionManager`
  - `DataSourceTransactionManager.setDataSource(javax.sql.DataSource)`
  - `setExceptionTranslator(org.springframework.jdbc.support.SQLExceptionTranslator)`
  - Serialized Form

## Nested Class Summary

## Nested classes/interfaces inherited from class org.springframework.transaction.support.AbstractPlatformTransactionManager

`org.springframework.transaction.support.AbstractPlatformTransactionManager.SuspendedResourcesHolder`

## Field Summary

### Fields inherited from class org.springframework.transaction.support.AbstractPlatformTransactionManager

`logger, SYNCHRONIZATION_ALWAYS, SYNCHRONIZATION_NEVER, SYNCHRONIZATION_ON_ACTUAL_TRANSACTION`

## Constructor Summary

Constructors
Constructor
Description
`JdbcTransactionManager()`
Create a new `JdbcTransactionManager` instance.
`JdbcTransactionManager(DataSource dataSource)`
Create a new `JdbcTransactionManager` instance.

## Method Summary

Modifier and Type
Method
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 to use for this instance, creating a default if necessary.
`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 operates on.
`void`
`setExceptionTranslator(SQLExceptionTranslator exceptionTranslator)`
Set the exception translator for this transaction manager.
`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`.

### Methods inherited from class org.springframework.jdbc.datasource.DataSourceTransactionManager

`doBegin, doCleanupAfterCompletion, doCommit, doGetTransaction, doResume, doRollback, doSetRollbackOnly, doSuspend, getDataSource, getResourceFactory, isEnforceReadOnly, isExistingTransaction, obtainDataSource, prepareTransactionalConnection, setDataSource, setEnforceReadOnly`

### Methods inherited from class org.springframework.transaction.support.AbstractPlatformTransactionManager

`commit, 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, useSavepointForNestedTransaction`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Methods inherited from interface org.springframework.transaction.ConfigurableTransactionManager

`addListener`

### Methods inherited from interface org.springframework.transaction.PlatformTransactionManager

`commit, getTransaction, rollback`

## Constructor Details

### JdbcTransactionManager

public JdbcTransactionManager()
Create a new `JdbcTransactionManager` instance. A `DataSource` has to be set to be able to use it.

See Also:
  - `DataSourceTransactionManager.setDataSource(javax.sql.DataSource)`

### JdbcTransactionManager

public JdbcTransactionManager(DataSource dataSource)
Create a new `JdbcTransactionManager` instance.

Parameters:
  `dataSource` - the JDBC DataSource to manage transactions for

## Method Details

### setDatabaseProductName

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` - the database product name that identifies the error codes entry
See Also:
  - `setExceptionTranslator(org.springframework.jdbc.support.SQLExceptionTranslator)`
  - `SQLErrorCodeSQLExceptionTranslator.setDatabaseProductName(java.lang.String)`
  - `DatabaseMetaData.getDatabaseProductName()`
  - `JdbcAccessor.setDatabaseProductName(java.lang.String)`

### setExceptionTranslator

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:
  - `SQLErrorCodeSQLExceptionTranslator`
  - `SQLExceptionSubclassTranslator`
  - `JdbcAccessor.setExceptionTranslator(org.springframework.jdbc.support.SQLExceptionTranslator)`

### getExceptionTranslator

public SQLExceptionTranslator getExceptionTranslator()
Return the exception translator to use for this instance, creating a default if necessary.

See Also:
  - `setExceptionTranslator(org.springframework.jdbc.support.SQLExceptionTranslator)`

### setLazyInit

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()`

### isLazyInit

public boolean isLazyInit()
Return whether to lazily initialize the SQLExceptionTranslator for this transaction manager.

See Also:
  - `getExceptionTranslator()`

### afterPropertiesSet

public void afterPropertiesSet()
Eagerly initialize the exception translator, if demanded, creating a default one for the specified DataSource if none set.

Specified by:
  `afterPropertiesSet` in interface `org.springframework.beans.factory.InitializingBean`
Overrides:
  `afterPropertiesSet` in class `DataSourceTransactionManager`

### translateException

protected RuntimeException translateException(String task, SQLException ex)
This implementation attempts to use the `SQLExceptionTranslator`, falling back to a `TransactionSystemException`.

Overrides:
  `translateException` in 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 `DataAccessException` or a `TransactionException`
See Also:
  - `getExceptionTranslator()`
  - `DataSourceTransactionManager.translateException(java.lang.String, java.sql.SQLException)`