mysql多数据源切换_Spring多数据源动态切换

原理DataSource向外提供一个 getConnection() 方法,得getConnection者得数据库AbstractRoutingDataSource 实现了 getConnection() 方法// line 166@Overridepublic Connection getConnection() throws SQLException {return determineTarg...
摘要由CSDN通过智能技术生成

原理

8e3acb887160e1da003c1f1a2876a643.png

3bf3640dbbdb50ea18c76a819eadc355.png

DataSource向外提供一个 getConnection() 方法,得getConnection者得数据库

AbstractRoutingDataSource 实现了 getConnection() 方法

// line 166

@Override

public Connection getConnection() throws SQLException {

return determineTargetDataSource().getConnection();

}

... 省略若干代码

// line 190

/**

* Retrieve the current target DataSource. Determines the

* {@link #determineCurrentLookupKey() current lookup key}, performs

* a lookup in the {@link #setTargetDataSources targetDataSources} map,

* falls back to the specified

* {@link #setDefaultTargetDataSource default target DataSource} if necessary.

* @see #determineCurrentLookupKey()

*/

protected DataSource determineTargetDataSource() {

Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");

Object lookupKey = determineCurrentLookupKey();

DataSource dataSource = this.resolvedDataSources.get(lookupKey);

if (dataSource == null && (this.lenientFallback || lookupKey == null)) {

dataSource = this.resolvedDefaultDataSource;

}

if (dataSource == null) {

throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");

}

return dataSource;

}

/**

* 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

* {@link #resolveSpecifiedLookupKey} method.

*/

@Nullable

protected abstract Object determineCurrentLookupKey();

然而 ....

AbstractRoutingDataSource 的getConnection() 方法只是调用了 determinTargetDataSource().getConnection() 来获取真正DataSource的getConnection()。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL多数据源切换可以使用Spring框架提供的AbstractRoutingDataSource类和ThreadLocal来实现。具体步骤如下: 1. 创建一个实现AbstractRoutingDataSource的类,重写determineCurrentLookupKey()方法,该方法返回一个字符串类型的值,表示当前数据源的标识。 ``` public class MultipleDataSource extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { return DataSourceContextHolder.getDataSourceType(); } } ``` 2. 创建一个存储当前数据源标识的类DataSourceContextHolder,使用ThreadLocal来存储当前线程使用的数据源标识。 ``` public class DataSourceContextHolder { private static final ThreadLocal<String> contextHolder = new ThreadLocal<>(); public static void setDataSourceType(String dataSourceType) { contextHolder.set(dataSourceType); } public static String getDataSourceType() { return contextHolder.get(); } public static void clearDataSourceType() { contextHolder.remove(); } } ``` 3. 在Spring配置文件中配置数据源和多数据源切换类,如下所示: ``` <!-- 数据源1 --> <bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <!-- 数据源2 --> <bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test2"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <!-- 多数据源切换类 --> <bean id="multipleDataSource" class="com.example.demo.config.MultipleDataSource"> <property name="defaultTargetDataSource" ref="dataSource1"/> <property name="targetDataSources"> <map> <entry key="dataSource1" value-ref="dataSource1"/> <entry key="dataSource2" value-ref="dataSource2"/> </map> </property> </bean> ``` 4. 在需要切换数据源的地方调用DataSourceContextHolder.setDataSourceType()方法设置当前线程使用的数据源标识即可。 ``` @Service public class UserService { @Autowired private UserMapper userMapper; public void addUser(User user) { DataSourceContextHolder.setDataSourceType("dataSource2"); userMapper.addUser(user); DataSourceContextHolder.clearDataSourceType(); } public List<User> getUserList() { DataSourceContextHolder.setDataSourceType("dataSource1"); List<User> userList = userMapper.getUserList(); DataSourceContextHolder.clearDataSourceType(); return userList; } } ``` 以上即为MySQL多数据源切换的实现方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值