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

public Class IsolationLevelDataSourceRouter

extends AbstractRoutingDataSource
Class Inheritance
Imports
java.util.Map, org.springframework.lang.Nullable, org.springframework.transaction.TransactionDefinition, org.springframework.transaction.support.TransactionSynchronizationManager, org.springframework.util.Assert

DataSource that routes to one of various target DataSources based on the current transaction isolation level. The target DataSources need to be configured with the isolation level name as key, as defined on the TransactionDefinition interface.

This is particularly useful in combination with JTA transaction management (typically through Spring's org.springframework.transaction.jta.JtaTransactionManager). Standard JTA does not support transaction-specific isolation levels. Some JTA providers support isolation levels as a vendor-specific extension (e.g. WebLogic), which is the preferred way of addressing this. As an alternative (e.g. on WebSphere), the target database can be represented through multiple JNDI DataSources, each configured with a different isolation level (for the entire DataSource). IsolationLevelDataSourceRouter allows to transparently switch to the appropriate DataSource based on the current transaction's isolation level.

For example, the configuration can look like the following, assuming that the target DataSources are defined as individual Spring beans with names "myRepeatableReadDataSource", "mySerializableDataSource", and "myDefaultDataSource":

<bean id="dataSourceRouter" class="org.springframework.jdbc.datasource.lookup.IsolationLevelDataSourceRouter">
  <property name="targetDataSources">
    <map>
      <entry key="ISOLATION_REPEATABLE_READ" value-ref="myRepeatableReadDataSource"/>
      <entry key="ISOLATION_SERIALIZABLE" value-ref="mySerializableDataSource"/>
    </map>
  </property>
  <property name="defaultTargetDataSource" ref="myDefaultDataSource"/>
</bean>
Alternatively, the keyed values can also be data source names, to be resolved through a DataSourceLookup: by default, JNDI names for a standard JNDI lookup. This allows for a single concise definition without the need for separate DataSource bean definitions.
<bean id="dataSourceRouter" class="org.springframework.jdbc.datasource.lookup.IsolationLevelDataSourceRouter">
  <property name="targetDataSources">
    <map>
      <entry key="ISOLATION_REPEATABLE_READ" value="java:comp/env/jdbc/myrrds"/>
      <entry key="ISOLATION_SERIALIZABLE" value="java:comp/env/jdbc/myserds"/>
    </map>
  </property>
  <property name="defaultTargetDataSource" value="java:comp/env/jdbc/mydefds"/>
</bean>
Note: If you are using this router in combination with Spring's org.springframework.transaction.jta.JtaTransactionManager, don't forget to switch the "allowCustomIsolationLevels" flag to "true". (By default, JtaTransactionManager will only accept a default isolation level because of the lack of isolation level support in standard JTA itself.)
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
  <property name="allowCustomIsolationLevels" value="true"/>
</bean>
Authors
Juergen Hoeller, Sam Brannen
Since
2.0.1
See Also
setTargetDataSources, setDefaultTargetDataSource, 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.jta.JtaTransactionManager

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.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected Object
protected Object
resolveSpecifiedLookupKey(Object
the lookup key object as specified by the user
lookupKey
)

Overrides org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource.resolveSpecifiedLookupKey.

Supports Integer values for the isolation level constants as well as isolation level names as defined on the TransactionDefinition interface.

Inherited from org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource:
afterPropertiesSetcreateConnectionBuildercreateShardingKeyBuilderdetermineTargetDataSourcegetConnectiongetConnectiongetResolvedDataSourcesgetResolvedDefaultDataSourceinitializeisWrapperForresolveSpecifiedDataSourcesetDataSourceLookupsetDefaultTargetDataSourcesetLenientFallbacksetTargetDataSourcesunwrap

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.

Constructor Detail

IsolationLevelDataSourceRouterback to summary
public IsolationLevelDataSourceRouter()

Method Detail

determineCurrentLookupKeyback to summary
protected Object determineCurrentLookupKey()

Implements abstract org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource.determineCurrentLookupKey.

Doc from org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource.determineCurrentLookupKey.

Determine the current lookup key. This will typically be implemented to check a thread-bound transaction context.

Allows for arbitrary keys. The returned key needs to match the stored lookup key type, as resolved by the resolveSpecifiedLookupKey method.

Annotations
@Override
@Nullable
resolveSpecifiedLookupKeyback to summary
protected Object resolveSpecifiedLookupKey(Object lookupKey)

Overrides org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource.resolveSpecifiedLookupKey.

Supports Integer values for the isolation level constants as well as isolation level names as defined on the TransactionDefinition interface.

Parameters
lookupKey:Object

Doc from org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource.resolveSpecifiedLookupKey.

the lookup key object as specified by the user

Returns:Object

Doc from org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource.resolveSpecifiedLookupKey.

the lookup key as needed for matching

Annotations
@Override