Top Description Fields Constructors Methods
org.springframework.jdbc.datasource.lookup

public abstract Class AbstractRoutingDataSource

extends AbstractDataSource
implements InitializingBean
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.InitializingBean
Known Direct Subclasses
org.springframework.jdbc.datasource.lookup.IsolationLevelDataSourceRouter
Imports
java.sql.Connection, .ConnectionBuilder, .SQLException, .ShardingKeyBuilder, java.util.Collections, .Map, javax.sql.DataSource, org.springframework.beans.factory.InitializingBean, org.springframework.jdbc.datasource.AbstractDataSource, org.springframework.lang.Nullable, org.springframework.util.Assert, .CollectionUtils

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.
Author
Juergen Hoeller
Since
2.0.1
See Also
setTargetDataSources, setDefaultTargetDataSource, determineCurrentLookupKey()

Field Summary

Modifier and TypeField and Description
private DataSourceLookup
private Object
private boolean
private Map<Object, DataSource>
private DataSource
private Map<Object, Object>
Inherited from org.springframework.jdbc.datasource.AbstractDataSource:
logger

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
public ConnectionBuilder
createConnectionBuilder()

Overrides default javax.sql.DataSource.createConnectionBuilder.

Create a new ConnectionBuilder instance

public ShardingKeyBuilder
createShardingKeyBuilder()

Overrides default javax.sql.CommonDataSource.createShardingKeyBuilder.

Creates a new ShardingKeyBuilder instance

protected abstract Object
determineCurrentLookupKey()

Determine the current lookup key.

protected DataSource
determineTargetDataSource()

Retrieve the current target DataSource.

public Connection
getConnection()

Implements javax.sql.DataSource.getConnection.

Attempts to establish a connection with the data source that this DataSource object represents.

public Connection
getConnection(String
the database user on whose behalf the connection is being made
username
,
String
the user's password
password
)

Implements javax.sql.DataSource.getConnection.

Attempts to establish a connection with the data source that this DataSource object represents.

public Map<Object, DataSource>

Returns:

an unmodifiable map of resolved lookup keys and DataSources
getResolvedDataSources
()

Return the resolved target DataSources that this router manages.

public DataSource

Returns:

the default DataSource, or null if none or not resolved yet
getResolvedDefaultDataSource
()

Return the resolved default target DataSource, if any.

public void
initialize()

Initialize the internal state of this AbstractRoutingDataSource by resolving the configured target DataSources.

public boolean
isWrapperFor(Class<?>
a Class defining an interface.
iface
)

Overrides org.springframework.jdbc.datasource.AbstractDataSource.isWrapperFor.

Implements java.sql.Wrapper.isWrapperFor.

Returns true if this either implements the interface argument or is directly or indirectly a wrapper for an object that does.

protected DataSource

Returns:

the resolved DataSource (never null)
resolveSpecifiedDataSource
(Object
the data source value object as specified in the targetDataSources map
dataSourceObject
)

Resolve the specified data source object into a DataSource instance.

protected Object

Returns:

the lookup key as needed for matching
resolveSpecifiedLookupKey
(Object
the lookup key object as specified by the user
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.

public void
setDataSourceLookup(DataSourceLookup dataSourceLookup)

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

public void
setDefaultTargetDataSource(Object defaultTargetDataSource)

Specify the default target DataSource, if any.

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.

public void
setTargetDataSources(Map<Object, Object> targetDataSources)

Specify the map of target DataSources, with the lookup key as key.

public <T> T
unwrap(Class<T>
A Class defining an interface that the result must implement.
iface
)

Overrides org.springframework.jdbc.datasource.AbstractDataSource.unwrap.

Implements java.sql.Wrapper.unwrap.

Returns an object that implements the given interface to allow access to non-standard methods, or standard methods not exposed by the proxy.

Inherited from org.springframework.jdbc.datasource.AbstractDataSource:
getLoginTimeoutgetLogWritergetParentLoggersetLoginTimeoutsetLogWriter

Field Detail

dataSourceLookupback to summary
private DataSourceLookup dataSourceLookup
defaultTargetDataSourceback to summary
private Object defaultTargetDataSource
Annotations
@Nullable
lenientFallbackback to summary
private boolean lenientFallback
resolvedDataSourcesback to summary
private Map<Object, DataSource> resolvedDataSources
Annotations
@Nullable
resolvedDefaultDataSourceback to summary
private DataSource resolvedDefaultDataSource
Annotations
@Nullable
targetDataSourcesback to summary
private Map<Object, Object> targetDataSources
Annotations
@Nullable

Constructor Detail

AbstractRoutingDataSourceback to summary
public AbstractRoutingDataSource()

Method Detail

afterPropertiesSetback to summary
public void afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Delegates to initialize().

Annotations
@Override
createConnectionBuilderback to summary
public ConnectionBuilder createConnectionBuilder() throws SQLException

Overrides default javax.sql.DataSource.createConnectionBuilder.

Doc from javax.sql.DataSource.createConnectionBuilder.

Create a new ConnectionBuilder instance

Returns:ConnectionBuilder

The ConnectionBuilder instance that was created

Annotations
@Override
Exceptions
SQLException:
if an error occurs creating the builder
createShardingKeyBuilderback to summary
public ShardingKeyBuilder createShardingKeyBuilder() throws SQLException

Overrides default javax.sql.CommonDataSource.createShardingKeyBuilder.

Doc from javax.sql.CommonDataSource.createShardingKeyBuilder.

Creates a new ShardingKeyBuilder instance

Returns:ShardingKeyBuilder

The ShardingKeyBuilder instance that was created

Annotations
@Override
Exceptions
SQLException:
if an error occurs creating the builder
determineCurrentLookupKeyback to summary
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 method.

Annotations
@Nullable
determineTargetDataSourceback to summary
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.

See Also
determineCurrentLookupKey()
getConnectionback to summary
public Connection getConnection() throws SQLException

Implements javax.sql.DataSource.getConnection.

Doc from javax.sql.DataSource.getConnection.

Attempts to establish a connection with the data source that this DataSource object represents.

Returns:Connection

a connection to the data source

Annotations
@Override
Exceptions
SQLException:
if a database access error occurs
getConnectionback to summary
public Connection getConnection(String username, String password) throws SQLException

Implements javax.sql.DataSource.getConnection.

Doc from javax.sql.DataSource.getConnection.

Attempts to establish a connection with the data source that this DataSource object represents.

Parameters
username:String

the database user on whose behalf the connection is being made

password:String

the user's password

Returns:Connection

a connection to the data source

Annotations
@Override
Exceptions
SQLException:
if a database access error occurs
getResolvedDataSourcesback to summary
public Map<Object, DataSource> getResolvedDataSources()

Return the resolved target DataSources that this router manages.

Returns:Map<Object, DataSource>

an unmodifiable map of resolved lookup keys and DataSources

Exceptions
IllegalStateException:
if the target DataSources are not resolved yet
Since
5.2.9
See Also
setTargetDataSources
getResolvedDefaultDataSourceback to summary
public DataSource getResolvedDefaultDataSource()

Return the resolved default target DataSource, if any.

Returns:DataSource

the default DataSource, or null if none or not resolved yet

Annotations
@Nullable
Since
5.2.9
See Also
setDefaultTargetDataSource
initializeback to summary
public void initialize()

Initialize the internal state of this AbstractRoutingDataSource by resolving the configured target DataSources.

Exceptions
IllegalArgumentException:
if the target DataSources have not been configured
Since
6.1
See Also
setTargetDataSources(Map), setDefaultTargetDataSource(Object), getResolvedDataSources(), getResolvedDefaultDataSource()
isWrapperForback to summary
public boolean isWrapperFor(Class<?> iface) throws SQLException

Overrides org.springframework.jdbc.datasource.AbstractDataSource.isWrapperFor.

Implements java.sql.Wrapper.isWrapperFor.

Doc from java.sql.Wrapper.isWrapperFor.

Returns true if this either implements the interface argument or is directly or indirectly a wrapper for an object that does. Returns false otherwise. If this implements the interface then return true, else if this is a wrapper then return the result of recursively calling isWrapperFor on the wrapped object. If this does not implement the interface and is not a wrapper, return false. This method should be implemented as a low-cost operation compared to unwrap so that callers can use this method to avoid expensive unwrap calls that may fail. If this method returns true then calling unwrap with the same argument should succeed.

Parameters
iface:Class<?>

a Class defining an interface.

Returns:boolean

true if this implements the interface or directly or indirectly wraps an object that does.

Annotations
@Override
Exceptions
SQLException:
if an error occurs while determining whether this is a wrapper for an object with the given interface.
resolveSpecifiedDataSourceback to summary
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:Object

the data source value object as specified in the targetDataSources map

Returns:DataSource

the resolved DataSource (never null)

Exceptions
IllegalArgumentException:
in case of an unsupported value type
resolveSpecifiedLookupKeyback to summary
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:Object

the lookup key object as specified by the user

Returns:Object

the lookup key as needed for matching

setDataSourceLookupback to summary
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.

setDefaultTargetDataSourceback to summary
public void setDefaultTargetDataSource(Object defaultTargetDataSource)

Specify the default target DataSource, if any.

The mapped value can either be a corresponding javax.sql.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.

setLenientFallbackback to summary
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
setTargetDataSources, setDefaultTargetDataSource, determineCurrentLookupKey()
setTargetDataSourcesback to summary
public void setTargetDataSources(Map<Object, Object> targetDataSources)

Specify the map of target DataSources, with the lookup key as key.

The mapped value can either be a corresponding javax.sql.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().

unwrapback to summary
public <T> T unwrap(Class<T> iface) throws SQLException

Overrides org.springframework.jdbc.datasource.AbstractDataSource.unwrap.

Implements java.sql.Wrapper.unwrap.

Doc from java.sql.Wrapper.unwrap.

Returns an object that implements the given interface to allow access to non-standard methods, or standard methods not exposed by the proxy. If the receiver implements the interface then the result is the receiver or a proxy for the receiver. If the receiver is a wrapper and the wrapped object implements the interface then the result is the wrapped object or a proxy for the wrapped object. Otherwise return the the result of calling unwrap recursively on the wrapped object or a proxy for that result. If the receiver is not a wrapper and does not implement the interface, then an SQLException is thrown.

Parameters
<T>
the type of the class modeled by this Class object
iface:Class<T>

A Class defining an interface that the result must implement.

Returns:T

an object that implements the interface. May be a proxy for the actual implementing object.

Annotations
@Override
@SuppressWarnings:unchecked
Exceptions
SQLException:
If no object found that implements the interface