简体   繁体   English

在@Transactional REST服务方法中捕获Hibernate Persistence Exceptions

[英]Catch Hibernate Persistence Exceptions in @Transactional REST service methods

We're building a REST service with Spring and JPA where each method is annotated @Transactional , since one REST call is one transaction. 我们正在使用Spring和JPA构建REST服务,其中每个方法都注释了@Transactional ,因为一个REST调用是一个事务。 Now if say a user is trying to call delete on a resource that has dependencies, hibernate will throw a PersistenceException , causing the REST server call to return with status 500. 现在,如果用户试图在具有依赖关系的资源上调用delete,则hibernate将抛出PersistenceException ,导致REST服务器调用以状态500返回。

We want to catch those exceptions and wrap them in our own error object, that will contain custom error codes as well as the orignial message. 我们希望捕获这些异常并将它们包装在我们自己的错误对象中,该对象将包含自定义错误代码以及orignial消息。

I tried just plain catching all RuntimeExceptions in the REST call and then returning our custom error object. 我试着在REST调用中捕获所有RuntimeExceptions ,然后返回我们的自定义错误对象。 That however won't work, as appearently the fact that the transaction has been aborted will get handled outside my method. 然而,这将无法正常工作,因为显然交易已中止的事实将在我的方法之外处理。

I also tried to catch the PersistenceException directly at DAO level. 我还尝试直接在DAO级别捕获PersistenceException This worked fine in my unit tests didn't change anything outside them. 这在我的单元测试中运行良好并没有改变他们之外的任何东西。 Also our web container seems to just do its own thing by throwing a DataIntegrityViolationException instead of a PersistenceException . 此外,我们的Web容器似乎只是通过抛出DataIntegrityViolationException而不是PersistenceException来做自己的事情。 That doesn't even appear in the trace. 这甚至没有出现在痕迹中。 As a side note, that's rather confusing, as I thought Hibernate would take care of its own stuff. 作为旁注,这是相当令人困惑的,因为我认为Hibernate会照顾自己的东西。 Why is spring interfering? 为什么春天会干扰?

Well anyway.. I also tried to use 好吧无论如何..我也试过用

TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

inside the REST service method with no success. 在REST服务方法内部没有成功。

I'm at a loss here.. It seems error handling for a transaction started by @Transactional is just out of my hands..?! 我在这里不知所措..似乎@Transactional启动的交易的错误处理只是出于我的手......?! Is it maybe because the annotation creates a proxy that gets processed in a different scope? 是否可能是因为注释创建了一个在不同范围内处理的代理?

Is the only option to handle transactions manually with the TransactionTemplate ? 是使用TransactionTemplate手动处理事务的唯一选项吗?

I expect that the exception will be thrown when one tries to commit the transaction. 我希望在尝试提交事务时抛出异常。 So you will not get the exception as long as you are inside of the transaction. 因此,只要您处于事务内部,就不会获得异常。

Spring will do something called Exception-Translation, this will "translate" some (vendor) specific exceptions into some other Spring Exceptions. Spring将执行一个名为Exception-Translation的操作,这会将一些(供应商)特定的异常“转换”为其他一些Spring Exceptions。 I think this is the reason why you will notice the DataIntegrityViolationException 我认为这就是你会注意到DataIntegrityViolationException的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM