简体   繁体   English

Hibernate 未经检查的异常未回滚

[英]Hibernate Unchecked exception not rollbacked

I think i don't understand something with unchecked exception.我想我不明白未经检查的异常。

I'm using spring/hibernate.我正在使用弹簧/休眠。

In a transaction, i'm catching a NullPointerException (so an unchecked), but at then end my transaction is not rollbacked.在事务中,我正在捕获 NullPointerException(因此未选中),但最后我的事务没有回滚。

Is it because i catch the NPE inside the same method that throw the NPE?是因为我在抛出 NPE 的同一方法中捕获了 NPE? This is the only diffrent i see from an other part of the code where i throw an IllegalArgumentException that is catched in an other method from the one throwing it ( and which ends with a rollback. )这是我从代码的其他部分看到的唯一不同之处,我在其中抛出了一个 IllegalArgumentException,该异常在另一个方法中被抛出它的方法捕获(并且以回滚结束。)

Thank you,谢谢,

Spring will rollback the transaction if an uncaught exception goes through the transactional proxy.如果未捕获的异常通过事务代理,Spring 将回滚事务。

If you catch the exception inside the wrapped method, the transaction will not be rolled back automatically.如果您在包装方法中捕获异常,则事务不会自动回滚。

If you do need to catch the exception for some reason, you could either rethrow it, throw another one (one that makes sense considering the contract of the method) or rollback the transaction manually.如果您出于某种原因确实需要捕获异常,您可以重新抛出它,抛出另一个(考虑到方法的合同是有意义的)或手动回滚事务。 Keep in mind that this last option breaks the transparancy of spring.请记住,最后一个选项破坏了 spring 的透明度。

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

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