分布式事务——Spring.ChainedTransactionManager

代码:https://github.com/JPCui/demos/tree/master/demo-chained-transactionmanager


1. 因为之前用过spring-data-neo4j,里面的事务使用了分布式事务的配置;

参考:http://docs.spring.io/spring-data/data-neo4j/docs/3.0.x/reference/htmlsingle/#reference_programming-model_transactions

2. 这里使用的是spring jdbc的配置方案;


下面是回滚测试的日志,配置了两个数据源(两个事务交由ChainedTransactionManager管理),从中可以看出,ChainedTransactionManager是开启了两个事务,同时开启,同时提交(或回滚)。既然是这样的话,也没什么高级的,我们也可以自定义自己的DT(Distribute Transaction)框架

2015-12-01  14:12:19.316 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'chainedTransactionManager'
2015-12-01  14:12:19.323 [main] DEBUG org.springframework.jdbc.datasource.DataSourceTransactionManager - Creating new transaction with name [com.it_sucre.service.TestService.test]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '',-java.lang.Exception
2015-12-01  14:12:19.324 [main] DEBUG org.springframework.jdbc.datasource.DriverManagerDataSource - Creating new JDBC DriverManager Connection to [jdbc:mysql://127.0.0.1:3308/Test?characterEncoding=UTF-8]
2015-12-01  14:12:19.650 [main] DEBUG org.springframework.jdbc.datasource.DataSourceTransactionManager - Acquired Connection [com.mysql.jdbc.JDBC4Connection@6f53b8a] for JDBC transaction
2015-12-01  14:12:19.657 [main] DEBUG org.springframework.jdbc.datasource.DataSourceTransactionManager - Switching JDBC Connection [com.mysql.jdbc.JDBC4Connection@6f53b8a] to manual commit
2015-12-01  14:12:19.658 [main] DEBUG org.springframework.jdbc.datasource.DataSourceTransactionManager - Creating new transaction with name [com.it_sucre.service.TestService.test]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '',-java.lang.Exception
2015-12-01  14:12:19.659 [main] DEBUG org.springframework.jdbc.datasource.DriverManagerDataSource - Creating new JDBC DriverManager Connection to [jdbc:mysql://127.0.0.1:3308/Test?characterEncoding=UTF-8]
2015-12-01  14:12:19.680 [main] DEBUG org.springframework.jdbc.datasource.DataSourceTransactionManager - Acquired Connection [com.mysql.jdbc.JDBC4Connection@48e1f6c7] for JDBC transaction
2015-12-01  14:12:19.681 [main] DEBUG org.springframework.jdbc.datasource.DataSourceTransactionManager - Switching JDBC Connection [com.mysql.jdbc.JDBC4Connection@48e1f6c7] to manual commit
2015-12-01  14:12:19.722 [main] DEBUG org.springframework.jdbc.core.JdbcTemplate - Executing prepared SQL update
2015-12-01  14:12:19.723 [main] DEBUG org.springframework.jdbc.core.JdbcTemplate - Executing prepared SQL statement [update test set name=?]
2015-12-01  14:12:19.745 [main] DEBUG org.springframework.jdbc.core.JdbcTemplate - SQL update affected 15 rows
2015-12-01  14:12:19.752 [main] DEBUG org.springframework.jdbc.core.JdbcTemplate - Executing SQL query [select name from test]
2015-12-01  14:12:19.757 [main] DEBUG org.springframework.jdbc.datasource.DataSourceTransactionManager - Initiating transaction rollback
2015-12-01  14:12:19.757 [main] DEBUG org.springframework.jdbc.datasource.DataSourceTransactionManager - Rolling back JDBC transaction on Connection [com.mysql.jdbc.JDBC4Connection@48e1f6c7]
2015-12-01  14:12:19.782 [main] DEBUG org.springframework.jdbc.datasource.DataSourceTransactionManager - Releasing JDBC Connection [com.mysql.jdbc.JDBC4Connection@48e1f6c7] after transaction
2015-12-01  14:12:19.782 [main] DEBUG org.springframework.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource
2015-12-01  14:12:19.785 [main] DEBUG org.springframework.jdbc.datasource.DataSourceTransactionManager - Resuming suspended transaction after completion of inner transaction
2015-12-01  14:12:19.785 [main] DEBUG org.springframework.jdbc.datasource.DataSourceTransactionManager - Initiating transaction rollback
2015-12-01  14:12:19.785 [main] DEBUG org.springframework.jdbc.datasource.DataSourceTransactionManager - Rolling back JDBC transaction on Connection [com.mysql.jdbc.JDBC4Connection@6f53b8a]
2015-12-01  14:12:19.787 [main] DEBUG org.springframework.jdbc.datasource.DataSourceTransactionManager - Releasing JDBC Connection [com.mysql.jdbc.JDBC4Connection@6f53b8a] after transaction
2015-12-01  14:12:19.787 [main] DEBUG org.springframework.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource
2015-12-01  14:12:19.787 [main] DEBUG org.springframework.jdbc.datasource.DataSourceTransactionManager - Resuming suspended transaction after completion of inner transaction


你可能感兴趣的:(数据库,spring,java,源码)