ThreadLocal简单的说就是将线程与当前线程绑定,多线程环境下安全。 比如说多数据源,一个datasource的变量,应用根据datasource中配置的是user还是product来获取数据库连接。 那么就可以用ThreadLocal来存储。实现如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
private ThreadLocal<String> local = new ThreadLocal<String>();
public void switchDataSource(String dataSourceName) {
this.local.set(dataSourceName);
}
// 获取一个数据源配置
public DataSourceConfig getDataSourceConfig() {
String dataSourceName = this.local.get();
DataSourceConfig dsc = new DataSourceConfig();
DruidDataSource dataSource = null;
if (dataSourceName == null || dataSourceName.trim().length() <= 0) {
dataSource = ((DruidDataSource) defaultDataSource);
} else {
dataSource = (DruidDataSource) this.applicationContext.getBean(dataSourceName);
}
dsc.setUrl(dataSource.getUrl());
dsc.setPassword(dataSource.getPassword());
dsc.setUsername(dataSource.getUsername());
dsc.setDriverClassName(dataSource.getDriverClassName());
return dsc;
}
private DataSource getDataSource(){
String dataSourceName = this.local.get();
if (dataSourceName == null || dataSourceName.trim().length() <= 0) {
return defaultDataSource;
}
try {
return (DataSource) this.applicationContext.getBean(dataSourceName);
} catch (NoSuchBeanDefinitionException ex) {
logger.error(",getDataSource.error", ex);
throw new DateSourceException(ResultCode.DATA_SOURCE_FAIL);
}
}
//TODO 替换示例 //TODO ThreadLocal引发的内存溢出