Skip to content

Commit b63df77

Browse files
author
何惠民
committed
Fixed issue of can't switch datasource
1 parent c3cfc2d commit b63df77

File tree

2 files changed

+55
-57
lines changed

2 files changed

+55
-57
lines changed
Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package cn.com.hellowood.dynamicdatasource.configuration;
22

3-
import org.springframework.beans.factory.annotation.Qualifier;
3+
import org.mybatis.spring.SqlSessionFactoryBean;
44
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
55
import org.springframework.boot.context.properties.ConfigurationProperties;
66
import org.springframework.context.annotation.Bean;
77
import org.springframework.context.annotation.Configuration;
8-
import org.springframework.context.annotation.Primary;
98

109
import javax.sql.DataSource;
1110
import java.util.HashMap;
@@ -14,21 +13,19 @@
1413
/**
1514
* Multiple DataSource Configurer
1615
*
17-
* @Date 2017-08-15 11:37
18-
* @Author HelloWood
19-
* @Email hellowoodes@gmail.com
16+
* @author HelloWood
17+
* @date 2017 -08-15 11:37
18+
* @Email hellowoodes @gmail.com
2019
*/
2120
@Configuration
2221
public class DataSourceConfigurer {
2322

2423
/**
2524
* master DataSource
2625
*
27-
* @return
26+
* @return data source
2827
*/
2928
@Bean("master")
30-
@Qualifier("master")
31-
@Primary
3229
@ConfigurationProperties(prefix = "application.server.db.master")
3330
public DataSource master() {
3431
return DataSourceBuilder.create().build();
@@ -37,53 +34,54 @@ public DataSource master() {
3734
/**
3835
* slave DataSource
3936
*
40-
* @return
37+
* @return data source
4138
*/
4239
@Bean("slave")
43-
@Qualifier("slave")
4440
@ConfigurationProperties(prefix = "application.server.db.slave")
4541
public DataSource slave() {
4642
return DataSourceBuilder.create().build();
4743
}
4844

45+
/**
46+
* Dynamic data source.
47+
*
48+
* @return the data source
49+
*/
4950
@Bean("dynamicDataSource")
50-
@Qualifier("dynamicDataSource")
51-
public DataSource dataSource() {
51+
public DataSource dynamicDataSource() {
5252
DynamicRoutingDataSource dynamicRoutingDataSource = new DynamicRoutingDataSource();
53-
54-
dynamicRoutingDataSource.setDefaultTargetDataSource(slave());
55-
5653
Map<Object, Object> dataSourceMap = new HashMap<>(2);
5754
dataSourceMap.put("master", master());
5855
dataSourceMap.put("slave", slave());
56+
57+
// Set master datasource as default
58+
dynamicRoutingDataSource.setDefaultTargetDataSource(master());
59+
// Set master and slave datasource as target datasource
5960
dynamicRoutingDataSource.setTargetDataSources(dataSourceMap);
60-
DynamicDataSourceContextHolder.dataSourceIds.addAll(dataSourceMap.keySet());
61+
62+
// To put datasource keys into DataSourceContextHolder to judge if the datasource is exist
63+
DynamicDataSourceContextHolder.dataSourceKeys.addAll(dataSourceMap.keySet());
6164
return dynamicRoutingDataSource;
6265
}
63-
//
64-
// @Bean
65-
// public PlatformTransactionManager transactionManager(DataSource dynamicDataSource) {
66-
// return new DataSourceTransactionManager(dynamicDataSource);
67-
// }
68-
//
69-
// @Bean
70-
// @ConfigurationProperties(prefix = "mybatis")
71-
// public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dynamicDataSource) {
72-
// SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
73-
// sqlSessionFactoryBean.setDataSource(dynamicDataSource);
74-
// return sqlSessionFactoryBean;
75-
// }
76-
//
77-
// @Bean
78-
// public SqlSessionFactory sqlSessionFactory() throws Exception {
79-
// return sqlSessionFactoryBean(dataSource()).getObject();
80-
//// return sqlSessionFactoryBean.getObject();
81-
// }
82-
//
83-
// @Bean
84-
// public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
85-
// SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
86-
// return sqlSessionTemplate;
87-
// }
66+
67+
/**
68+
* Sql session factory bean.
69+
* Here to config datasource for SqlSessionFactory
70+
* <p>
71+
* You need to add @{@code @ConfigurationProperties(prefix = "mybatis")}, if you are using *.xml file,
72+
* the {@code 'mybatis.type-aliases-package'} and {@code 'mybatis.mapper-locations'} should be set in
73+
* {@code 'application.properties'} file, or there will appear invalid bond statement exception
74+
*
75+
* @return the sql session factory bean
76+
*/
77+
@Bean
78+
@ConfigurationProperties(prefix = "mybatis")
79+
public SqlSessionFactoryBean sqlSessionFactoryBean() {
80+
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
81+
// Here is very important, if don't config this, will can't switch datasource
82+
// put all datasource into SqlSessionFactoryBean, then will autoconfig SqlSessionFactory
83+
sqlSessionFactoryBean.setDataSource(dynamicDataSource());
84+
return sqlSessionFactoryBean;
85+
}
8886
}
8987

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
11
package cn.com.hellowood.dynamicdatasource.configuration;
22

33

4-
import org.slf4j.Logger;
5-
import org.slf4j.LoggerFactory;
6-
74
import java.util.ArrayList;
85
import java.util.List;
96

107
/**
118
* Multiple DataSource Context Holder
129
*
13-
* @Date 2017-08-15 14:26
14-
* @Author HelloWood
15-
* @Email hellowoodes@gmail.com
10+
* @author HelloWood
11+
* @date 2017 -08-15 14:26
12+
* @Email hellowoodes @gmail.com
1613
*/
17-
//@Component
1814
public class DynamicDataSourceContextHolder {
1915

20-
private static final Logger logger = LoggerFactory.getLogger(DynamicDataSourceContextHolder.class);
16+
// private static final Logger logger = LoggerFactory.getLogger(DynamicDataSourceContextHolder.class);
2117

2218
/**
2319
* Maintain variable for every thread, to avoid effect other thread
2420
*/
25-
private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
21+
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>() {
22+
@Override
23+
protected String initialValue() {
24+
return "master";
25+
}
26+
};
2627

2728
/**
2829
* All DataSource List
2930
*/
30-
public static List<Object> dataSourceIds = new ArrayList<>();
31+
public static List<Object> dataSourceKeys = new ArrayList<>();
3132

3233
/**
3334
* To switch DataSource
3435
*
35-
* @param key
36+
* @param key the key
3637
*/
3738
public static void setDataSourceKey(String key) {
3839
contextHolder.set(key);
@@ -41,7 +42,7 @@ public static void setDataSourceKey(String key) {
4142
/**
4243
* Get current DataSource
4344
*
44-
* @return
45+
* @return data source key
4546
*/
4647
public static String getDataSourceKey() {
4748
return contextHolder.get();
@@ -57,11 +58,10 @@ public static void clearDataSourceKey() {
5758
/**
5859
* Check if give DataSource is in current DataSource list
5960
*
60-
* @param key
61-
* @return
61+
* @param key the key
62+
* @return boolean boolean
6263
*/
6364
public static boolean containDataSourceKey(String key) {
64-
logger.info("DataSourceIds is {}", dataSourceIds);
65-
return dataSourceIds.contains(key);
65+
return dataSourceKeys.contains(key);
6666
}
6767
}

0 commit comments

Comments
 (0)