场景介绍:
一个项目俩个数据源,连接俩个不同的库
数据源初始化
@Configuration @MapperScan(basePackages = "com.qing.mapper.payment", sqlSessionTemplateRef = "paymentSqlSessionTemplate") public class PaymentDataSourceConfig { @Bean(name = "paymentDataSource") @ConfigurationProperties(prefix = "spring.datasource.payment") @Primary public DataSource paymentDataSource() { return DruidDataSourceBuilder.create().build(); } @Bean(name = "paymentSqlSessionFactory") @Primary public SqlSessionFactory paymentSqlSessionFactory(@Qualifier("paymentDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); return bean.getObject(); } @Bean(name = "paymentTransactionManager") @Primary public DataSourceTransactionManager paymentTransactionManager(@Qualifier("paymentDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "paymentSqlSessionTemplate") @Primary public SqlSessionTemplate paymentSqlSessionTemplate(@Qualifier("paymentSqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
@Configuration @MapperScan(basePackages = "com.qing.mapper.read", sqlSessionTemplateRef = "readSqlSessionTemplate") public class ReadDataSourceConfig { @Bean(name = "readDataSource") @ConfigurationProperties(prefix = "spring.datasource.read") public DataSource readDataSource() { return DruidDataSourceBuilder.create().build(); } @Bean(name = "readSqlSessionFactory") public SqlSessionFactory readSqlSessionFactory(@Qualifier("readDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); return bean.getObject(); } @Bean(name = "readTransactionManager") public DataSourceTransactionManager readTransactionManager(@Qualifier("readDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "readSqlSessionTemplate") public SqlSessionTemplate readSqlSessionTemplate(@Qualifier("readSqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
测试代码
@Service public class TestService { @Autowired VipOrderReadingMapper mapper1; @Autowired TenVipOrderReadingMapper mapper2; @Autowired GlobalRefundOrderMapper mapper3; @Transactional public void test() { mapper1.test(); mapper3.test(); mapper2.test(); throw new RuntimeException("test"); } }
执行日志
2019-03-26 15:04:04.584 [http-nio-8080-exec-10] DEBUG o.s.jdbc.datasource.DataSourceTransactionManager - Acquired Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@3a6cc709] for JDBC transaction 2019-03-26 15:04:04.588 [http-nio-8080-exec-10] DEBUG o.s.jdbc.datasource.DataSourceTransactionManager - Switching JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@3a6cc709] to manual commit 2019-03-26 15:04:04.637 [http-nio-8080-exec-10] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession 2019-03-26 15:04:04.650 [http-nio-8080-exec-10] DEBUG org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@18b9642b] 2019-03-26 15:04:04.657 [http-nio-8080-exec-10] DEBUG o.springframework.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource 2019-03-26 15:04:04.658 [http-nio-8080-exec-10] DEBUG org.apache.catalina.loader.WebappClassLoaderBase - findResources(META-INF/druid/wall/mysql/deny-variant.txt) 2019-03-26 15:04:04.658 [http-nio-8080-exec-10] DEBUG org.apache.catalina.loader.WebappClassLoaderBase - findResources(META-INF/druid/wall/mysql/deny-schema.txt) 2019-03-26 15:04:04.659 [http-nio-8080-exec-10] DEBUG org.apache.catalina.loader.WebappClassLoaderBase - findResources(META-INF/druid/wall/mysql/deny-function.txt) 2019-03-26 15:04:04.659 [http-nio-8080-exec-10] DEBUG org.apache.catalina.loader.WebappClassLoaderBase - findResources(META-INF/druid/wall/mysql/deny-table.txt) 2019-03-26 15:04:04.660 [http-nio-8080-exec-10] DEBUG org.apache.catalina.loader.WebappClassLoaderBase - findResources(META-INF/druid/wall/mysql/deny-object.txt) 2019-03-26 15:04:04.660 [http-nio-8080-exec-10] DEBUG org.apache.catalina.loader.WebappClassLoaderBase - findResources(META-INF/druid/wall/mysql/readonly-table.txt) 2019-03-26 15:04:04.660 [http-nio-8080-exec-10] DEBUG org.apache.catalina.loader.WebappClassLoaderBase - findResources(META-INF/druid/wall/mysql/permit-function.txt) 2019-03-26 15:04:04.661 [http-nio-8080-exec-10] DEBUG org.apache.catalina.loader.WebappClassLoaderBase - findResources(META-INF/druid/wall/mysql/permit-table.txt) 2019-03-26 15:04:04.662 [http-nio-8080-exec-10] DEBUG org.apache.catalina.loader.WebappClassLoaderBase - findResources(META-INF/druid/wall/mysql/permit-schema.txt) 2019-03-26 15:04:04.662 [http-nio-8080-exec-10] DEBUG org.apache.catalina.loader.WebappClassLoaderBase - findResources(META-INF/druid/wall/mysql/permit-variant.txt) 2019-03-26 15:04:04.663 [http-nio-8080-exec-10] DEBUG org.apache.catalina.loader.WebappClassLoaderBase - findResources(META-INF/services/com.alibaba.druid.filter.Filter) 2019-03-26 15:04:05.868 [http-nio-8080-exec-10] INFO com.alibaba.druid.pool.DruidDataSource - {dataSource-2} inited 2019-03-26 15:04:05.868 [http-nio-8080-exec-10] DEBUG o.springframework.jdbc.datasource.DataSourceUtils - Registering transaction synchronization for JDBC Connection 2019-03-26 15:04:05.870 [http-nio-8080-exec-10] DEBUG o.m.spring.transaction.SpringManagedTransaction - JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@58c237f2] will be managed by Spring 2019-03-26 15:04:05.874 [http-nio-8080-exec-10] DEBUG c.q.mapper.read.order.VipOrderReadingMapper.test - ==> Preparing: update vip_order_reading set order_status = 0 where reading_vip_order_id = 32152481900022110 2019-03-26 15:04:06.049 [http-nio-8080-exec-10] DEBUG c.q.mapper.read.order.VipOrderReadingMapper.test - ==> Parameters: 2019-03-26 15:04:06.191 [http-nio-8080-exec-10] DEBUG c.q.mapper.read.order.VipOrderReadingMapper.test - <== Updates: 1 2019-03-26 15:04:06.192 [http-nio-8080-exec-10] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@18b9642b] 2019-03-26 15:04:06.192 [http-nio-8080-exec-10] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession 2019-03-26 15:04:06.192 [http-nio-8080-exec-10] DEBUG org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@22fb4694] 2019-03-26 15:04:06.194 [http-nio-8080-exec-10] DEBUG o.m.spring.transaction.SpringManagedTransaction - JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@3a6cc709] will be managed by Spring 2019-03-26 15:04:06.194 [http-nio-8080-exec-10] DEBUG c.q.m.payment.order.GlobalRefundOrderMapper.test - ==> Preparing: select count(*) fromglobal_refund_order 2019-03-26 15:04:06.198 [http-nio-8080-exec-10] DEBUG c.q.m.payment.order.GlobalRefundOrderMapper.test - ==> Parameters: 2019-03-26 15:04:06.255 [http-nio-8080-exec-10] DEBUG c.q.m.payment.order.GlobalRefundOrderMapper.test - <== Total: 1 2019-03-26 15:04:06.256 [http-nio-8080-exec-10] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@22fb4694] 2019-03-26 15:04:06.256 [http-nio-8080-exec-10] DEBUG org.mybatis.spring.SqlSessionUtils - Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@18b9642b] from current transaction 2019-03-26 15:04:06.256 [http-nio-8080-exec-10] DEBUG c.q.m.read.order.TenVipOrderReadingMapper.test - ==> Preparing: update ten_vip_order_reading set order_status = 0 where reading_vip_order_id = 36153137641513810 2019-03-26 15:04:06.257 [http-nio-8080-exec-10] DEBUG c.q.m.read.order.TenVipOrderReadingMapper.test - ==> Parameters: 2019-03-26 15:04:06.367 [http-nio-8080-exec-10] DEBUG c.q.m.read.order.TenVipOrderReadingMapper.test - <== Updates: 1 2019-03-26 15:04:06.367 [http-nio-8080-exec-10] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@18b9642b] 2019-03-26 15:04:06.368 [http-nio-8080-exec-10] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@18b9642b] 2019-03-26 15:04:06.368 [http-nio-8080-exec-10] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@18b9642b] 2019-03-26 15:04:06.368 [http-nio-8080-exec-10] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@22fb4694] 2019-03-26 15:04:06.368 [http-nio-8080-exec-10] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@22fb4694] 2019-03-26 15:04:06.368 [http-nio-8080-exec-10] DEBUG o.springframework.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource 2019-03-26 15:04:06.368 [http-nio-8080-exec-10] DEBUG o.s.jdbc.datasource.DataSourceTransactionManager - Initiating transaction rollback 2019-03-26 15:04:06.368 [http-nio-8080-exec-10] DEBUG o.s.jdbc.datasource.DataSourceTransactionManager - Rolling back JDBC transaction on Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@3a6cc709] 2019-03-26 15:04:06.477 [http-nio-8080-exec-10] DEBUG o.s.jdbc.datasource.DataSourceTransactionManager - Releasing JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@3a6cc709] after transaction 2019-03-26 15:04:06.477 [http-nio-8080-exec-10] DEBUG o.springframework.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource 2019-03-26 15:04:06.479 [http-nio-8080-exec-10] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception from handler [java.lang.String com.qing.controller.TestController.test()]: java.lang.RuntimeException: test 2019-03-26 15:04:06.480 [http-nio-8080-exec-10] DEBUG o.s.b.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'qingControllerAdvice' 2019-03-26 15:04:06.480 [http-nio-8080-exec-10] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Invoking @ExceptionHandler method: public com.qingread.utils.generate.RestGenerator<?> com.qing.controller.aop.QingControllerAdvice.errorHandler(java.lang.Exception) 2019-03-26 15:04:06.483 [http-nio-8080-exec-10] ERROR com.qing.controller.aop.QingControllerAdvice - java.lang.RuntimeException: test at com.qing.service.test.TestService.test(TestService.java:32)
由日志可以知道用的JDBC Connection
2019-03-26 15:04:04.588 [http-nio-8080-exec-10] DEBUG o.s.jdbc.datasource.DataSourceTransactionManager - Switching JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@3a6cc709] to manual commit
注意最后的回滚语句
2019-03-26 15:04:06.368 [http-nio-8080-exec-10] DEBUG o.s.jdbc.datasource.DataSourceTransactionManager - Rolling back JDBC transaction on Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@3a6cc709]
代码结果是什么,你懂的