public class RoutingDataSource extends org.springframework.jdbc.datasource.AbstractDataSource implements DataSource, org.springframework.beans.factory.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.concurrent.ConcurrentHashMap<java.lang.Object, org.openingo.spring.boot.extension.datasource.provider.IDataSourceProvider>),
setDefaultTargetDataSource(org.openingo.spring.boot.extension.datasource.provider.IDataSourceProvider),
determineCurrentLookupKey()| Constructor and Description |
|---|
RoutingDataSource() |
RoutingDataSource(IDataSourceProvider defaultTargetDataSource) |
RoutingDataSource(IDataSourceProvider defaultTargetDataSource,
ConcurrentHashMap<Object,IDataSourceProvider> targetDataSources) |
| Modifier and Type | Method and Description |
|---|---|
void |
addDataSource(Object dataSourceKey,
IDataSourceProvider dataSource)
add a new dataSource
|
void |
addDataSources(Map<Object,IDataSourceProvider> dataSources)
add more dataSources
|
void |
afterPropertiesSet() |
protected Object |
determineCurrentLookupKey()
Determine the current lookup key.
|
protected DataSource |
determineTargetDataSource()
Retrieve the current target DataSource.
|
Connection |
getConnection() |
Connection |
getConnection(String username,
String password) |
IDataSourceProvider |
getCurrentUsingDataSourceProvider()
Returns current using dataSource provider instance
default is defaultTargetDataSource
|
Boolean |
hasDataSource(Object dataSourceKey)
the dataSource has the
dataSourceKey is exists or not. |
boolean |
isWrapperFor(Class<?> iface) |
void |
preDestroy() |
void |
removeDataSource(Object dataSourceKey)
remove the dataSource with the
dataSourceKey |
void |
setAutoCloseSameKeyDataSource(Boolean autoCloseSameKeyDataSource)
whether or not close the dataSource that has the same dataSource key
|
void |
setDefaultTargetDataSource(IDataSourceProvider defaultTargetDataSource)
Specify the default target DataSource, if any.
|
void |
setTargetDataSources(ConcurrentHashMap<Object,IDataSourceProvider> targetDataSources)
Specify the map of target DataSources, with the lookup key as key.
|
<T> T |
unwrap(Class<T> iface) |
getLoginTimeout, getLogWriter, getParentLogger, setLoginTimeout, setLogWriterclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitgetLoginTimeout, getLogWriter, getParentLogger, setLoginTimeout, setLogWriterpublic RoutingDataSource()
public RoutingDataSource(IDataSourceProvider defaultTargetDataSource)
public RoutingDataSource(IDataSourceProvider defaultTargetDataSource, ConcurrentHashMap<Object,IDataSourceProvider> targetDataSources)
public void setAutoCloseSameKeyDataSource(Boolean autoCloseSameKeyDataSource)
autoCloseSameKeyDataSource - true close the dataSource that has the same dataSourceKeypublic Boolean hasDataSource(Object dataSourceKey)
dataSourceKey is exists or not.dataSourceKey - dataSource keypublic void addDataSource(Object dataSourceKey, IDataSourceProvider dataSource)
dataSourceKey - dataSource keydataSource - dataSource Providerpublic void addDataSources(Map<Object,IDataSourceProvider> dataSources)
dataSources - dataSource provider mappingpublic void removeDataSource(Object dataSourceKey)
dataSourceKeydataSourceKey - dataSource key dataSource key@PreDestroy public void preDestroy()
public void setTargetDataSources(ConcurrentHashMap<Object,IDataSourceProvider> targetDataSources)
DataSource
instance.
determineCurrentLookupKey().public void setDefaultTargetDataSource(IDataSourceProvider defaultTargetDataSource)
The mapped value can either be a corresponding DataSource
instance.
determineCurrentLookupKey() current lookup key.
public void afterPropertiesSet()
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBeanpublic Connection getConnection() throws SQLException
getConnection in interface DataSourceSQLExceptionpublic Connection getConnection(String username, String password) throws SQLException
getConnection in interface DataSourceSQLExceptionpublic <T> T unwrap(Class<T> iface) throws SQLException
unwrap in interface Wrapperunwrap in class org.springframework.jdbc.datasource.AbstractDataSourceSQLExceptionpublic boolean isWrapperFor(Class<?> iface) throws SQLException
isWrapperFor in interface WrapperisWrapperFor in class org.springframework.jdbc.datasource.AbstractDataSourceSQLExceptionprotected 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 Object determineCurrentLookupKey()
public IDataSourceProvider getCurrentUsingDataSourceProvider()
Copyright © 2021. All rights reserved.