Class AbstractRoutingDataSource
- All Implemented Interfaces:
Wrapper
,CommonDataSource
,DataSource
,InitializingBean
- Direct Known Subclasses:
IsolationLevelDataSourceRouter
DataSource
implementation that routes getConnection()
calls to one of various target DataSources based on a lookup key. The latter is usually
(but not necessarily) determined through some thread-bound transaction context.- Since:
- 2.0.1
- Author:
- Juergen Hoeller
- See Also:
-
Field Summary
Fields inherited from class org.springframework.jdbc.datasource.AbstractDataSource
logger
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.protected abstract Object
Determine the current lookup key.protected DataSource
Retrieve the current target DataSource.getConnection
(String username, String password) Return the resolved target DataSources that this router manages.Return the resolved default target DataSource, if any.boolean
isWrapperFor
(Class<?> iface) protected DataSource
resolveSpecifiedDataSource
(Object dataSource) Resolve the specified data source object into a DataSource instance.protected Object
resolveSpecifiedLookupKey
(Object lookupKey) Resolve the given lookup key object, as specified in thetargetDataSources
map, into the actual lookup key to be used for matching with thecurrent lookup key
.void
setDataSourceLookup
(DataSourceLookup dataSourceLookup) Set the DataSourceLookup implementation to use for resolving data source name Strings in thetargetDataSources
map.void
setDefaultTargetDataSource
(Object defaultTargetDataSource) Specify the default target DataSource, if any.void
setLenientFallback
(boolean lenientFallback) Specify whether to apply a lenient fallback to the default DataSource if no specific DataSource could be found for the current lookup key.void
setTargetDataSources
(Map<Object, Object> targetDataSources) Specify the map of target DataSources, with the lookup key as key.<T> T
Methods inherited from class org.springframework.jdbc.datasource.AbstractDataSource
getLoginTimeout, getLogWriter, getParentLogger, setLoginTimeout, setLogWriter
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface javax.sql.CommonDataSource
createShardingKeyBuilder
Methods inherited from interface javax.sql.DataSource
createConnectionBuilder
-
Constructor Details
-
AbstractRoutingDataSource
public AbstractRoutingDataSource()
-
-
Method Details
-
setTargetDataSources
Specify the map of target DataSources, with the lookup key as key. The mapped value can either be a correspondingDataSource
instance or a data source name String (to be resolved via aDataSourceLookup
).The key can be of arbitrary type; this class implements the generic lookup process only. The concrete key representation will be handled by
resolveSpecifiedLookupKey(Object)
anddetermineCurrentLookupKey()
. -
setDefaultTargetDataSource
Specify the default target DataSource, if any.The mapped value can either be a corresponding
DataSource
instance or a data source name String (to be resolved via aDataSourceLookup
).This DataSource will be used as target if none of the keyed
targetDataSources
match thedetermineCurrentLookupKey()
current lookup key. -
setLenientFallback
public void setLenientFallback(boolean lenientFallback) Specify whether to apply a lenient fallback to the default DataSource if no specific DataSource could be found for the current lookup key.Default is "true", accepting lookup keys without a corresponding entry in the target DataSource map - simply falling back to the default DataSource in that case.
Switch this flag to "false" if you would prefer the fallback to only apply if the lookup key was
null
. Lookup keys without a DataSource entry will then lead to an IllegalStateException. -
setDataSourceLookup
Set the DataSourceLookup implementation to use for resolving data source name Strings in thetargetDataSources
map.Default is a
JndiDataSourceLookup
, allowing the JNDI names of application server DataSources to be specified directly. -
afterPropertiesSet
public void afterPropertiesSet()Description copied from interface:InitializingBean
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
- Specified by:
afterPropertiesSet
in interfaceInitializingBean
-
resolveSpecifiedLookupKey
Resolve the given lookup key object, as specified in thetargetDataSources
map, into the actual lookup key to be used for matching with thecurrent lookup key
.The default implementation simply returns the given key as-is.
- Parameters:
lookupKey
- the lookup key object as specified by the user- Returns:
- the lookup key as needed for matching
-
resolveSpecifiedDataSource
Resolve the specified data source object into a DataSource instance.The default implementation handles DataSource instances and data source names (to be resolved via a
DataSourceLookup
).- Parameters:
dataSource
- the data source value object as specified in thetargetDataSources
map- Returns:
- the resolved DataSource (never
null
) - Throws:
IllegalArgumentException
- in case of an unsupported value type
-
getResolvedDataSources
Return the resolved target DataSources that this router manages.- Returns:
- an unmodifiable map of resolved lookup keys and DataSources
- Throws:
IllegalStateException
- if the target DataSources are not resolved yet- Since:
- 5.2.9
- See Also:
-
getResolvedDefaultDataSource
Return the resolved default target DataSource, if any.- Returns:
- the default DataSource, or
null
if none or not resolved yet - Since:
- 5.2.9
- See Also:
-
getConnection
- Specified by:
getConnection
in interfaceDataSource
- Throws:
SQLException
-
getConnection
- Specified by:
getConnection
in interfaceDataSource
- Throws:
SQLException
-
unwrap
- Specified by:
unwrap
in interfaceWrapper
- Overrides:
unwrap
in classAbstractDataSource
- Throws:
SQLException
-
isWrapperFor
- Specified by:
isWrapperFor
in interfaceWrapper
- Overrides:
isWrapperFor
in classAbstractDataSource
- Throws:
SQLException
-
determineTargetDataSource
Retrieve the current target DataSource. Determines thecurrent lookup key
, performs a lookup in thetargetDataSources
map, falls back to the specifieddefault target DataSource
if necessary.- See Also:
-
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(java.lang.Object)
method.
-