如果在同一个service类中定义的两个方法, 内层REQUIRES_NEW并不会开启新的事务,save和update中回滚都会导致整个事务的回滚
如果在不同的service中定义的两个方法, 内层REQUIRES_NEW会开启新的事务,并且二者独立,事务回滚互不影响
测试:以下代码定义在UserService2中
/**
@Transactional(propagation = Propagation.REQUIRED)
@Override
public void save(UserRecord userParam) {
logger.info("开始执行 save {}, {}", userParam.getId(), userParam.getPhone());
userMapper.save(userParam);
try {
logger.info("save 完成---数据库中的值为 {} 开始 sleep", userParam.getId());
Thread.sleep(1000);
this.update(userParam);
// throw new RuntimeException();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Override
public Integer update(UserRecord userParam) {
logger.info("开始执行 update {}, {}", userParam.getId(), userParam.getPhone());
int result = userMapper.update(userParam);
try {
logger.info("update 完成---数据库中的值为 {} 开始 sleep");
Thread.sleep(1000 * 10);
logger.info("sleep结束");
// throw new RuntimeException();
} catch (InterruptedException e) {
e.printStackTrace();
}
return result;
}
2017-12-24 08:46:43 [http-nio-9082-exec-3] INFO c.c.service.impl.UserServiceImpl2 - 开始执行 save null, 15210712345
2017-12-24 08:46:43 [http-nio-9082-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
2017-12-24 08:46:43 [http-nio-9082-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@65e040df]
2017-12-24 08:46:43 [http-nio-9082-exec-3] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [com.mysql.jdbc.JDBC4Connection@665e820d] will be managed by Spring
2017-12-24 08:46:43 [http-nio-9082-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@65e040df]
2017-12-24