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

public Class IsolationLevelDataSourceAdapter

extends UserCredentialsDataSourceAdapter
Class Inheritance
Imports
java.sql.Connection, .SQLException, java.util.Map, org.springframework.lang.Nullable, org.springframework.transaction.TransactionDefinition, org.springframework.transaction.support.TransactionSynchronizationManager, org.springframework.util.Assert

An adapter for a target javax.sql.DataSource, applying the current Spring transaction's isolation level (and potentially specified user credentials) to every getConnection call. Also applies the read-only flag, if specified.

Can be used to proxy a target JNDI DataSource that does not have the desired isolation level (and user credentials) configured. Client code can work with this DataSource as usual, not worrying about such settings.

Inherits the capability to apply specific user credentials from its superclass UserCredentialsDataSourceAdapter; see the latter's javadoc for details on that functionality (e.g. setCredentialsForCurrentThread).

Warning

This adapter simply calls java.sql.Connection#setTransactionIsolation and/or java.sql.Connection#setReadOnly for every Connection obtained from it. It does, however, not reset those settings; it rather expects the target DataSource to perform such resetting as part of its connection pool handling. Make sure that the target DataSource properly cleans up such transaction state.

Authors
Juergen Hoeller, Sam Brannen
Since
2.0.3
See Also
setIsolationLevel, setIsolationLevelName, setUsername, setPassword

Field Summary

Modifier and TypeField and Description
pack-priv static final Map<String, Integer>
constants

Map of constant names to constant values for the isolation constants defined in TransactionDefinition.

private Integer

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected Connection
doGetConnection(String
the username to use
username
,
String
the password to use
password
)

Overrides org.springframework.jdbc.datasource.UserCredentialsDataSourceAdapter.doGetConnection.

Applies the current isolation level value and read-only flag to the returned Connection.
protected Integer

Returns:

the current isolation level, or null if none
getCurrentIsolationLevel
()

Determine the current isolation level: either the transaction's isolation level or a statically defined isolation level.

protected Boolean

Returns:

whether there is a read-only hint for the current scope
getCurrentReadOnlyFlag
()

Determine the current read-only flag: by default, the transaction's read-only hint.

protected Integer
getIsolationLevel()

Return the statically specified isolation level, or null if none.

public void
setIsolationLevel(int isolationLevel)

Specify the default isolation level to use for Connection retrieval, according to the JDBC java.sql.Connection constants (equivalent to the corresponding Spring org.springframework.transaction.TransactionDefinition constants).

public final void
setIsolationLevelName(String
name of the constant
constantName
)

Set the default isolation level by the name of the corresponding constant in org.springframework.transaction.TransactionDefinition — for example, "ISOLATION_SERIALIZABLE".

Inherited from org.springframework.jdbc.datasource.UserCredentialsDataSourceAdapter:
getConnectiongetConnectionremoveCredentialsFromCurrentThreadsetCatalogsetCredentialsForCurrentThreadsetPasswordsetSchemasetUsername

Field Detail

constantsback to summary
pack-priv static final Map<String, Integer> constants

Map of constant names to constant values for the isolation constants defined in TransactionDefinition.

isolationLevelback to summary
private Integer isolationLevel
Annotations
@Nullable

Constructor Detail

IsolationLevelDataSourceAdapterback to summary
public IsolationLevelDataSourceAdapter()

Method Detail

doGetConnectionback to summary
protected Connection doGetConnection(String username, String password) throws SQLException

Overrides org.springframework.jdbc.datasource.UserCredentialsDataSourceAdapter.doGetConnection.

Applies the current isolation level value and read-only flag to the returned Connection.

Parameters
username:String

Doc from org.springframework.jdbc.datasource.UserCredentialsDataSourceAdapter.doGetConnection.

the username to use

password:String

Doc from org.springframework.jdbc.datasource.UserCredentialsDataSourceAdapter.doGetConnection.

the password to use

Returns:Connection

Doc from org.springframework.jdbc.datasource.UserCredentialsDataSourceAdapter.doGetConnection.

the Connection

Annotations
@Override
See Also
getCurrentIsolationLevel(), getCurrentReadOnlyFlag()
getCurrentIsolationLevelback to summary
protected Integer getCurrentIsolationLevel()

Determine the current isolation level: either the transaction's isolation level or a statically defined isolation level.

Returns:Integer

the current isolation level, or null if none

Annotations
@Nullable
See Also
org.springframework.transaction.support.TransactionSynchronizationManager#getCurrentTransactionIsolationLevel(), setIsolationLevel
getCurrentReadOnlyFlagback to summary
protected Boolean getCurrentReadOnlyFlag()

Determine the current read-only flag: by default, the transaction's read-only hint.

Returns:Boolean

whether there is a read-only hint for the current scope

Annotations
@Nullable
See Also
org.springframework.transaction.support.TransactionSynchronizationManager#isCurrentTransactionReadOnly()
getIsolationLevelback to summary
protected Integer getIsolationLevel()

Return the statically specified isolation level, or null if none.

Annotations
@Nullable
setIsolationLevelback to summary
public void setIsolationLevel(int isolationLevel)

Specify the default isolation level to use for Connection retrieval, according to the JDBC java.sql.Connection constants (equivalent to the corresponding Spring org.springframework.transaction.TransactionDefinition constants).

If not specified, the target DataSource's default will be used. Note that a transaction-specific isolation value will always override any isolation setting specified at the DataSource level.

See Also
java.sql.Connection#TRANSACTION_READ_UNCOMMITTED, java.sql.Connection#TRANSACTION_READ_COMMITTED, java.sql.Connection#TRANSACTION_REPEATABLE_READ, java.sql.Connection#TRANSACTION_SERIALIZABLE, org.springframework.transaction.TransactionDefinition#ISOLATION_READ_UNCOMMITTED, org.springframework.transaction.TransactionDefinition#ISOLATION_READ_COMMITTED, org.springframework.transaction.TransactionDefinition#ISOLATION_REPEATABLE_READ, org.springframework.transaction.TransactionDefinition#ISOLATION_SERIALIZABLE, org.springframework.transaction.TransactionDefinition#getIsolationLevel(), org.springframework.transaction.support.TransactionSynchronizationManager#getCurrentTransactionIsolationLevel()
setIsolationLevelNameback to summary
public final void setIsolationLevelName(String constantName) throws IllegalArgumentException

Set the default isolation level by the name of the corresponding constant in org.springframework.transaction.TransactionDefinition — for example, "ISOLATION_SERIALIZABLE".

If not specified, the target DataSource's default will be used. Note that a transaction-specific isolation value will always override any isolation setting specified at the DataSource level.

Parameters
constantName:String

name of the constant

See Also
org.springframework.transaction.TransactionDefinition#ISOLATION_READ_UNCOMMITTED, org.springframework.transaction.TransactionDefinition#ISOLATION_READ_COMMITTED, org.springframework.transaction.TransactionDefinition#ISOLATION_REPEATABLE_READ, org.springframework.transaction.TransactionDefinition#ISOLATION_SERIALIZABLE, setIsolationLevel