连接关闭时,将c3p0配置为关闭Hibernate会话 [英] Configure c3p0 to close Hibernate session when connection is down

查看:135
本文介绍了连接关闭时,将c3p0配置为关闭Hibernate会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Hibernate c3p0 用于连接池。由于我在多网状数据库环境中工作,并且碎片停机时间的可能性是一个现实的用例场景(通过手头应用程序外部的方式,例如某人只是因为某种原因无视碎片),所以我试图让c3p0显式关闭Hibernate会话,一旦它检测到数据库连接已关闭,以便我可以跳过我的多分片扫描中特定的分片会话。

I am using Hibernate with c3p0 for connection pooling. Since I am working in a multisharded database environment and the possibility of shards' downtime is a realistic use case scenario (by means external to the application at hand, e.g. someone just taking the shard down for whatever reason), I am trying to have c3p0 explicitly close the Hibernate session once it detects that the database connection is down so that I can skip the particular shard's session in my multi-shard scan.

有没有办法通过调用 Session.close()来配置c3p0以通知Hibernate,一旦它发现连接断开,因此调用 Session.isOpen()可以返回一个有意义的布尔值?

Is there a way to configure c3p0 to notify Hibernate, by invoking Session.close() once it finds out that the connection is down so that calling Session.isOpen() can return a meaningful boolean?

相关: 先发制人地检查org.hibernate.Session是否仍然连接(通过c3p0)

推荐答案

简短的回答是否定的,c3p0不会帮你在这里。

the short answer is no, c3p0 won't help you here.

听起来像(看这里和你以前的问题)你正在做一些与c3p0交叉的工作,那就是你持有长寿的Sessions而不是比根据需要创建会话,然后立即销毁它们。那种事情非常脆弱。它是架构连接池存在,以避免。

it sounds like (looking here and at your previous question) you are doing something that works at cross-purposes to c3p0, that is you are holding long-lived Sessions rather than creating Sessions on an as-needed basis and then destroying them promptly. that kind of thing is very fragile. it is the architecture Connection pools exist to avoid.

您的最佳选择,IMO,将是不要那样做。永远不要缓存会话。那么普通的连接测试(可能还会设置checkoutTimeout)将解决问题。如果分片关闭,尝试获取连接将失败,如果这是最好的解决方法,则可以跳过分片。

your best choice, IMO, would be "don't do that". don't ever cache Sessions at all. then ordinary Connection testing (perhaps with checkoutTimeout set too) will resolve the problem. if a shard is down, attempts to acquire a Connection will fail, and you can skip the shard if that's the best workaround.

c3p0对hibernate一无所知。 c3p0的作者感谢hibernate推广他的(我的)图书馆,但c3p0不知道Session是什么,只是一个连接。从理论上讲,c3p0可以帮助你的唯一方法就是当它发现数据库关闭(根据你的配置可能相当迅速或相当缓慢)时它可以向你报告事件。您可以通过close()ing会话来响应该事件。

c3p0 knows nothing whatsoever about hibernate. c3p0's author is grateful to hibernate for popularizing his (my) library, but c3p0 has no idea what a Session is, just a Connection. the only way that c3p0 could help you a little bit, in theory, would be that it could report to you an event when it notices that a database is down (which might be quite promptly or quite slowly depending on your configuration). you could respond to the event by close()ing sessions.

不幸的是,c3p0尚未提供通过哪些用户响应获取失败的钩子。它可能—这是我正在考虑为0.9.6添加的功能。但现在不是。然而,实现轮询你的分片并自己关闭Sessions的事情是相当微不足道的。所有c3p0都会注意到中断是调用DriverManager.getConnection(...)或dataSource.getConnection()并观察异常。你可以自己做!

unfortunately, c3p0 doesn't (yet) offer hooks through which users could respond to acquisition failures. it might — that's a feature i'm thinking about adding for 0.9.6. but it doesn't now. however, it would be pretty trivial to implement something that polls your shards and closes their Sessions yourself. all c3p0 would do to notice outages would be to call DriverManager.getConnection( ... ) or dataSource.getConnection()and observe Exceptions. you can do that yourself!

尽管如此,我仍强烈建议第一个解决方案。摆脱长期存在的Session对象。

i still strongly recommend the first solution, though. get rid of the long-lived Session objects.

这篇关于连接关闭时,将c3p0配置为关闭Hibernate会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆