I was having some trouble making transactions work properly in my play application, and my code is structured in such a way that I cannot afford the time to do it “properly” as it shows in the Play Framework documentation.
Only using @Transactional was not enough because for some reason it was not rolling back when I threw my own exceptions.
So this is the way I managed to make transactions work:
@Transactional
public Result myAction() {
try {
return ok(Json.toJson(myServiceClass.doStuff()));
} catch (BusinessRuleException e) {
return badRequest(Json.toJson("error"));
}
}
...
public class MyServiceClass {
public Bill doStuff() throws BusinessRuleException {
return jpaApi.withTransaction(() -> {
//...do a bunch of stuff here
});
}
}
Is this fine? Can it cause problems later for some reason?