public abstract class

AbstractRoutingDataSource

extends AbstractDataSource
implements InitializingBean
java.lang.Object
   ↳ org.springframework.jdbc.datasource.AbstractDataSource
     ↳ org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource
Known Direct Subclasses

Class Overview

Abstract javax.sql.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.

Summary

[Expand]
Inherited Fields
From class org.springframework.jdbc.datasource.AbstractDataSource
Public Constructors
AbstractRoutingDataSource()
Public Methods
void afterPropertiesSet()
Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).
Connection getConnection()
Connection getConnection(String username, String password)
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<ObjectObject> targetDataSources)
Specify the map of target DataSources, with the lookup key as key.
Protected Methods
abstract Object determineCurrentLookupKey()
Determine the current lookup key.
DataSource determineTargetDataSource()
Retrieve the current target DataSource.
DataSource resolveSpecifiedDataSource(Object dataSource)
Resolve the specified data source object into a DataSource instance.
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.
[Expand]
Inherited Methods
From class org.springframework.jdbc.datasource.AbstractDataSource
From class java.lang.Object
From interface java.sql.Wrapper
From interface javax.sql.CommonDataSource
From interface javax.sql.DataSource
From interface org.springframework.beans.factory.InitializingBean

Public Constructors

public AbstractRoutingDataSource ()

Public Methods

public void afterPropertiesSet ()

Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).

This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.

public Connection getConnection ()

Throws
SQLException

public Connection getConnection (String username, String password)

Throws
SQLException

public void setDataSourceLookup (DataSourceLookup dataSourceLookup)

Set the DataSourceLookup implementation to use for resolving data source name Strings in the targetDataSources map.

Default is a JndiDataSourceLookup, allowing the JNDI names of application server DataSources to be specified directly.

public void setDefaultTargetDataSource (Object defaultTargetDataSource)

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 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)

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.

public void setTargetDataSources (Map<ObjectObject> targetDataSources)

Specify the map of target DataSources, with the lookup key as key. 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().

Protected Methods

protected abstract Object 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.

protected DataSource determineTargetDataSource ()

Retrieve the current target DataSource. Determines the current lookup key, performs a lookup in the targetDataSources map, falls back to the specified default target DataSource if necessary.

protected DataSource resolveSpecifiedDataSource (Object dataSource)

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 the targetDataSources map
Returns
  • the resolved DataSource (never null)
Throws
IllegalArgumentException in case of an unsupported value type

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.

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