【发布时间】:2016-04-06 21:50:53
【问题描述】:
修改bean属性不使用spring @Transactional回滚
TestController.java
@RestController
public class TestController {
@Autowired
private TestService testService;
@Autowired
private TestBean testBean;
@RequestMapping(value = "/test")
public String testTransaction() {
try{
testService.testTransaction();
}catch(Exception e){
System.out.println("After exception: " + testBean.getAttribute());
}
return "test"
}
}
TestService.java
@Service
public class TestService {
@Autowired
private TestBean testBean;
@Transactional(rollbackFor = Exception.class)
public void testTransaction() {
testBean.increment();
System.out.println("Before exception: " + testBean.getAttribute());
throw new UnexpectedRollbackException("unexpected exception");
}
}
TestBean.java
@Component
public class TestBean {
private int attribute = 0;
public TestBean() {
}
public void increment () {
attribute++;
}
public int getAttribute() {
return attribute;
}
}
控制台日志
Before exception: 1
After exception: 1
我想知道为什么属性值没有回滚到 0(它的初始值)。
【问题讨论】:
标签: java spring transactions spring-transactions transactional