自动重新连接数据库连接

问题描述:

我在Tomcat中有一个DBCP连接池.问题在于,当连接短暂丢失时,该应用程序将中断,因为在存在连接时,DBCP不会在以后再次尝试重新连接.我可以让DBCP自动重新连接吗?

I have a DBCP connection pool in Tomcat. The problem is that when the connection is lost briefly the appliction is broken because DBCP won't try to reconnect again later when there is a connection. Can I get DBCP to reconnect automatically?

有两种方法可以解决"此问题,尽管两者都有一些问题:

There are 2 ways to "solve" this, though both have some issues:

  1. 您可以使用"validationQuery"(请参见下文)在运行之前运行测试查询(通常类似从双选中选择1"之类的东西,这将用于在获得/给予之前/之后进行连接测试)将它们添加到池中.这会增加来自池中每个连接请求的额外呼叫.请参阅: http://wiki. apache.org/commons/DBCP

您可以通过设置testWhileIdle来让idleEvictorThread进行操作,而不是对每个查询执行此操作,尽管在某些版本中,线程可能会在高负载下导致死锁.有关更多信息,请参见: http://commons.apache.org/dbcp/configuration.html 该选项和其他选项的详细信息

Instead of doing this per query, you can have the idleEvictorThread do it by setting testWhileIdle, though in some versions that thread can cause deadlocking under high-load. See: http://commons.apache.org/dbcp/configuration.html for more details on that and other options