当调用 openSession 并且无法建立与数据库的连接时,Hibernate 不会抛出异常
当我配置 hibernate 连接到数据库并且由于某种原因连接失败时,我注意到它在构建工厂(这是可以接受的)和调用 sessionFactory.openSession() (这是不太可接受的)时都没有抛出任何异常。更烦人的是,在我调用 sessionFactory.openSession() 后,session.isConnected() 返回 true。 (这是完全不可接受的)。
到目前为止,告诉我它尚未连接到数据库的唯一提示是由 java.sql.SQLException 引起的 WARN 日志条目,它在内部捕获并仅发送到记录器。除了创建伪事务来强制异常之外,还有其他方法获取连接状态吗? (也许配置中的一个选项说“不记录失败的连接尝试,但抛出休眠异常)。我已经搜索但没有找到任何内容。非常感谢。
When I configure hibernate to connect to a DB and for some reason the connection fails, I noticed that it doesnt throw any exception neither when building the factory (which is acceptable) nor when I call sessionFactory.openSession() (which is less acceptable). What is more annoying is that session.isConnected() returns true after I call sessionFactory.openSession(). (Which is totally unacceptable).
The only hint up to that moment that tells me that it hasnt connected to the DB is a WARN log entry caused by a java.sql.SQLException that it internally catches and just sends to the logger. Besides creating a pseudo transaction to force an exception is there any other way of getting the connection status? (Maybe an option in the configuration that says "dont log the failed connection attempt, but throw a hibernate exception) . I ve searched but not found anything. Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您使用什么连接池? Hibernate 的内置池 应该除了简单测试之外,不得用于 任何其他用途。
另一方面,Proxool 允许您完全按照自己的意愿行事:设置 test-before-use 属性设置为 true,如果找不到或创建合适的连接,则会抛出异常。如果需要,您甚至可以监听连接事件。
What connection pool are you using? Hibernate's built-in pool should not be used for anything other than simple tests.
Proxool, on the other hand, allows you to do exactly what you want: set test-before-use property to true and it will throw an exception if it can't find or create a suitable connection. You can even listen to connection events if you want.
正如 ChssPly 提到的,您不应该将 Hibernate 的内置池用于任何事情。 Proxool(我没有亲自使用过,所以我不能亲自保证)之外的另一个选择是 C3P0。在设置连接池时它肯定会抛出异常 - 我可以保证:)因为这是我想要的应用程序行为,所以它非常适合我们。
As ChssPly mentions, you should not use Hibernate's built-in pool for much of anything. Another option other than Proxool (which I have not personally used, so I can not personally vouch for it) is C3P0. It definitely throws an exception while setting up the connection pool - I CAN vouch for that :) As that is the behavior I want with our application, it works perfectly for us.
抱歉再次提出这个问题,但是:我尝试了 C3p0,它比内置的更好,但就我的问题而言,它与普通休眠完全没有什么不同:如果它无法获取连接,它会抛出异常,Hibernate 捕获那个例外,并再次向我隐藏。通过使用 c3p0,我仍然使用 hibernate API,无论 c3p0 抛出什么,它都会被 hibernate 吸走。我唯一的方法是以编程方式实例化 c3p0 数据源,如果没有抛出异常(我可以捕获),则将其传递给 hibernate。但后来我意识到要将 DataSource 传递给 Hibernate,只能通过 JNDI URL 来完成,而这不是一个选项。所以我最后的希望是实现自定义连接提供程序,在内部使用 c3p0 并捕获 c3p0 的异常。还有其他选择吗?
请注意,我没有尝试 proxool,因为使用 hibernate 配置似乎很棘手,尽管我自己并没有给予它一个公平的机会。也很抱歉在彻底测试之前分配答案。
Sorry to reraise the issue but: I tried C3p0 and it is better than built-in, but as far as my problem is concerned it does absolutely nothing different than vanilla hibernate: If it cant acquire a connection, it throws an exception, Hibernate catches that exception and, again, hides it from me. By using c3p0, I still use the hibernate API, and whatever c3p0 throws, it gets sucked away by hibernate. Only way I though is to programmatically instantiate c3p0 datasource, and if exception is not thrown (this I can catch), then pass it to hibernate. But then I realized that to pass a DataSource to Hibernate, you can only do through JNDI URL, and this is not an option. So my last hope is to implement custom Connection Provider, use c3p0 inside and catch c3p0's exception's in there. Is there any other alternative?
Note that I didnt try proxool because it seems tricky to configure with hibernate, though I didnt myself give it a fair shot. Also sorry for assigning an answer before thoroughly testing.