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 AbstractDataSource
logger -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidDelegates toinitialize().Determine the current lookup key.protected DataSourceRetrieve 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.voidInitialize the internal state of thisAbstractRoutingDataSourceby resolving the configured target DataSources.booleanisWrapperFor(Class<?> iface) protected DataSourceresolveSpecifiedDataSource(Object dataSourceObject) Resolve the specified data source object into a DataSource instance.protected ObjectresolveSpecifiedLookupKey(Object lookupKey) Resolve the given lookup key object, as specified in thetargetDataSourcesmap, into the actual lookup key to be used for matching with thecurrent lookup key.voidsetDataSourceLookup(@Nullable DataSourceLookup dataSourceLookup) Set the DataSourceLookup implementation to use for resolving data source name Strings in thetargetDataSourcesmap.voidsetDefaultTargetDataSource(Object defaultTargetDataSource) Specify the default target DataSource, if any.voidsetLenientFallback(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.voidsetTargetDataSources(Map<Object, Object> targetDataSources) Specify the map of target DataSources, with the lookup key as key.<T> TMethods inherited from class AbstractDataSource
getLoginTimeout, getLogWriter, getParentLogger, setLoginTimeout, setLogWriter
-
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 corresponding
DataSourceinstance 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
DataSourceinstance or a data source name String (to be resolved via aDataSourceLookup).This DataSource will be used as target if none of the keyed
targetDataSourcesmatch 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.- See Also:
-
setDataSourceLookup
Set the DataSourceLookup implementation to use for resolving data source name Strings in thetargetDataSourcesmap.Default is a
JndiDataSourceLookup, allowing the JNDI names of application server DataSources to be specified directly. -
afterPropertiesSet
public void afterPropertiesSet()Delegates toinitialize().- Specified by:
afterPropertiesSetin interfaceInitializingBean
-
initialize
public void initialize()Initialize the internal state of thisAbstractRoutingDataSourceby resolving the configured target DataSources.- Throws:
IllegalArgumentException- if the target DataSources have not been configured- Since:
- 6.1
- See Also:
-
resolveSpecifiedLookupKey
Resolve the given lookup key object, as specified in thetargetDataSourcesmap, 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
protected DataSource resolveSpecifiedDataSource(Object dataSourceObject) throws IllegalArgumentException 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:
dataSourceObject- the data source value object as specified in thetargetDataSourcesmap- 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
nullif none or not resolved yet - Since:
- 5.2.9
- See Also:
-
getConnection
- Specified by:
getConnectionin interfaceDataSource- Throws:
SQLException
-
getConnection
- Specified by:
getConnectionin interfaceDataSource- Throws:
SQLException
-
createConnectionBuilder
- Specified by:
createConnectionBuilderin interfaceDataSource- Throws:
SQLException
-
createShardingKeyBuilder
- Specified by:
createShardingKeyBuilderin interfaceCommonDataSource- Throws:
SQLException
-
unwrap
- Specified by:
unwrapin interfaceWrapper- Overrides:
unwrapin classAbstractDataSource- Throws:
SQLException
-
isWrapperFor
- Specified by:
isWrapperForin interfaceWrapper- Overrides:
isWrapperForin classAbstractDataSource- Throws:
SQLException
-
determineTargetDataSource
Retrieve the current target DataSource. Determines thecurrent lookup key, performs a lookup in thetargetDataSourcesmap, falls back to the specifieddefault target DataSourceif 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(Object)method.
-