通过日志来看Spring跨库更新操作的事务

场景介绍:

一个项目俩个数据源,连接俩个不同的库

数据源初始化

@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]

代码结果是什么,你懂的

posted on 2019-03-26 15:12 好吧,就是菜菜 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/shuiyonglewodezzzzz/p/10600284.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值