1.Spring mvc、mybatis中增加多个数据源,各个数据源支持自己的事务,多数据源事务参考JTA。
2.我已验证过,各个数据源都是好使的,事务都是支持回滚的。
整理人:哥学会了沉默 2015.7.17
1.多个数据源配置
#database config here#
#本机
db_local.url=jdbc:mysql://127.0.0.1:3307/demo
db_local.username=root
db_local.password=root
db_local.driverClassName=com.mysql.jdbc.Driver
db_local.connectionProperties=useUnicode=true;characterEncoding=UTF8;rewriteBatchedStatements=true;socketTimeout=180000;autoReconnect=true;autoReconnectForPools=true;zeroDateTimeBehavior=convertToNull
db_local.defaultAutoCommit=false
db_local.initialSize=0
db_local.maxTotal=20
db_local.maxIdle=5
db_local.minIdle=2
db_local.maxWaitMillis=10000
db_local.validationQuery=select 1
db_local.testOnBorrow=true
db_local.testWhileIdle=false
db_local.testOnReturn=false
db_local.timeBetweenEvictionRunsMillis=120000
db_local.minEvictableIdleTimeMillis=300000
#测试环境
db_test.url=jdbc:mysql://127.0.0.1:3308/demo
db_test.username=root
db_test.password=root
db_test.driverClassName=com.mysql.jdbc.Driver
db_test.connectionProperties=useUnicode=true;characterEncoding=UTF8;rewriteBatchedStatements=true;socketTimeout=180000;autoReconnect=true;autoReconnectForPools=true;zeroDateTimeBehavior=convertToNull
db_test.defaultAutoCommit=false
db_test.initialSize=0
db_test.maxTotal=20
db_test.maxIdle=5
db_test.minIdle=2
db_test.maxWaitMillis=10000
db_test.validationQuery=select 1
db_test.testOnBorrow=true
db_test.testWhileIdle=false
db_test.testOnReturn=false
db_test.timeBetweenEvictionRunsMillis=120000
db_test.minEvictableIdleTimeMillis=300000
2.多个数据源
<!-- 加载数据库配置文件 -->
<context:property-placeholder location="classpath:conf/db.properties"
ignore-unresolvable="true" />
<!-- 本机数据库连接池配置 -->
<bean id="dataSourceLocal" class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<property name="url" value="${db_local.url}" />
<property name="username" value="${db_local.username}" />
<property name="password" value="${db_local.password}" />
<property name="driverClassName" value="${db_local.driverClassName}" />
<property name="connectionProperties" value="${db_local.connectionProperties}" />
<property name="defaultAutoCommit" value="${db_local.defaultAutoCommit}" />
<property name="initialSize" value="${db_local.initialSize}" />
<property name="maxTotal" value="${db_local.maxTotal}" />
<property name="minIdle" value="${db_local.minIdle}" />
<property name="maxIdle" value="${db_local.maxIdle}" />
<property name="maxWaitMillis" value="${db_local.maxWaitMillis}" />
<property name="validationQuery" value="${db_local.validationQuery}" />
<property name="testOnBorrow" value="${db_local.testOnBorrow}" />
<property name="testOnReturn" value="${db_local.testOnReturn}" />
<property name="testWhileIdle" value="${db_local.testWhileIdle}" />
<property name="timeBetweenEvictionRunsMillis" value="${db_local.timeBetweenEvictionRunsMillis}" />
<property name="minEvictableIdleTimeMillis" value="${db_local.minEvictableIdleTimeMillis}" />
</bean>
<!-- 测试数据库连接池配置 -->
<bean id="dataSourceTest" class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<property name="url" value="${db_test.url}" />
<property name="username" value="${db_test.username}" />
<property name="password" value="${db_test.password}" />
<property name="driverClassName" value="${db_test.driverClassName}" />
<property name="connectionProperties" value="${db_test.connectionProperties}" />
<property name="defaultAutoCommit" value="${db_test.defaultAutoCommit}" />
<property name="initialSize" value="${db_test.initialSize}" />
<property name="maxTotal" value="${db_test.maxTotal}" />
<property name="minIdle" value="${db_test.minIdle}" />
<property name="maxIdle" value="${db_test.maxIdle}" />
<property name="maxWaitMillis" value="${db_test.maxWaitMillis}" />
<property name="validationQuery" value="${db_test.validationQuery}" />
<property name="testOnBorrow" value="${db_test.testOnBorrow}" />
<property name="testOnReturn" value="${db_test.testOnReturn}" />
<property name="testWhileIdle" value="${db_test.testWhileIdle}" />
<property name="timeBetweenEvictionRunsMillis" value="${db_test.timeBetweenEvictionRunsMillis}" />
<property name="minEvictableIdleTimeMillis" value="${db_test.minEvictableIdleTimeMillis}" />
</bean>
3.多个sqlSessionFactory。不同的数据源用不同的mybatis配置文件,便于管理。
<!-- Mybatis链接工厂 -->
<bean id="sqlSessionFactoryLocal" class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSourceLocal" p:configLocation="classpath:mybatis/configuration.xml" />
<!-- Mybatis链接工厂 -->
<bean id="sqlSessionFactoryTest" class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSourceTest" p:configLocation="classpath:mybatis/configurationTest.xml" />
4.自动扫包。不同的数据源用不同的包路径,便于管理。
<!-- Mybatis自动扫包,使用这种方式可以只写dao接口不写实现类,但是推荐还是 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.letv.shop.demoWeb.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryLocal" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.letv.shop.demoWeb.dao2" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryTest" />
</bean>
5.关键代码。“@Transactional(value = "transactionManagerTest"”中的value不是必填项,但是默认是spring配置中的的最上面的哪个配置,如果有多个数据源,建议加上这个value。
@Autowired
private BizEventDao bizEventDao;
@Autowired
private BizEventTestDao bizEventTestDao;
@Override
@Transactional(value = "transactionManagerLocal", rollbackFor = Exception.class)
public void addBizEvent(BizEvent bizEvent) {
bizEvent.setBizKey("1");
bizEventDao.addBizEvent(bizEvent);
}
@Transactional(value = "transactionManagerTest", rollbackFor = Exception.class)
public void testDao(BizEvent bizEvent) {
bizEvent.setBizKey("1");
bizEventTestDao.addBizEvent(bizEvent);
}