类 IsolationLevelDataSourceRouter
- java.lang.Object
- org.springframework.jdbc.datasource.AbstractDataSource
- org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource
- org.springframework.jdbc.datasource.lookup.IsolationLevelDataSourceRouter
- 所有已实现的接口:
Wrapper
,CommonDataSource
,DataSource
,InitializingBean
public class IsolationLevelDataSourceRouter extends AbstractRoutingDataSource
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 theTransactionDefinition interface
.This is particularly useful in combination with JTA transaction management (typically through Spring's
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 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). The present DataSource router allows to transparently switch to the appropriate DataSource based on the current transaction's isolation level.The configuration can for example look like this, 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 aDataSourceLookup
: 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'sJtaTransactionManager
, 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>
- 从以下版本开始:
- 2.0.1
- 作者:
- Juergen Hoeller
- 另请参阅:
AbstractRoutingDataSource.setTargetDataSources(java.util.Map<java.lang.Object, java.lang.Object>)
,AbstractRoutingDataSource.setDefaultTargetDataSource(java.lang.Object)
,TransactionDefinition.ISOLATION_READ_UNCOMMITTED
,TransactionDefinition.ISOLATION_READ_COMMITTED
,TransactionDefinition.ISOLATION_REPEATABLE_READ
,TransactionDefinition.ISOLATION_SERIALIZABLE
,JtaTransactionManager
字段概要
从类继承的字段 org.springframework.jdbc.datasource.AbstractDataSource
logger
构造器概要
构造器 构造器 说明 IsolationLevelDataSourceRouter()
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 protected Object
determineCurrentLookupKey()
Determine the current lookup key.protected Object
resolveSpecifiedLookupKey(Object lookupKey)
Supports Integer values for the isolation level constants as well as isolation level names as defined on theTransactionDefinition interface
.从类继承的方法 org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource
afterPropertiesSet, determineTargetDataSource, getConnection, getConnection, isWrapperFor, resolveSpecifiedDataSource, setDataSourceLookup, setDefaultTargetDataSource, setLenientFallback, setTargetDataSources, unwrap
从类继承的方法 org.springframework.jdbc.datasource.AbstractDataSource
getLoginTimeout, getLogWriter, getParentLogger, setLoginTimeout, setLogWriter
从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
从接口继承的方法 javax.sql.CommonDataSource
createShardingKeyBuilder
从接口继承的方法 javax.sql.DataSource
createConnectionBuilder
构造器详细资料
IsolationLevelDataSourceRouter
public IsolationLevelDataSourceRouter()
方法详细资料
resolveSpecifiedLookupKey
protected Object resolveSpecifiedLookupKey(Object lookupKey)
Supports Integer values for the isolation level constants as well as isolation level names as defined on theTransactionDefinition interface
.- 覆盖:
resolveSpecifiedLookupKey
在类中AbstractRoutingDataSource
- 参数:
lookupKey
- the lookup key object as specified by the user- 返回:
- the lookup key as needed for matching
determineCurrentLookupKey
protected Object determineCurrentLookupKey()
从类复制的说明:AbstractRoutingDataSource
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
AbstractRoutingDataSource.resolveSpecifiedLookupKey(java.lang.Object)
method.