mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
提供多线程情况下子父线程获取切换数据源问题
This commit is contained in:
parent
6eadba4e77
commit
0a2e2f9799
@ -70,6 +70,12 @@
|
|||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>transmittable-thread-local</artifactId>
|
||||||
|
<version>2.14.2</version>
|
||||||
|
</dependency>
|
||||||
<!--optional end-->
|
<!--optional end-->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|||||||
@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.mybatisflex.core.datasource;
|
package com.mybatisflex.core.datasource;
|
||||||
|
|
||||||
|
import com.alibaba.ttl.TransmittableThreadLocal;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -25,12 +26,12 @@ public class DataSourceKey {
|
|||||||
/**
|
/**
|
||||||
* 通过注解设置的 key
|
* 通过注解设置的 key
|
||||||
*/
|
*/
|
||||||
private static final ThreadLocal<String> annotationKeyThreadLocal = new ThreadLocal<>();
|
private static final ThreadLocal<String> annotationKeyThreadLocal = new TransmittableThreadLocal<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过手动编码指定的 key
|
* 通过手动编码指定的 key
|
||||||
*/
|
*/
|
||||||
private static final ThreadLocal<String> manualKeyThreadLocal = new ThreadLocal<>();
|
private static final ThreadLocal<String> manualKeyThreadLocal = new TransmittableThreadLocal<>();
|
||||||
|
|
||||||
public static String manualKey;
|
public static String manualKey;
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,66 @@
|
|||||||
|
package com.mybatisflex.test;
|
||||||
|
|
||||||
|
import com.mybatisflex.core.MybatisFlexBootstrap;
|
||||||
|
import com.mybatisflex.core.audit.AuditManager;
|
||||||
|
import com.mybatisflex.core.audit.ConsoleMessageCollector;
|
||||||
|
import com.mybatisflex.core.audit.MessageCollector;
|
||||||
|
import com.mybatisflex.core.datasource.DataSourceKey;
|
||||||
|
import com.mybatisflex.core.row.Db;
|
||||||
|
import com.mybatisflex.core.row.Row;
|
||||||
|
import com.mybatisflex.core.row.RowUtil;
|
||||||
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
|
import java.util.List;
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||||
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author life
|
||||||
|
*/
|
||||||
|
public class MultiThreadsTest {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
DataSource dataSource = new EmbeddedDatabaseBuilder()
|
||||||
|
.setType(EmbeddedDatabaseType.H2)
|
||||||
|
.setName("db1")
|
||||||
|
.addScript("schema.sql")
|
||||||
|
.addScript("data.sql")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HikariDataSource dataSource2 = new HikariDataSource();
|
||||||
|
dataSource2.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/flex_test?characterEncoding=utf-8");
|
||||||
|
dataSource2.setUsername("root");
|
||||||
|
dataSource2.setPassword("131496");
|
||||||
|
|
||||||
|
MybatisFlexBootstrap.getInstance()
|
||||||
|
.setDataSource(dataSource)
|
||||||
|
.addMapper(AccountMapper.class)
|
||||||
|
.addDataSource("ds2", dataSource2)
|
||||||
|
.start();
|
||||||
|
|
||||||
|
//开启审计功能
|
||||||
|
AuditManager.setAuditEnable(true);
|
||||||
|
|
||||||
|
//设置 SQL 审计收集器
|
||||||
|
MessageCollector collector = new ConsoleMessageCollector();
|
||||||
|
AuditManager.setMessageCollector(collector);
|
||||||
|
|
||||||
|
//默认查询 db1
|
||||||
|
System.out.println("\n------ds1");
|
||||||
|
List<Row> rows1 = Db.selectAll(null, "tb_account");
|
||||||
|
RowUtil.printPretty(rows1);
|
||||||
|
|
||||||
|
System.out.println("\n------ds2");
|
||||||
|
DataSourceKey.use("ds2");
|
||||||
|
new Thread(() -> {
|
||||||
|
//查询数据源 ds2
|
||||||
|
System.out.println("\n------Thread-ds2");
|
||||||
|
List<Row> rows = Db.selectAll(null, "tb_account");
|
||||||
|
RowUtil.printPretty(rows);
|
||||||
|
}).start();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user