PROPAGATION_REQUIRES_NEW传播方式对于第二个事务,会产生一个全新的Connection来处理.两个连接的操作类似于:
- Connection con1 = ds.getConnection();
- con1.setAutoCommit(false );
- con1.executeUpdate(sql1);
- con1.executeUpdate(sql2);
- ...
- Connection con2 = ds.getConnection();
- con2.setAutoCommit(false );
- con2.executeUpdate(sql1);
- con2.exeucteUpdate(sql2);
- ...
- con2.commit();
- con2.setAutoCommit(true );
- con2.close();
- con1.executeUpdate(sqln);
- ...
- con1.commit();
- con1.setAutoCommit(false );
- con1.close();<pre></pre>
Spring事务管理中事务涉及的变量主要有:

变量在两事务(第一个事务为PROPAGATION_REQUIRED,第二个事务为PROPAGATION_REQUIRES_NEW)创建、执行、完成过程中的变化如下:
| 第一个事务 | ||||||||
| TransStatus1 | Transaction1 | ConnectionHolder1 | ||||||
| Transaction | Transaction1 | ConnectionHolder | ConnectionHolder1 | SynchronizedWithTransaction | TRUE | |||
| newTransaction | TRUE | Connection | con1 | |||||
| newSynchronization | TRUE | resource | ||||||
| suspendedResources | null | key | dataSource | Synchronizations | ||||
| savepoint | null | value | ConnectionHolder1 | ArrayList1 | empty | |||
| 第二个事务:doGetTransaction | ||||||||
| Transaction2 | ||||||||
| ConnectionHolder | ConnectionHolder1 | |||||||
| doSuspendTransaction | ||||||||
| Transaction2 | SuspendedResourcesHolder1 | |||||||
| ConnectionHolder | null | suspendedSynchronizations | ArrayList1 | |||||
| suspendedResources | ConnectionHolder1 | |||||||
| resource | ||||||||
| null | Synchronizations | |||||||
| null | ||||||||
| doBegin | ||||||||
| TransStatus2 | Transaction2 | ConnectionHolder2 | ||||||
| Transaction | Transaction2 | ConnectionHolder | ConnectionHolder2 | SynchronizedWithTransaction | TRUE | |||
| newTransaction | TRUE | Connection | con2 | |||||
| newSynchronization | TRUE | resource | ||||||
| suspendedResources | SuspendedResourcesHolder1 | key | dataSource | Synchronizations | ||||
| value | ConnectionHolder2 | ArrayList2 | empty | |||||
| savepoine | null | |||||||
| triggerBeforeCommit | triggerBeforeCompletion | doCommit:con2.commit | triggerAfterCompletion | |||||
| doCleanupAfterCompletion | ||||||||
| con2恢复autoCommit与isolationLevel | resource | Synchronizations | ||||||
| 关闭连接 | null | null | ||||||
| resume | ||||||||
| 已经恢复到第一个事务doBegin刚完成状态 | resource | Synchronizations | ||||||
| key | dataSource | ArrayList1 | empty | |||||
| value | ConnectionHolder1 | |||||||
源码:
- public void addEmployee(Employee employee) throws SQLException {
- transactionTemplate.execute(new TransactionCallback() {
- public Object doInTransaction(TransactionStatus ts) {
- try {
- jdbcTemplate.update("INSERT INTO Employee (username,age) VALUES(?, ?)" ,
- new Object[]{ "lizi" , new Integer( 22 )});
- departmentDao.addDepartment();
- jdbcTemplate.update("INSERT INTO Employee (username,age) VALUES(?, ?)" ,
- new Object[]{ "lijun" , new Integer( 55 )});
- System.out.println("更新成功" );
- } catch (Exception ex) {
- ex.printStackTrace();
- System.out.println("更新失败" );
- ts.setRollbackOnly();
- }
- //ts.setRollbackOnly();
- return null ;
- }
- });
- }
- //DepartmentDao.java
- public void setTransactionTemplate(
- TransactionTemplate deptTransactionTemplate) {
- this .transactionTemplate = deptTransactionTemplate;
- deptTransactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
- DataSourceTransactionManager manager = (DataSourceTransactionManager)deptTransactionTemplate.getTransactionManager();
- DataSource ds = manager.getDataSource();
- jdbcTemplate = new JdbcTemplate(ds);
- }
- public void addDepartment() {
- transactionTemplate.execute(new TransactionCallback() {
- public Object doInTransaction(TransactionStatus status) {
- try {
- jdbcTemplate.update("insert into department (dept,deptName) values('IT','IT department')" );
- } catch (Exception ex) {
- ex.printStackTrace();
- status.setRollbackOnly();
- }
- return null ;
- }
- });
- }
本文详细探讨了Spring框架中PROPAGATION_REQUIRED与PROPAGATION_REQUIRES_NEW两种事务传播级别的具体实现方式及其内部机制。通过具体的Java示例代码,展示了这两种事务传播级别下,如何创建独立的数据库连接以及它们之间的交互过程。
865

被折叠的 条评论
为什么被折叠?



