在 Java 中捕获 TransactionRolledbackLocalException
我在 Websphere 7 中从容器收到 javax.ejb.TransactionRolledbackLocalException,我想知道如何捕获此异常?我在 Websphere 中设置了超时,并在此时间后收到此消息。我运行会话 bean。
我正在尝试查找导致此异常的 SQl 语句。我在哪里可以找到那个?
I receive javax.ejb.TransactionRolledbackLocalException in Websphere 7 from the container and I wonder how is it possible to catch this exception? I have a timeout set in Websphere and get this message after this time. I run session beans.
I am trying to find what SQl statement was the cause of this exception. Where can i find that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据 Sun 的文档
这是底线指南:如果可以合理地预期客户端会从异常中恢复,则将其设置为已检查的异常。如果客户端无法执行任何操作来从异常中恢复,请将其设为未经检查的异常。
TransactionRolledbackLocalException 是一个未经检查的异常,并且存在如果发生这种情况你无能为力。你可以按照 Aaron Digulla 在他的回答中建议的那样抓住它,但这有什么意义呢?
如果你抓住了它,那么你就会弄乱应用程序服务器的内部结构。您将在客户端上收到异常,并且可以对客户端上收到的异常调用 getCause() 以正确通知用户。
你有两个解决办法
(可能是错误的 SQL)
As per Sun's docs
Here's the bottom line guideline: If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.
TransactionRolledbackLocalException is an unchecked exception and there is nothing you can do if it happens. You could catch it as Aaron Digulla suggests in his answer, but what is the point ?
If you catch it then you will be messing with the internals of the App Server. You will get an exception on the client and you can call getCause() on the exception you get on the client to properly notify the user.
You have two solutions
(Probably bad SQL)
当满足两个条件之一时,事务将回滚:
显然,您可以通过使用
try{}catch( 包装最外层代码来捕获案例#1 中的异常)
。但是你的哪段代码是在超时时执行的呢?除非您的应用服务器提供 API 来将侦听器附加到此类事件,否则这是不可能的。请参阅随产品一起收到的文档或致电支持人员了解详细信息。
[编辑] 如果您想查看实际导致超时的 SQL,您有两个选择:
您可以使用
java.sql.DriverManager.setLogWriter(w);
记录所有 SQL 语句到一个文件。虽然这总是有效,但它会创建大量输出,并且很难将其与异常相匹配,除非您可以确保您是唯一一个正在运行的请求。如果您使用 OR 映射器(例如 Hibernate 等),您可以为它们启用日志记录。请参阅此处了解 Hibernate.
A transaction is rolled back when one of two conditions are met:
Obviously, you can catch the exception in case #1 by wrapping the outermost code with a
try{}catch()
. But which code of yours is executed on timeout?Unless your app server offers an API to attach a listener to such events, this is not possible. See the documentation which you received with the product or call the support for details.
[EDIT] If you want to see the SQL which actually causes the timeout, you have two options:
You can use
java.sql.DriverManager.setLogWriter(w);
to log all SQL statements to a file. While this always works, it will create a lot of output and it will be hard to match this against the exception unless you can make sure you are the only one running requests.If you use an OR mapper (like Hibernate and such), you can enable logging for them. See here for Hibernate.
您还可以使用 [Log4JDBC] (https://code.google.com/p/log4jdbc/< /a>)。
这允许在驾驶员面前进行登录。
它的工作方式是像代理一样在数据源中指定它。
当然,如果您使用 hibernate,设置 show sql 属性会更简单。
但如果您使用 JDBC,这将起作用,因为所有查询都经过这里。
You can use also the [Log4JDBC] (https://code.google.com/p/log4jdbc/).
This allows for logging in front of the driver.
The way it works is that you specify it in the datasource like a proxy.
Of course that if you are using hibernate it is simpler to set the show sql property.
But if you use JDBC this will work because all query's go through here.