spring多数据源事务

 @Override
    //方法使用注解事务,指定asset数据源
    @Transactional(value = "asset", rollbackFor = Throwable.class)
    public void test() {
        //asset数据源,配置了事务管理器
        investMapper.selectById("1");

        investMapper.selectById("2");

        //assetNew数据源,没配置事务管理器
        save();

        select();

        log.info("stop");

    }
方法开始

方法刚进入test方法,还未执行具体逻辑

org.springframework.jdbc.datasource.DataSourceTransactionManager - Creating new transaction with name [com.fengjr.p2p.settle.service.TestService.TestServiceImpl.test]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; 'asset',-java.lang.Throwable
org.springframework.jdbc.datasource.DataSourceTransactionManager - Acquired Connection [jdbc:mysql://betadb.fengjr.inc:3306/asset?useUnicode=true&characterEncoding=UTF-8, [email protected], MySQL Connector Java] for JDBC transaction
org.springframework.jdbc.datasource.DataSourceTransactionManager - Switching JDBC Connection [jdbc:mysql://betadb.fengjr.inc:3306/asset?useUnicode=true&characterEncoding=UTF-8, [email protected], MySQL Connector Java] to manual commit

上面这段日志实际上是由动态代理类打印出来
1.创建事务
2.获取数据库连接a
3.将a设置为手动提交

第一步

执行 investMapper.selectById("1");

org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@72ca1a11]
org.mybatis.spring.transaction.SpringManagedTransaction - JDBC Connection [jdbc:mysql://betadb.fengjr.inc:3306/asset?useUnicode=true&characterEncoding=UTF-8, [email protected], MySQL Connector Java] will be managed by Spring
com.fengjr.p2p.settle.dao.asset.auto.InvestMapper.selectById - ooo Using Connection [jdbc:mysql://betadb.fengjr.inc:3306/asset?useUnicode=true&characterEncoding=UTF-8, [email protected], MySQL Connector Java]
com.fengjr.p2p.settle.dao.asset.auto.InvestMapper.selectById - ==>  Preparing: select ID, AMOUNT, BIDMETHOD, LOANID, PURPOSE, RATE, REPAYMETHOD, STATUS, SUBMITTIME, USERID, DAYS, MONTHS, YEARS, CREDITASSIGNID, ORIGINALAMOUNT, BUYAMOUNT, SOURCE, UPDATE_TIME, PAYTYPE, CURRENTLOANID, COMBINATIONID, COMBINATIONALLOTID, CATEGORY, CATEGORY_ID, TIMEFINISHED from TB_INVEST where ID = ? 
com.fengjr.p2p.settle.dao.asset.auto.InvestMapper.selectById - ==> Parameters: 1(String)
org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@72ca1a11]
第二步
org.mybatis.spring.SqlSessionUtils - Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@72ca1a11] from current transaction
com.fengjr.p2p.settle.dao.asset.auto.InvestMapper.selectById - ooo Using Connection [jdbc:mysql://betadb.fengjr.inc:3306/asset?useUnicode=true&characterEncoding=UTF-8, [email protected], MySQL Connector Java]
com.fengjr.p2p.settle.dao.asset.auto.InvestMapper.selectById - ==>  Preparing: select ID, AMOUNT, BIDMETHOD, LOANID, PURPOSE, RATE, REPAYMETHOD, STATUS, SUBMITTIME, USERID, DAYS, MONTHS, YEARS, CREDITASSIGNID, ORIGINALAMOUNT, BUYAMOUNT, SOURCE, UPDATE_TIME, PAYTYPE, CURRENTLOANID, COMBINATIONID, COMBINATIONALLOTID, CATEGORY, CATEGORY_ID, TIMEFINISHED from TB_INVEST where ID = ? 
com.fengjr.p2p.settle.dao.asset.auto.InvestMapper.selectById - ==> Parameters: 2(String)
org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@72ca1a11]
第三步
org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@40e1df78]
org.springframework.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource
org.springframework.jdbc.datasource.DataSourceUtils - Registering transaction synchronization for JDBC Connection
org.mybatis.spring.transaction.SpringManagedTransaction - JDBC Connection [com.dangdang.ddframe.rdb.sharding.jdbc.core.connection.ShardingConnection@5950a15f] will be managed by Spring
c.f.p2p.sharding.repayment.mapper.InvestRepaymentShardingMapper.insertSelective - ooo Using Connection [com.dangdang.ddframe.rdb.sharding.jdbc.core.connection.ShardingConnection@5950a15f]
c.f.p2p.sharding.repayment.mapper.InvestRepaymentShardingMapper.insertSelective - ==>  Preparing: insert into TB_INVEST_REPAYMENT ( ID, CURRENTPERIOD, STATUS, SOURCE_ID, AMOUNTINTEREST, AMOUNTOUTSTANDING, AMOUNTPRINCIPAL, DUEDATE, INVEST_ID, USER_ID, LOAN_ID, TIMEFINISHED ) values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) 
c.f.p2p.sharding.repayment.mapper.InvestRepaymentShardingMapper.insertSelective - ==> Parameters: K562369F-98AC-4AE2-9526-DB453FE990D1(String), 6(Integer), REPAYED(String), REPAYED(String), 0.1(BigDecimal), 0.1(BigDecimal), 0.1(BigDecimal), 2018-01-03(String), 17022000-A000-AAA2-07CA-487C9C782066(String), YFDBFA9F-C8B0-499C-AE67-45FD7608858B(String), S062369F-98AC-4AE2-9526-DB453FE990D1(String), 2017090822(String)
org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@40e1df78]

第四步
org.mybatis.spring.SqlSessionUtils - Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@40e1df78] from current transaction
c.f.p.sharding.repayment.mapper.InvestRepaymentShardingMapper.selectByCondition - ooo Using Connection [com.dangdang.ddframe.rdb.sharding.jdbc.core.connection.ShardingConnection@5950a15f]
c.f.p.sharding.repayment.mapper.InvestRepaymentShardingMapper.selectByCondition - ==>  Preparing: select ID, CURRENTPERIOD, REPAYAMOUNT, REPAYDATE, STATUS, SOURCE_ID, SOURCE_REALM, AMOUNTINTEREST, AMOUNTOUTSTANDING, AMOUNTPRINCIPAL, DUEDATE, INVEST_ID, USER_ID, LOAN_ID, RELATIVEPERIOD, CREATE_TIME, UPDATE_TIME, CATEGORY, TIMEFINISHED from TB_INVEST_REPAYMENT WHERE ( USER_ID = ? and TIMEFINISHED = ? ) 
c.f.p.sharding.repayment.mapper.InvestRepaymentShardingMapper.selectByCondition - ==> Parameters: UFDBFA9F-C8B0-499C-AE67-45FD7608858B(String), 2018010722(String)
org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@40e1df78]

方法结束
com.fengjr.p2p.settle.service.TestService.TestServiceImpl - stop
org.mybatis.spring.SqlSessionUtils - Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@72ca1a11]
org.mybatis.spring.SqlSessionUtils - Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@40e1df78]
org.mybatis.spring.SqlSessionUtils - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@72ca1a11]
org.mybatis.spring.SqlSessionUtils - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@72ca1a11]
org.mybatis.spring.SqlSessionUtils - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@40e1df78]
org.mybatis.spring.SqlSessionUtils - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@40e1df78]
org.springframework.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource
org.springframework.jdbc.datasource.DataSourceTransactionManager - Initiating transaction commit
org.springframework.jdbc.datasource.DataSourceTransactionManager - Committing JDBC transaction on Connection [jdbc:mysql://betadb.fengjr.inc:3306/asset?useUnicode=true&characterEncoding=UTF-8, [email protected], MySQL Connector Java]
org.springframework.jdbc.datasource.DataSourceTransactionManager - Releasing JDBC Connection [jdbc:mysql://betadb.fengjr.inc:3306/asset?useUnicode=true&characterEncoding=UTF-8, [email protected], MySQL Connector Java] after transaction
org.springframework.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource

从上面可以看出,
1.虽然assetNew上没是事务,但是
org.mybatis.spring.transaction.SpringManagedTransaction - JDBC Connection [com.dangdang.ddframe.rdb.sharding.jdbc.core.connection.ShardingConnection] will be managed by Spring
2.committing SqlSession不是提交事务!!!
Committing JDBC transaction才是。

你可能感兴趣的:(spring多数据源事务)