休眠/ C3P0:有没有办法检查连接是否实时关闭? [英] Hibernate/C3P0: Is there a way to check if the connection is down in real time?

查看:183
本文介绍了休眠/ C3P0:有没有办法检查连接是否实时关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我们将其称为 App.class ,该应用程序每秒钟检查数据库中是否有实时事件。

I have an application which we'll refer to as App.class that checks the database every second for real time events.

我遇到一个数据库关闭的实例,并且 App.class 保持重试并抛出异常。

I encountered an instance where the database was down and App.class kept on retrying and throwing an exception.

我想要实现的是确定有效连接是否可用。

What I want to achieve is to determine whether a valid connection is available or not in real time.

我可以通过以下方式获取提供者:

I can get the provider by:

C3P0ConnectionProvider provider =(C3P0ConnectionProvider)sessionFactoryImpl.getConnectionProvider()

我可以通过下面的方法检查连接是否关闭: provider.getConnection()。isClosed()

and I could check if the connection is closed by doing: provider.getConnection().isClosed().

然而,如果数据库关闭,我尝试调用 provider.getConnection() App.class 挂起(可能试图寻找连接),然后抛出一个异常。

However, if the database is down and I try to invoke provider.getConnection(), App.class hangs for a bit (probably trying to look for a connection), then throws an exception.

我想知道是否有一种简单的方法来确定连接离子存在,而不是仅仅捕获错误/异常。

I was wondering if there's an easy way to determine if a connection exists instead of just catching the error/exception when it happens.

推荐答案

所以,c3p0不能告诉你数据库是否是下。在形而上学的认识论上,它无法获得这些信息。它仅观察网络墙上的DBMS阴影。

So, c3p0 cannot tell you whether a database is "down". Metaphysically, epistemologically, it just has no access to that information. It observes only shadows of the DBMS on the walls of the network.

尽管c3p0可以为您做些事情:

There are a few thing c3p0 can do for you though:


  1. 它可以在给您之前测试连接

  2. 它不会让您挂起

  3. 您可以事先询问是否有可能无需等待连接。

听起来您已经问过c3p0通过 hibernate.c3p0.validate c3p0.testConnectionOnCheckout 为您在结帐时测试Connections。如果你没有,当数据库关闭时,你不会看到你的应用程序挂在getConnection()上,当你尝试使用Connection时,你会看到失败。 (您可能正在测试连接在登机和闲置,更多的可能性在下面。)

It sounds like you have already asked c3p0 to test Connections for you on checkout, either via hibernate.c3p0.validate or c3p0.testConnectionOnCheckout. If you hadn't, you wouldn't see your app hang on getConnection() when the database was down, you'd just see failures when you tried to use the Connection. (You might be testing Connections on check-in and idle. More on that possibility below.)

你可以设置配置参数<一个href =http://www.mchange.com/projects/c3p0/#checkoutTimeout =nofollow> c3p0.checkoutTimeout ,所以如果c3p0没有一个好的Connection来传递给你相对很快,它会为您免费提供一个例外。

Rather than putting up with a hang, you could set the config parameter c3p0.checkoutTimeout, so that if c3p0 doesn't have a good Connection to hand you relatively quickly, it sets you free with an Exception.

理想情况下,您可能更愿意提前知道是否有好的连接可用,然后尝试检查并挂起简要介绍。您可以通过c3p0的 PooledDataSource 界面。如果您有权访问 PooledDataSource pds ,那么您只需执行类似于

Ideally, you might prefer to just know in advance whether a good Connection is available before trying to check it out and hanging even briefly. You can ask c3p0 whether Connections are likely to be immediately available, via methods on c3p0's PooledDataSource interface. If you had access to a PooledDataSource pds, you'd just do something like

int idle = pds.getNumIdleConnectionsDefaultUser();

并且可以知道是否调用 getConnection()很可能会很快成功。 (你不能保证,因为c3p0和hibernate是非常异步的,所以在你的check和你的checkout尝试之间,事情可能会改变。不,你不能尝试使用 synchronized block或者某些东西来使这些事情成为原子,c3p0的池在内部执行它们的锁定,而不是在外部 DataSource 级别。)

and could know whether a call to getConnection() would be likely to success quickly if there are at least a few idle Connections. (You can't have a guarantee, because c3p0 and hibernate are very asynchronous, so between your check and your checkout attempt, things may change. And no, you can't try to use a synchronized block or something to make these things atomic, c3p0's pools do their locking internally, not at the outer DataSource level.)

但即使存在空闲连接,在测试它们之前也不知道它们是否合适,并且如果数据库管理系统关闭,您将挂起,因为c3p0尝试所有空闲连接,拒绝他们,然后徒劳地等待尝试获得连接。你想要的是c3p0预先测试过Connections,所以如果没有好的,没有任何东西显示空闲。

But even if there are idle Connections, you won't know if they are good until you test them, and if the DBMS is down, you'll hang as c3p0 tries all of its idle Connections, rejects them, and then waits in vain to try to acquire Connections. What you'd like is for c3p0 to have pre-tested the Connections, so that if none are good, none appear idle.

你无法完美地获得,但您可以通过在结账时将连接测试替换为连接测试,并结合频繁的空闲连接测试来完成。有关详细信息,请参见 c3p0的文档。如果你这样做,糟糕的连接将很快从池中被逐出,并且空闲连接计数将近似于空闲连接的数量。所以,你会有一些(尽管不完美)的信心,如果你看到空闲的Connections,DBMS就会启动,并且对 getConnection()的调用将会很快成功。在DBMS刚刚崩溃的时间段内,使用 checkoutTimeout 返回,但c3p0尚未检测到它,并且您将非常接近您想要的位置be。

You can't get that perfectly, but you can come close by replacing connection testing on checkout with connection testing on checking combined with frequent tests of idle Connections. See c3p0's docs for details on how. If you do this, bad Connections will quickly be evicted from the pool, and the idle Connection count will come to approximate the count of idle good Connections. So, you'll have some, albeit imperfect, confidence that, if you see idle Connections, the DBMS is up and a call to getConnection() will succeed quickly. Back that up with a checkoutTimeout for the period when the DBMS has just crashed, but c3p0 hasn't yet detected it, and you'll be pretty close to where you want to be.

哦,要从休眠连接提供程序获取PooledDataSource,查看源代码应该是类似...

Oh, to get the PooledDataSource from a hibernate Connection provider, looking at the source code, should be something like...

import com.mchange.v2.c3p0.PooledDataSource;

PooledDataSource pds = provider.unwrap(PooledDataSource.class);
int idle = pds.getNumIdleConnectionsDefaultUser();

我还没有尝试过这段代码,并且正在写一篇可怕的急件(对不起!),但如果它值得这么多麻烦,那就应该有效。我认为大多数用户只是使用 c3p0.checkoutTimeout 来限制数据库中断的时间成本,而不必费心去获取 PooledDataSource 询问空闲连接。但如果你需要的话,你可以得到它。

I haven't tried this code, and am writing this in a terrible rush (sorry!), but that should work, if it is worth this much trouble. I think most users just limit the time cost of a database outage using c3p0.checkoutTimeout without going to the trouble to get the PooledDataSource to ask about idle Connections. But if you need that, you can get it.

这篇关于休眠/ C3P0:有没有办法检查连接是否实时关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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