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();
-
}
-
-
return
null
;
-
}
-
});
-
}
-
-
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
;
-
}
-
-
});
-
}
分享到:
相关推荐
PROPAGATION_REQUIRES_NEW--新建事务,如果当前存在事务,把当前事务挂起。 PROPAGATION_NOT_SUPPORTED--以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。 PROPAGATION_NEVER--以非事务方式执行,如果...
loopy belief propagation 算法
Affinity_Propagation_(AP)_AP聚类聚类算法介绍
Cap01_Introduction to Wireless Propagation_信道建模_matlab_源码.rar.rar
Spring在TransactionDefinition接口中规定了7种类型的事务传播行为,它们规定了事务方法和事务方法发生嵌套调用时事务...PROPAGATION_REQUIRES_NEW PROPAGATION_NOT_SUPPORTED PROPAGATION_NEVER PROPAGATION_NESTED
2、Propagation.REQUIRES_NEW 无论何时自身都会开启事务 3、Propagation.SUPPORTS 自身不会开启事务,在事务范围内则使用相同事务,否则不使用事务 4、Propagation.NOT_SUPPORTED 自身不会开启事务,...
Cap01_Introduction to Wireless Propagation_信道建模_matlab_源码.zip
propagation script for lasers
wave propagation_大气通信_大气湍流仿真_光通信仿真_大气湍流_大气湍流多步仿真程序_源码.zip
wave propagation_大气通信_大气湍流仿真_光通信仿真_大气湍流_大气湍流多步仿真程序.zip
simple back propagation
模拟真空中Gauss光束的三维场分布,方便加深高斯光束性质的理解
激光光束在介质中传播的分析及仿真,可仿真光束传输特性,程序运行结果较为直观,
Fundamentals of Antennas and Wave propagation Solutions for solving problems in standard textbooks.
关于信道测量和天线方面的一本书,很经典。
Error Back-Propagation Algorithm for MLP Neural Networks
Back Propagation Neuual Networks
simulation of Wireless Propagation
Script that solves and plot pulse propagation thru Split step Fourier method agrawal book
BP(back propagation)神经网络是1986年由Rumelhart和McClelland为首的科学家提出的概念,是一种按照误差逆向传播算法训练的多层前馈神经网络,是目前应用最广泛的神经网络。