@Transactional
public void save(User user) throws Exception{
try {
for (int i = 0;i < 5;i++){
apiService.createUser();//在权限系统创建用户以http形式
user.setUserName(“”);
user.setPassWord(null);
userDao.save(user);
System.out.print("继续执行"+i);
}
} catch (Exception e) {
System.out.print("service进来了");
}
公司 架构是spring+spring data jpa+springmvc
在执行save方法时
问题一:理论上在循环第一次已经异常不应该继续往下执行,但是会继续循环五次
问题二:在service中不会进入catch,而在controller中可以,如果我在循环中throw new RuntimeException()异常service进入catch,如果我删除事务注解,没问题进入catch.
感觉很奇怪,求指点
巴扎黑2017-04-18 10:35:55
What is the abnormal information? The transaction was executed using an agent, and an error may have occurred during the agent process. In this way, errors cannot be captured in the service and are thrown to the controller
ringa_lee2017-04-18 10:35:55
I forgot to reply. In fact, spring data jpa caches the data when executing the save operation. If it is not submitted to the database, it needs to be flushed to catch the exception