声明式事务控制方式 (@Transactional) 将DB访问封装到 @Service/@Component 类中, 并将具体访问过程放到一个 public 方法中, 并加上 @Transactional...注意自调用问题, @Transactional 注解仅在外部类的调用才生效, 原因是使用 Spring AOP 机制造成的....关于自调用问题和 Public 的限制, 是因为Spring 使用了 Spring AOP 代理造成的, 如果要解决这两个问题, 使用 AspectJ 取代 Spring AOP 代理....示例: 单个 Service 类的多个事务函数调用问题
Class ServiceImpl{ @Autowired Dao dao;
// 因为自调用问题, 直接调用 test()..., 调用本类事务标注方法, 回滚 */ @Transactional public void runRollbackCase6() throws SQLException {