如何捕获c3p0和JPA异常?
我得到了这个应用程序,用户必须手动连接数据库,
EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistenceUnit", connectionProperties);
当用户/密码不正确或主机超时由于错误的 url C3P0 中断连接时,
我用这一行执行此操作,然后我在控制台中得到这些
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask, 1853 - A RESOURCE POOL IS PERMANENTLY BROKEN! [com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@18e3f02a]
问题:我可以' t 捕获该异常,以便我可以将其转换为屏幕上用户友好的消息,就像始终创建 emf 一样,我无法判断何时损坏或未损坏
更新:我在 Hibernate http://docs.jboss.org /hibernate/entitymanager/3.5/reference/en/html/transactions.html#transactions-demarcation-exceptions
但我用每一次尝试包围了我的 emf抓住可能但仍然抓不到东西
I got this app where the user must connect the DB manually, I do that with this line
EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistenceUnit", connectionProperties);
when the user/password is incorrect or the host timeouts due to wrong url C3P0 breaks the connection
then I get these in console
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask, 1853 - A RESOURCE POOL IS PERMANENTLY BROKEN! [com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@18e3f02a]
THE PROBLEM: I can't capture that exception so I can translate it to a user friendly message on screen, it is like emf is always created and I got no way to tell when is broken or not
UPDATE: I found on Hibernate http://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/transactions.html#transactions-demarcation-exceptions
But I surrounded my emf with EVERY single try catch possible and still can't catch a thing
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JPA 规范规定,如果持久性单元无法实例化,Persistence.createEntityManagerFactory 将返回 null(并且没有说明任何可能的异常)。因此,它就像巧克力茶壶一样用于以编程方式检测原因。向编写 JPA 规范的人抱怨。
The JPA spec says that Persistence.createEntityManagerFactory will return null if a persistence-unit can't be instantiated (and states nothing about possible exceptions). Consequently it's as much use as a chocolate teapot for detecting the reason programmatically. Complain to the people who wrote the JPA spec.
我知道这是一个旧线程。
但我试图做和你完全一样的事情。
我查了很多资料都没有找到答案,即使你很久以前发布了这个也没有得到答案。
嗯.. 发生的情况是,大多数 Hibernate 代码都会捕获异常并将其放入本地异常列表中。
例如,看一下 org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(),它捕获所有 jdbc 异常并将它们放入异常列表中。
不幸的是,使用此类的代码无法跟踪这些异常,因为不再引用这些对象。
虽然所有异常都会被记录..但我发现这种行为真的很烦人,因为我是一个控制狂。将异常放在列表中的想法对于框架来说非常好。但如果另一端不能使用它......这只是代码膨胀,它会让你浪费时间分析为什么你不能得到该死的异常(对于那些永不放弃的人)。
我所做的是对数据库执行一些基本的启动操作,当我收到查询异常时,我就知道出了问题。 Servlet 中的错误页面更加通用,但不会给用户留下系统有很多错误的印象(呵呵,这听起来是错误的)。
啊..只是一个评论。这不是 JPA 规范问题(就像 DataNucleus 所说),因为如果您使用 hibernate 的本机初始化,它是相同的。虽然我确实认为 JPA 缺乏初始化异常。
I know this is an old thread.
But I was trying to do exactly the same as you.
I did a big search and could not find the answer, even you posted this a long time ago and got no answer.
Well.. what happens is that most Hibernate code catches exceptions and put it into a local exception list.
For example, take a look at
org.hibernate.tool.hbm2ddl.SchemaUpdate.execute()
where it catches all jdbc exceptions and put them on exceptions list.Unfortunately, the code that uses this class lose track of these exceptions since the objects are not referenced anymore.
All exceptions are logged though.. but I find this behavior really annoying since I'm a control freak. The idea of putting exceptions on a list is very good for a framework. But if the other end can't use it.. it's just code bloat and it makes you wast time analyzing why you can't get the God damn exception (For those who never give up).
What I do is perform some basic start up operations on the database and when I get a query exception, then I know something is wrong. The error page in the servlet is more generic but doesn't give the user the impression that the system has lots of bugs (hehe, this sounded wrong).
Ah.. just a remark. It's not a JPA specification problem (like DataNucleus said) since if you use hibernate's native initialization, it's the same. Although I really think JPA lacks of initialization exceptions.