Class IsolationLevelDataSourceAdapter
- All Implemented Interfaces:
Wrapper,CommonDataSource,DataSource,InitializingBean
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. UserCredentialsDataSourceAdapter.setCredentialsForCurrentThread(java.lang.String, java.lang.String)).
WARNING: This adapter simply calls
Connection.setTransactionIsolation(int) and/or
Connection.setReadOnly(boolean) 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.
- Since:
- 2.0.3
- Author:
- Juergen Hoeller, Sam Brannen
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected ConnectiondoGetConnection(String username, String password) Applies the current isolation level value and read-only flag to the returned Connection.protected IntegerDetermine the current isolation level: either the transaction's isolation level or a statically defined isolation level.protected BooleanDetermine the current read-only flag: by default, the transaction's read-only hint.protected IntegerReturn the statically specified isolation level, ornullif none.voidsetIsolationLevel(int isolationLevel) Specify the default isolation level to use for Connection retrieval, according to the JDBCConnectionconstants (equivalent to the corresponding SpringTransactionDefinitionconstants).final voidsetIsolationLevelName(String constantName) Set the default isolation level by the name of the corresponding constant inTransactionDefinition— for example,"ISOLATION_SERIALIZABLE".Methods inherited from class org.springframework.jdbc.datasource.UserCredentialsDataSourceAdapter
getConnection, getConnection, removeCredentialsFromCurrentThread, setCatalog, setCredentialsForCurrentThread, setPassword, setSchema, setUsernameMethods inherited from class org.springframework.jdbc.datasource.DelegatingDataSource
afterPropertiesSet, createConnectionBuilder, createShardingKeyBuilder, getLoginTimeout, getLogWriter, getParentLogger, getTargetDataSource, isWrapperFor, obtainTargetDataSource, setLoginTimeout, setLogWriter, setTargetDataSource, unwrap
-
Constructor Details
-
IsolationLevelDataSourceAdapter
public IsolationLevelDataSourceAdapter()
-
-
Method Details
-
setIsolationLevelName
Set the default isolation level by the name of the corresponding constant inTransactionDefinition— 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- name of the constant- Throws:
IllegalArgumentException- See Also:
-
setIsolationLevel
public void setIsolationLevel(int isolationLevel) Specify the default isolation level to use for Connection retrieval, according to the JDBCConnectionconstants (equivalent to the corresponding SpringTransactionDefinitionconstants).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:
-
Connection.TRANSACTION_READ_UNCOMMITTEDConnection.TRANSACTION_READ_COMMITTEDConnection.TRANSACTION_REPEATABLE_READConnection.TRANSACTION_SERIALIZABLETransactionDefinition.ISOLATION_READ_UNCOMMITTEDTransactionDefinition.ISOLATION_READ_COMMITTEDTransactionDefinition.ISOLATION_REPEATABLE_READTransactionDefinition.ISOLATION_SERIALIZABLETransactionDefinition.getIsolationLevel()TransactionSynchronizationManager.getCurrentTransactionIsolationLevel()
-
getIsolationLevel
Return the statically specified isolation level, ornullif none. -
doGetConnection
protected Connection doGetConnection(@Nullable String username, @Nullable String password) throws SQLException Applies the current isolation level value and read-only flag to the returned Connection.- Overrides:
doGetConnectionin classUserCredentialsDataSourceAdapter- Parameters:
username- the username to usepassword- the password to use- Returns:
- the Connection
- Throws:
SQLException- See Also:
-
getCurrentIsolationLevel
Determine the current isolation level: either the transaction's isolation level or a statically defined isolation level.- Returns:
- the current isolation level, or
nullif none - See Also:
-
getCurrentReadOnlyFlag
Determine the current read-only flag: by default, the transaction's read-only hint.- Returns:
- whether there is a read-only hint for the current scope
- See Also:
-