`
woaiyingyu123
  • 浏览: 69717 次
  • 性别: Icon_minigender_1
  • 来自: 广西
社区版块
存档分类
最新评论

spring管理数据源切换的配置和实现

 
阅读更多
在applicationContent.xml中:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config/jdbc.properties</value>
<value>classpath:config/cpool.properties</value>
</list>
</property>
</bean>

<!-- 主数据源 -->
<bean id="masterDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.masterDriverClass}" />
<property name="jdbcUrl" value="${jdbc.masterUrl}" />
<property name="user" value="${jdbc.masterUsername}" />
<property name="password" value="${jdbc.masterPassword}" />
<property name="checkoutTimeout" value="${cpool.checkoutTimeout}" />
<property name="initialPoolSize" value="${cpool.initialPoolSize}" />
<property name="minPoolSize" value="${cpool.minPoolSize}" />
<property name="maxPoolSize" value="${cpool.maxPoolSize}" />
<property name="maxIdleTime" value="${cpool.maxIdleTime}" />
<property name="acquireIncrement" value="${cpool.acquireIncrement}" />
<property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}" />
</bean>

<!-- 从数据源 -->
<bean id="slaveDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.slaveDriverClass}" />
<property name="jdbcUrl" value="${jdbc.slaveUrl}" />
<property name="user" value="${jdbc.slaveUsername}" />
<property name="password" value="${jdbc.slavePassword}" />
<property name="checkoutTimeout" value="${cpool.checkoutTimeout}" />
<property name="initialPoolSize" value="${cpool.initialPoolSize}" />
<property name="minPoolSize" value="${cpool.minPoolSize}" />
<property name="maxPoolSize" value="${cpool.maxPoolSize}" />
<property name="maxIdleTime" value="${cpool.maxIdleTime}" />
<property name="acquireIncrement" value="${cpool.acquireIncrement}" />
<property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}" />
</bean>

<!-- 动态切换数据源 -->
<bean id="dataSource" class="com.xxx.xxx.DynamicDataSource"> 
        <property name="targetDataSources"> 
            <map key-type="java.lang.String"> 
                <entry key="slave" value-ref="slaveDataSource" />
                <entry key="master" value-ref="masterDataSource" /> 
            </map> 
        </property> 
        <property name="defaultTargetDataSource" ref="masterDataSource" /> 
    </bean> 

<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="*" read-only="true" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>

<!-- 动态切换数据源的切面 --> 
    <bean id="dataSourceAdvice" class="com.xxx.xxx.DataSourceAdvice" /> 
   
<!-- 那些类的哪些方法参与事务 -->
<aop:config>
<aop:pointcut id="txPointcut" expression="execution(* *..service*..*(..))" />
    <aop:advisor   order="-2147483648"  pointcut-ref="txPointcut"  advice-ref="dataSourceAdvice" />  <!-- 数据源主从切换(顺序要在事务上面) -->
<aop:advisor    pointcut-ref="txPointcut" advice-ref="txAdvice" /><!-- 事务 -->
</aop:config>


Java类:

/** 数据源动态切换类 */
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceSwitcher.getDataSource();
}

}


/** 数据源转换器 */
public class DataSourceSwitcher {
private static final ThreadLocal contextHolder = new ThreadLocal();

@SuppressWarnings("unchecked")
public static void setDataSource(String dataSource) {
contextHolder.set(dataSource);
}

public static void setMaster() {
setDataSource("master");
}

public static void setSlave() {
setDataSource("slave");
}

public static String getDataSource() {
return (String) contextHolder.get();
}
}

/** 数据源判断切面 */
public class DataSourceAdvice implements MethodBeforeAdvice, ThrowsAdvice {

public void before(Method method, Object[] args, Object target) throws Throwable {
if (method.getName().startsWith("add") || method.getName().startsWith("create") || method.getName().startsWith("save") || method.getName().startsWith("edit") || method.getName().startsWith("update") || method.getName().startsWith("delete") || method.getName().startsWith("remove")) {
DataSourceSwitcher.setMaster();

} else {
DataSourceSwitcher.setSlave();
}

}

public void afterThrowing(Method method, Object[] args, Object target, Exception ex) throws Throwable {
System.out.println("异常切换到: master");
DataSourceSwitcher.setMaster();
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics