学习Spring事务操作-学习
This commit is contained in:
@@ -10,23 +10,19 @@ import java.sql.SQLException;
|
||||
|
||||
@Service
|
||||
public class DeptService {
|
||||
|
||||
@Autowired
|
||||
private DeptDao1 deptDao1;
|
||||
@Autowired
|
||||
private DeptDao2 deptDao2;
|
||||
|
||||
@Transactional(rollbackFor = RuntimeException.class)
|
||||
public void update() throws SQLException {
|
||||
deptDao1.insert("b1--");
|
||||
deptDao2.insert("b2--");
|
||||
}
|
||||
|
||||
// @Transactional 自调用(实际上是目标对象内的方法调用目标对象的另一个方法)在运行时不会导致实际的事务
|
||||
// 所以想要让本方法得到调用就得重新写 @Transactional 原方法的失效
|
||||
@Transactional
|
||||
public void test() throws SQLException {
|
||||
update();
|
||||
// @Transactional
|
||||
// public void test(){
|
||||
// update();
|
||||
// }
|
||||
|
||||
@Autowired
|
||||
Manager m;
|
||||
|
||||
// 调用对象的情况哪怕本方法不写注解也没关系
|
||||
public void test(){
|
||||
m.update();
|
||||
}
|
||||
}
|
||||
// 事务传播
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.ycjy.service;
|
||||
|
||||
import com.ycjy.dao.DeptDao1;
|
||||
import com.ycjy.dao.DeptDao2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class Manager {
|
||||
@Autowired
|
||||
private DeptDao1 deptDao1;
|
||||
@Autowired
|
||||
private DeptDao2 deptDao2;
|
||||
|
||||
@Transactional(rollbackFor = RuntimeException.class)
|
||||
public void update(){
|
||||
deptDao1.insert("b1--");
|
||||
deptDao2.insert("b2--");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user