public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean
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.setTargetDataSources(java.util.Map<java.lang.Object, java.lang.Object>)
,
setDefaultTargetDataSource(java.lang.Object)
,
determineCurrentLookupKey()
logger
Constructor and Description |
---|
AbstractRoutingDataSource() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Invoked by the containing
BeanFactory after it has set all bean properties
and satisfied BeanFactoryAware , ApplicationContextAware etc. |
protected abstract Object |
determineCurrentLookupKey()
Determine the current lookup key.
|
protected DataSource |
determineTargetDataSource()
Retrieve the current target DataSource.
|
Connection |
getConnection() |
Connection |
getConnection(String username,
String password) |
Map<Object,DataSource> |
getResolvedDataSources()
Return the resolved target DataSources that this router manages.
|
DataSource |
getResolvedDefaultDataSource()
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 the
targetDataSources map, into
the actual lookup key to be used for matching with the
current lookup key . |
void |
setDataSourceLookup(DataSourceLookup dataSourceLookup)
Set the DataSourceLookup implementation to use for resolving data source
name Strings in the
targetDataSources 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 |
unwrap(Class<T> iface) |
getLoginTimeout, getLogWriter, getParentLogger, setLoginTimeout, setLogWriter
public void setTargetDataSources(Map<Object,Object> targetDataSources)
The mapped value can either be a corresponding DataSource
instance or a data source name String (to be resolved via a
DataSourceLookup
).
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)
and
determineCurrentLookupKey()
.
public void setDefaultTargetDataSource(Object defaultTargetDataSource)
The mapped value can either be a corresponding DataSource
instance or a data source name String (to be resolved via a
DataSourceLookup
).
This DataSource will be used as target if none of the keyed
targetDataSources
match the
determineCurrentLookupKey()
current lookup key.
public void setLenientFallback(boolean lenientFallback)
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.
public void setDataSourceLookup(@Nullable DataSourceLookup dataSourceLookup)
targetDataSources
map.
Default is a JndiDataSourceLookup
, allowing the JNDI names
of application server DataSources to be specified directly.
public void afterPropertiesSet()
InitializingBean
BeanFactory
after it has set all bean properties
and satisfied BeanFactoryAware
, 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.
afterPropertiesSet
in interface InitializingBean
protected Object resolveSpecifiedLookupKey(Object lookupKey)
targetDataSources
map, into
the actual lookup key to be used for matching with the
current lookup key
.
The default implementation simply returns the given key as-is.
lookupKey
- the lookup key object as specified by the userprotected DataSource resolveSpecifiedDataSource(Object dataSource) throws IllegalArgumentException
The default implementation handles DataSource instances and data source
names (to be resolved via a DataSourceLookup
).
dataSource
- the data source value object as specified in the
targetDataSources
mapnull
)IllegalArgumentException
- in case of an unsupported value typepublic Map<Object,DataSource> getResolvedDataSources()
IllegalStateException
- if the target DataSources are not resolved yetsetTargetDataSources(java.util.Map<java.lang.Object, java.lang.Object>)
@Nullable public DataSource getResolvedDefaultDataSource()
null
if none or not resolved yetsetDefaultTargetDataSource(java.lang.Object)
public Connection getConnection() throws SQLException
getConnection
in interface DataSource
SQLException
public Connection getConnection(String username, String password) throws SQLException
getConnection
in interface DataSource
SQLException
public <T> T unwrap(Class<T> iface) throws SQLException
unwrap
in interface Wrapper
unwrap
in class AbstractDataSource
SQLException
public boolean isWrapperFor(Class<?> iface) throws SQLException
isWrapperFor
in interface Wrapper
isWrapperFor
in class AbstractDataSource
SQLException
protected DataSource determineTargetDataSource()
current lookup key
, performs
a lookup in the targetDataSources
map,
falls back to the specified
default target DataSource
if necessary.determineCurrentLookupKey()
@Nullable protected abstract Object determineCurrentLookupKey()
Allows for arbitrary keys. The returned key needs
to match the stored lookup key type, as resolved by the
resolveSpecifiedLookupKey(java.lang.Object)
method.