本文介绍了网络出现故障时c3p0会挂在getConnection上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用c3p0 ComboPooledDataSource来缓冲数据库连接(到Oracle 10g数据库).我在处理数据库连接中断时遇到问题.

I'm using c3p0 ComboPooledDataSource to pool database connections (to an Oracle 10g database). I'm having problems with handling database connectivity outages.

如果在获取第一个连接时没有与数据库的连接,则会触发签出超时,并且按预期失败.

If there is no connection to the database when the first connection is being acquired, the checkout timeout fires and it fails as expected.

但是,如果在获取一个或多个连接并且它们已经在连接池中之后发生连接中断,则调用getConnection()会挂起.没有异常被抛出.我猜这是因为它正在尝试使用池化连接,但是该连接不再有效.

However, if a connection outage occurs after one or more connections have been acquired and are already in the connection pool, calling getConnection() just hangs. No exception is thrown. I'm guessing this is because it's trying to use a pooled connection but that connection is no longer alive.

有没有办法在尝试使用连接之前检查连接是否有效?我尝试设置testConnectionOnCheckout = true,但似乎没有任何效果.

Is there a way to check whether the connection is valid before trying to use it? I tried setting testConnectionOnCheckout=true but it doesn't seem to have any effect.

这是线程转储

C3P0PooledConnectionPoolManager [identityToken-> 2rvy8f8x1oujxrx1majv5s | be41d5]-Object.wait()中的HelperThread-#2守护程序prio = 6 tid = 0x0307a800 nid = 0x840 [0x03d1f000] java.lang.Thread.State:TIMED_WAITING(在对象监视器上) 在java.lang.Object.wait(本机方法)-在< 0x28387f88>上等待(com.mchange.v2.async.ThreadPoolAsynchronousRunner) 在com.mchange.v2.async.ThreadPoolAsynchronousRunner $ PoolThread.run(ThreadPoolAsynchronousRunner.java:635) -已锁定< 0x28387f88>(com.mchange.v2.async.ThreadPoolAsynchronousRunner)

C3P0PooledConnectionPoolManager[identityToken->2rvy8f8x1oujxrx1majv5s|be41d5]-HelperThread-#2" daemon prio=6 tid=0x0307a800 nid=0x840 in Object.wait() [0x03d1f000] java.lang.Thread.State: TIMED_WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x28387f88> (a com.mchange.v2.async.ThreadPoolAsynchronousRunner) at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:635) - locked <0x28387f88> (a com.mchange.v2.async.ThreadPoolAsynchronousRunner)

锁定的可拥有同步器: -没有

Locked ownable synchronizers: - None

推荐答案

我遇到了相同的问题.就我而言,这是由于未将JDBC驱动程序设置为在套接字故障时超时而导致的.我在C3P0 ComboPooledDataSource配置中添加了以下内容:

I encountered the same issue. In my case, it was caused by the JDBC driver not being setup to timeout on socket failures. I made the following additions to my C3P0 ComboPooledDataSource configuration:

cpds = new ComboPooledDataSource();
...

//--------------------------------------------------------------------------------------
// NOTE: Once you decide to use cpds.setProperties() to set some connection properties,
//       all properties must be set, including user/password, otherwise an exception
//       will be thrown
Properties prop = new Properties();
prop.setProperty("oracle.net.CONNECT_TIMEOUT",
    Integer.toString(JDBC_CONNECTION_TIMEOUT_IN_MILLISECONDS));
prop.setProperty("oracle.jdbc.ReadTimeout",
    Integer.toString(JDBC_SOCKET_TIMEOUT_IN_MILLISECONDS));
prop.setProperty("user", username);
prop.setProperty("password", password);
cpds.setProperties(prop);
//--------------------------------------------------------------------------------------

...

当C3P0创建Connection对象时,将应用Oracle驱动程序属性.如果套接字连接处于非活动状态超过30秒,则特别是这两个属性将引发异常.

The Oracle driver properties are applied when C3P0 creates the Connection object. These two properties in particular will cause an exception to be thrown if the socket connection is inactive for more than 30 seconds.

如果不连接到Oracle数据库,则其他数据库供应商的其他JDBC驱动程序具有类似的属性.其中一些显示在此内容的底部附近页面.

If you're not connecting to an Oracle database, there are similar properties for other JDBC drivers for other database vendors. Some of them are shown near the bottom of this page.

这篇关于网络出现故障时c3p0会挂在getConnection上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-21 03:09