I'm triyng to use @Transational in a method but i'm facing the following problem.
First this is the scenario:
1- I update his password
2- I insert his id in a tracking table(simple insert query)
To test my transaction, I give an existing id (primary key) in the insert query.
The transaction works but I get this error Transaction silently rolled back because it has been marked as rollback-only knowing that the two functions are
inside try-catch block and normally it should return Internal error.
When I use the breakpoint on the response it shows the internal error but in Postman I get Transaction silently rolled back because it has been marked as rollback-only.
This is my code:
import javax.transaction.Transactional;
@Transactional
public UpdateUserPasswordResponse updateUserPassword(String login, String pwd, String newPwd)
throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException,
UnsupportedEncodingException {
GetUserByUsernameAndPasswordResponse responseLogin = new GetUserByUsernameAndPasswordResponse();
UpdateUserPasswordResponse response = new UpdateUserPasswordResponse();
// some code
if (responseLogin.getUser() != null) {
//some code
try {
// call the update query from DAO
int lineUpdated = dao.updateUserPassword(login, Cryptedpwd);
int lineInserted = dao.insertUtilisateurTracking(responseLogin.getUser().getId(), new Date());
response.setMessage(Constants.SUCCESS);
response.setStatus(Constants.OK);
} catch (Exception e) {
response.setMessage(Constants.ERREUR_INTERNE);
response.setStatus(Constants.KO);
}
} else {
if (responseLogin.getMessage().equals(Constants.LOGIN_PASSWORD_INCORRECT)) {
response.setStatus(Constants.KO);
response.setMessage(Constants.LOGIN_PASSWORD_INCORRECT);
} else {
response.setMessage(Constants.ERREUR_INTERNE);
response.setStatus(Constants.KO);
}
}
return response;
}
@Transactionalannotation will do for you. - Alain Cruz@Transactional(rollbackFor = Exception.class). This force to rollback only if an Exception is thrown, might work, but not sure. - Alain Cruz