如何停止记录已处理的异常?

发布于 2024-09-25 14:22:33 字数 1789 浏览 1 评论 0原文

我刚刚为 JPA 实体的唯一约束实现了异常处理。它按照我想要的方式工作,但是当触发时将处理的异常转储到容器日志文件中。

  • JPA 实体由 SLSB(服务外观)管理。服务外观是从另一个 SLSB 调用的,该 SLSB 提供基于 JAX-RS 的远程处理功能。
  • 在服务外观中,EntityManager 操作被包装在 try-catch-块中,检测唯一约束违规的原因。然后它抛出一个自定义检查的ApplicationException
  • REST-Bean 捕获 ApplicationException 并抛出自定义的未经检查的 BadRequestException
  • ExceptionMapperBadRequestException 输出到远程客户端。

这一切都运作良好。我不明白的部分是:(已处理的)异常被记录在容器的日志文件中(带有长堆栈跟踪):

[#|2010-09-29T18:49:39.185+0200|WARNING|glassfish3.0.1|org.eclipse.persistence.session.file:/Users/hank/NetBeansProjects/CoreServer/build/classes/_coreServerPersistenceUnit|_ThreadID=30;_ThreadName=Thread-1;|
Local Exception Stack: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry....
....
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry....

并抛出 BadRequestException:

[#|2010-09-29T18:49:39.336+0200|WARNING|glassfish3.0.1|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=30;_ThreadName=Thread-1;|A system exception occurred during an invocation on EJB ShopperResource method public javax.ws.rs.core.Response mvs.gateway.ShopperResource.create(javax.xml.bind.JAXBElement)
javax.ejb.EJBException
at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:5119)
....
Caused by: mvs.api.exception.BadRequestException: mvs.api.exception.MvsCause: Field 'MSISDN' must be unique!

这是应该的样子吗?我想既然我处理了异常,它们就不会被转储到日志中吗?

I have just implemented exception handling for a unique-constraint of a JPA entity. It is working as I want it to, but when triggered dumps the handled exceptions to the container logfile.

  • A JPA entity is managed by a SLSB (Service Façade). The Service Façade is called from another SLSB, which provides remoting capabilities based on JAX-RS.
  • In the Service Façade, the EntityManager operations are wrapped in a try-catch-block, detecting the cause of the unique-constraint-violation. It then throws a custom checked ApplicationException.
  • The REST-Bean catches the ApplicationException and throws a custom unchecked BadRequestException.
  • An ExceptionMapper outputs the BadRequestException to the remote client.

This is all working well. The part that I don't understand is: the (handled) exceptions get logged in the container's logfile (complete with a long stacktrace):

[#|2010-09-29T18:49:39.185+0200|WARNING|glassfish3.0.1|org.eclipse.persistence.session.file:/Users/hank/NetBeansProjects/CoreServer/build/classes/_coreServerPersistenceUnit|_ThreadID=30;_ThreadName=Thread-1;|
Local Exception Stack: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry....
....
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry....

and from throwing the BadRequestException:

[#|2010-09-29T18:49:39.336+0200|WARNING|glassfish3.0.1|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=30;_ThreadName=Thread-1;|A system exception occurred during an invocation on EJB ShopperResource method public javax.ws.rs.core.Response mvs.gateway.ShopperResource.create(javax.xml.bind.JAXBElement)
javax.ejb.EJBException
at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:5119)
....
Caused by: mvs.api.exception.BadRequestException: mvs.api.exception.MvsCause: Field 'MSISDN' must be unique!

Is this how it should be? I thought since I handle the exceptions, they wouldn't be dumped to the log?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

⊕婉儿 2024-10-02 14:22:33

由于您启用了异常日志记录,因此会记录异常。

当您的日志级别为“警告”或更高级别时,默认情况下会记录异常。如果您将日志级别设置为 SEVERE 或 OFF,则不会记录它们。

IE
"eclipselink.logging.level"="SEVERE"

您还可以设置“eclipselink.logging.exceptions”="false" 属性以仅禁用异常日志记录。

看,
http://wiki.eclipse.org/EclipseLink/Examples/JPA/Logging

The exceptions are logged because you have exception logging enabled.

Exceptions get logged by default when your log level is WARNING or greater. If you set your log level to SEVERE or OFF then they will not be logged.

i.e.
"eclipselink.logging.level"="SEVERE"

You can also set the "eclipselink.logging.exceptions"="false" property to disable just exception logging.

See,
http://wiki.eclipse.org/EclipseLink/Examples/JPA/Logging

友谊不毕业 2024-10-02 14:22:33

数据库层负责记录异常。当你捕获它们时,它们已经被写入日志中。

It's the database layer that does the logging of the exceptions. The time you catch them they are already written to the log.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文