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 SummaryFields inherited from class org.springframework.jdbc.datasource.AbstractDataSourcelogger
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionvoidInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.protected abstract ObjectDetermine 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.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(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 org.springframework.jdbc.datasource.AbstractDataSourcegetLoginTimeout, getLogWriter, getParentLogger, setLoginTimeout, setLogWriterMethods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface javax.sql.CommonDataSourcecreateShardingKeyBuilderMethods inherited from interface javax.sql.DataSourcecreateConnectionBuilder
- 
Constructor Details- 
AbstractRoutingDataSourcepublic AbstractRoutingDataSource()
 
- 
- 
Method Details- 
setTargetDataSourcesSpecify the map of target DataSources, with the lookup key as key. The mapped value can either be a correspondingDataSourceinstance 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().
- 
setDefaultTargetDataSourceSpecify 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.
- 
setLenientFallbackpublic 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.
- 
setDataSourceLookupSet 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.
- 
afterPropertiesSetpublic void afterPropertiesSet()Description copied from interface:InitializingBeanInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.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:
- afterPropertiesSetin interface- InitializingBean
 
- 
resolveSpecifiedLookupKeyResolve 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
 
- 
resolveSpecifiedDataSourceprotected 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 the- targetDataSourcesmap
- Returns:
- the resolved DataSource (never null)
- Throws:
- IllegalArgumentException- in case of an unsupported value type
 
- 
getResolvedDataSourcesReturn 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:
 
- 
getResolvedDefaultDataSourceReturn 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 interface- DataSource
- Throws:
- SQLException
 
- 
getConnection- Specified by:
- getConnectionin interface- DataSource
- Throws:
- SQLException
 
- 
unwrap- Specified by:
- unwrapin interface- Wrapper
- Overrides:
- unwrapin class- AbstractDataSource
- Throws:
- SQLException
 
- 
isWrapperFor- Specified by:
- isWrapperForin interface- Wrapper
- Overrides:
- isWrapperForin class- AbstractDataSource
- Throws:
- SQLException
 
- 
determineTargetDataSourceRetrieve the current target DataSource. Determines thecurrent lookup key, performs a lookup in thetargetDataSourcesmap, falls back to the specifieddefault target DataSourceif necessary.- See Also:
 
- 
determineCurrentLookupKeyDetermine 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.
 
-