【发布时间】:2018-04-29 09:44:37
【问题描述】:
我最近遇到了 play framework 2 @Transactional 的问题。根据我的测试,在发生异常的情况下,只有在未检查异常(没有 catch 块)时,事务方法才会回滚。 这是我的控制器:
@Transactional
public Result myController(){
ObjectNode result = Json.newObject();
try{
JsonNode json = request().body().asJson();
someFunction(json);
//doing some stuff using the json object inside someFunction
//which I may intentionally throw an exception
//based on some logic from within
//(using "throw new RuntimeException()")
result.put("success", true);
return ok(Json.toJson(result));
}catch(Exception e){
result.put("success", false);
result.put("msg", e.getMessage());
return internalServerError(Json.toJson(result));
}
}
我希望我的控制器始终返回 JSON 作为响应。但这是以我在代码中抛出异常时没有数据库回滚为代价的。我知道在春天你可以将它添加到@Transactional 注释中,但我使用的是 play.db.jpa.Transactional。有什么方法可以在不使用弹簧的情况下在我的 catch 块中回滚?
【问题讨论】:
标签: java hibernate jpa playframework transactions