I have an entity User
, a Repository/Dao class UserDao
(using Spring Data JPA) and a Service class UserService
with a method addUser
annotated as @Transactional
:
@Service
public class UserService {
@Autowired
private UserDao userDao;
@Transactional
public void addUser() throws Exception {
User user = new User();
user.setUsername("aaa");
// Save the user, but since this method have the @Transactional
// annotation it should not be committed....
userDao.save(user);
// Forcing an error here I expected that the previous operation
// were rolled back.. Instead the user is saved in the db.
if ("".equals("")) {
throw new Exception("something fails");
}
// Other operations (never executed in this example)
user.setUsername("bbb");
userDao.save(user);
return;
} // method addUser
} // class UserService
The UserDao
is simply this:
@Transactional
public interface UserDao extends CrudRepository<User, Long> { }
Reading the Spring Data JPA documentation and other questions on the same argument (1, 2) my expectations were that each operations inside a method marked with @Transactional
will be rolled back if some error occurs..
What am I doing wrong? Is there a way for rollback the save operation in the previous example if an error occurs?