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

时间:2024-07-02 19:06:08

场景介绍:

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

数据源初始化

@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");
} }

执行日志

-- ::04.584 [http-nio--exec-] DEBUG o.s.jdbc.datasource.DataSourceTransactionManager - Acquired Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@3a6cc709] for JDBC transaction
-- ::04.588 [http-nio--exec-] DEBUG o.s.jdbc.datasource.DataSourceTransactionManager - Switching JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@3a6cc709] to manual commit
-- ::04.637 [http-nio--exec-] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
-- ::04.650 [http-nio--exec-] DEBUG org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@18b9642b]
-- ::04.657 [http-nio--exec-] DEBUG o.springframework.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource
-- ::04.658 [http-nio--exec-] DEBUG org.apache.catalina.loader.WebappClassLoaderBase - findResources(META-INF/druid/wall/mysql/deny-variant.txt)
-- ::04.658 [http-nio--exec-] DEBUG org.apache.catalina.loader.WebappClassLoaderBase - findResources(META-INF/druid/wall/mysql/deny-schema.txt)
-- ::04.659 [http-nio--exec-] DEBUG org.apache.catalina.loader.WebappClassLoaderBase - findResources(META-INF/druid/wall/mysql/deny-function.txt)
-- ::04.659 [http-nio--exec-] DEBUG org.apache.catalina.loader.WebappClassLoaderBase - findResources(META-INF/druid/wall/mysql/deny-table.txt)
-- ::04.660 [http-nio--exec-] DEBUG org.apache.catalina.loader.WebappClassLoaderBase - findResources(META-INF/druid/wall/mysql/deny-object.txt)
-- ::04.660 [http-nio--exec-] DEBUG org.apache.catalina.loader.WebappClassLoaderBase - findResources(META-INF/druid/wall/mysql/readonly-table.txt)
-- ::04.660 [http-nio--exec-] DEBUG org.apache.catalina.loader.WebappClassLoaderBase - findResources(META-INF/druid/wall/mysql/permit-function.txt)
-- ::04.661 [http-nio--exec-] DEBUG org.apache.catalina.loader.WebappClassLoaderBase - findResources(META-INF/druid/wall/mysql/permit-table.txt)
-- ::04.662 [http-nio--exec-] DEBUG org.apache.catalina.loader.WebappClassLoaderBase - findResources(META-INF/druid/wall/mysql/permit-schema.txt)
-- ::04.662 [http-nio--exec-] DEBUG org.apache.catalina.loader.WebappClassLoaderBase - findResources(META-INF/druid/wall/mysql/permit-variant.txt)
-- ::04.663 [http-nio--exec-] DEBUG org.apache.catalina.loader.WebappClassLoaderBase - findResources(META-INF/services/com.alibaba.druid.filter.Filter)
-- ::05.868 [http-nio--exec-] INFO com.alibaba.druid.pool.DruidDataSource - {dataSource-} inited
-- ::05.868 [http-nio--exec-] DEBUG o.springframework.jdbc.datasource.DataSourceUtils - Registering transaction synchronization for JDBC Connection
-- ::05.870 [http-nio--exec-] DEBUG o.m.spring.transaction.SpringManagedTransaction - JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@58c237f2] will be managed by Spring
-- ::05.874 [http-nio--exec-] DEBUG c.q.mapper.read.order.VipOrderReadingMapper.test - ==> Preparing: update vip_order_reading set order_status = where reading_vip_order_id =
-- ::06.049 [http-nio--exec-] DEBUG c.q.mapper.read.order.VipOrderReadingMapper.test - ==> Parameters:
-- ::06.191 [http-nio--exec-] DEBUG c.q.mapper.read.order.VipOrderReadingMapper.test - <== Updates:
-- ::06.192 [http-nio--exec-] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@18b9642b]
-- ::06.192 [http-nio--exec-] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
-- ::06.192 [http-nio--exec-] DEBUG org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@22fb4694]
-- ::06.194 [http-nio--exec-] DEBUG o.m.spring.transaction.SpringManagedTransaction - JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@3a6cc709] will be managed by Spring
-- ::06.194 [http-nio--exec-] DEBUG c.q.m.payment.order.GlobalRefundOrderMapper.test - ==> Preparing: select count(*) fromglobal_refund_order
-- ::06.198 [http-nio--exec-] DEBUG c.q.m.payment.order.GlobalRefundOrderMapper.test - ==> Parameters:
-- ::06.255 [http-nio--exec-] DEBUG c.q.m.payment.order.GlobalRefundOrderMapper.test - <== Total:
-- ::06.256 [http-nio--exec-] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@22fb4694]
-- ::06.256 [http-nio--exec-] DEBUG org.mybatis.spring.SqlSessionUtils - Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@18b9642b] from current transaction
-- ::06.256 [http-nio--exec-] DEBUG c.q.m.read.order.TenVipOrderReadingMapper.test - ==> Preparing: update ten_vip_order_reading set order_status = where reading_vip_order_id =
-- ::06.257 [http-nio--exec-] DEBUG c.q.m.read.order.TenVipOrderReadingMapper.test - ==> Parameters:
-- ::06.367 [http-nio--exec-] DEBUG c.q.m.read.order.TenVipOrderReadingMapper.test - <== Updates:
-- ::06.367 [http-nio--exec-] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@18b9642b]
-- ::06.368 [http-nio--exec-] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@18b9642b]
-- ::06.368 [http-nio--exec-] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@18b9642b]
-- ::06.368 [http-nio--exec-] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@22fb4694]
-- ::06.368 [http-nio--exec-] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@22fb4694]
-- ::06.368 [http-nio--exec-] DEBUG o.springframework.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource
-- ::06.368 [http-nio--exec-] DEBUG o.s.jdbc.datasource.DataSourceTransactionManager - Initiating transaction rollback
-- ::06.368 [http-nio--exec-] DEBUG o.s.jdbc.datasource.DataSourceTransactionManager - Rolling back JDBC transaction on Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@3a6cc709]
-- ::06.477 [http-nio--exec-] DEBUG o.s.jdbc.datasource.DataSourceTransactionManager - Releasing JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@3a6cc709] after transaction
-- ::06.477 [http-nio--exec-] DEBUG o.springframework.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource
-- ::06.479 [http-nio--exec-] 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
-- ::06.480 [http-nio--exec-] DEBUG o.s.b.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'qingControllerAdvice'
-- ::06.480 [http-nio--exec-] 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)
-- ::06.483 [http-nio--exec-] ERROR com.qing.controller.aop.QingControllerAdvice - java.lang.RuntimeException: test
at com.qing.service.test.TestService.test(TestService.java:)

由日志可以知道用的JDBC Connection

-- ::04.588 [http-nio--exec-] DEBUG o.s.jdbc.datasource.DataSourceTransactionManager - Switching JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@3a6cc709] to manual commit

注意最后的回滚语句

-- ::06.368 [http-nio--exec-] DEBUG o.s.jdbc.datasource.DataSourceTransactionManager - Rolling back JDBC transaction on Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@3a6cc709]

代码结果是什么,你懂的