My project encountered the problem that Spring JPA transactions would not roll back. The project framework is spring MVC + Spring + spring data JPA + oracle. I searched a lot of information on the Internet, but still could not solve my problem.
I've tried many ways, such as setting the method to public or adding rollbackFor = Exception.class
in @Transactional
, but it still can't be solved.
Here's my code
Controller
@RequestMapping(value = {"addUser"}, method = RequestMethod.GET)
@ResponseBody
public Boolean insertUser() throws Exception{
User user = new User();
user.setId(10);
userServiceI.addUser(user);
return true;
}
Service
Service Interface
public interface UserServiceI {
void addUser(User user);
}
Service Implementation class
@Service
public class UserService implements UserServiceI {
@Autowired
public UserDao userDao;
@Autowired
PersonService personService;
@Override
@Transactional(propagation= Propagation.REQUIRED,rollbackFor=Exception.class)
public void addUser(User user){
User user1 = userDao.saveAndFlush(user);
System.out.println(1/0);
}
}
Dao
public interface UserDao extends JpaRepository<User,Integer> {
}
My @Transactional
method loads my implementation class Service, which writes an error-prone 1/0. I expect the transaction to roll back after the error, but it doesn't.