java 如何处理休眠异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5725419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:29:39  来源:igfitidea点击:

how to handle hibernate exceptions

javahibernateexception-handling

提问by barbiegows

I have few questions like

我有几个问题,比如

  1. Which is the right place to handle a Hibernate RuntimeException. DAO Layer? ServiceLayer?

  2. For instance, I have saveCustomerand the transaction fails, how can I inform the user through Exceptions?

  3. I'm using the OpenSessionInViewpattern, which will commit the transaction only after the view is rendered. In this case, if the transaction fails, How can I inform the user about this?

  1. 这是处理 Hibernate 的正确位置RuntimeException。DAO层?服务层?

  2. 例如,我有saveCustomer交易失败,我如何通过 通知用户Exceptions

  3. 我正在使用OpenSessionInView模式,它只会在呈现视图后提交事务。在这种情况下,如果交易失败,我该如何通知用户?

回答by A Lee

  1. Generally it's a good idea to handle exceptions at the point in your code where you have the most information on how to handle them (or generate an appropriate error message with enough information). In the j2ee apps I've developed I usually have a transactional service layer that stitches together various DAO calls and I usually handle hibernate-specific runtime exceptions and all other data-related exceptions in the service layer. That said, if there's a pile of logic within a DAO where something bad could go wrong, it's probably not a bad idea idea to catch it there, add some error message context, and then rethrow it up the chain.

  2. If an exception happens within your transaction you can either leave it uncaught or rethrow it with some additional context. This way your transaction manager knows to rollback the transaction. Always have an exception handler set up in your view/controller layer to handle any application-specific runtime exceptions that were thrown from your service layer calls. Inspect them for additional error messages and then use them to inform the user appropriately.

  3. As far as I know, the default behavior when an exception is thrown out of a transaction it should rollback and not get committed at all. The answer to #2 really answers this one as well, i.e., if you've wrapped all your view layer's service calls in try/catch blocks or configure one globally via your web frameworkyou shouldn't have a problem notifying the user that something bad-wrong happened.

  1. 通常,最好在代码中有关如何处理异常的信息最多的地方处理异常(或生成具有足够信息的适当错误消息)。在我开发的 j2ee 应用程序中,我通常有一个事务服务层,它将各种 DAO 调用拼接在一起,我通常在服务层中处理特定于休眠的运行时异常和所有其他与数据相关的异常。也就是说,如果 DAO 中有一堆逻辑可能会出错,那么在那里捕获它,添加一些错误消息上下文,然后将其重新抛出链上可能不是一个坏主意。

  2. 如果在您的事务中发生异常,您可以不捕获它或使用一些额外的上下文重新抛出它。这样您的事务管理器就知道回滚事务。始终在您的视图/控制器层中设置一个异常处理程序,以处理从您的服务层调用中抛出的任何特定于应用程序的运行时异常。检查它们是否有其他错误消息,然后使用它们适当地通知用户。

  3. 据我所知,从事务中抛出异常时的默认行为应该回滚并且根本不提交。#2 的答案也确实回答了这个问题,即,如果您已将所有视图层的服务调用包装在 try/catch 块中或通过您的 Web 框架全局配置一个,您应该不会有问题通知用户某些事情坏事发生了。

This article (Best Practices for Exception Handling)has a decent overview on exception handling in general that you may also find useful.

这篇文章(异常处理的最佳实践)对一般的异常处理有一个不错的概述,您可能也会发现它很有用。

回答by Deepak

Catch the exception where you can do something with the exception. If you catch it in DAO layer, then the information specific to dao layer needs to be extracted. e.g. if it is can not insert null value, then log the field details where it is failing. Once done, then the service layer can do business related processing of the exception directly or in wrapped format. You can see these common mistakes in exception handling. This is not applicable to specific hibernate examplebut in general to all.

捕获异常,您可以对异常执行某些操作。如果在DAO层捕获,则需要提取dao层特有的信息。例如,如果无法插入空值,则记录失败的字段详细信息。一旦完成,那么服务层可以直接或以包装格式对异常进行业务相关的处理。您可以在异常处理中看到这些常见错误。这不适用于特定的休眠示例,但通常适用于所有示例