首页 技术 正文
技术 2022年11月6日
0 收藏 907 点赞 960 浏览 3195 个字

多数据源问题很常见,例如读写分离数据库配置。

1、首先配置多个datasource

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver">
</property>
<property name="url" value="jdbc:jtds:sqlserver://xxx:1433;databaseName=XXX">
</property>
<property name="username" value="XXX"></property>
<property name="password" value="XXX"></property>
</bean>
<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver">
</property>
<property name="url" value="jdbc:jtds:sqlserver://XXX:1433;databaseName=XXX">
</property>
<property name="username" value="XXX"></property>
<property name="password" value="XXX"></property>
</bean>

2、写一个DynamicDataSource类继承AbstractRoutingDataSource,并实现determineCurrentLookupKey方法

package com.standard.core.util;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return CustomerContextHolder.getCustomerType();
}
}

3、利用ThreadLocal解决线程安全问题

package com.standard.core.util;
public class CustomerContextHolder {
public static final String DATA_SOURCE_A = "dataSource";
public static final String DATA_SOURCE_B = "dataSource2";
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
public static void setCustomerType(String customerType) {
contextHolder.set(customerType);
}
public static String getCustomerType() {
return contextHolder.get();
}
public static void clearCustomerType() {
contextHolder.remove();
}
}

4、数据源配置

<bean id="dynamicDataSource" class="com.standard.core.util.DynamicDataSource" >
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry value-ref="dataSource" key="dataSource"></entry>
<entry value-ref="dataSource2" key="dataSource2"></entry>
</map>
</property>
<property name="defaultTargetDataSource" ref="dataSource" >
</property>
</bean>

5、利用拦截器,设置每个请求线程的CustomerContextHolder

public class DatabaseInterceptor implements Interceptor {    private static final Logger DB_INC_LOG = LoggerFactory.getLogger(DatabaseInterceptor.class);    @Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
try {
Method method = methodInvocation.getMethod();
if (method != null && method.getAnnotation(ReadOnlyDataSource.class) != null) {
DataSourceProvider.setDataSource(AvailableDataSources.READ);
}
return methodInvocation.proceed();
} finally {
DataSourceProvider.clearDataSource();
}

6、重写AbstractRoutingDataSource的方法determineCurrentLookupKey(),切换数据源,切换读写

@Override
protected Object determineCurrentLookupKey() {
if (System.currentTimeMillis() - RWSplittingClosed_cache_time <= RW_SPLITTING_CLOSED_TIMEOUT) {
if (RWSplittingClosed_cache) {
return AvailableDataSources.WRITE;
}
return DataSourceProvider.getDataSource();
}
Jedis jedis = null;
try {
jedis = pingJedis();
if (jedis == null) {
return AvailableDataSources.WRITE;
}
Boolean masterForced = Boolean.valueOf(jedis.get(Constants.RW_SPLITTING_CLOSED));
RWSplittingClosed_cache = masterForced;
RWSplittingClosed_cache_time = System.currentTimeMillis();
if (masterForced) {
return AvailableDataSources.WRITE;
}
return DataSourceProvider.getDataSource();
} catch (Exception e) {
log.error(e.getMessage());
return AvailableDataSources.WRITE;
} finally {
if (jedis != null) {
pool.returnResource(jedis);
}
}
}

 

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903