link 发表于 2017-2-10 08:44:50

tomcat和weblogic服务器在数据库意外断开后重连方法

  本文地址:http://qzone.qq.com/blog/454389842-1217557897
<!-- 文章内容开始-->http://imgcache.qq.com/ac/b.gif
http://imgcache.qq.com/ac/b.gifTomcat设置方法
在配置Context连接池参数时候加上如下参数:复制内容到剪贴板代码:
             <parameter>
              <name>validationQuery</name>
              <value>SELECT COUNT(*) FROM DUAL</value>
            </parameter>
            <parameter>
              <name>testOnBorrow</name>
              <value>true</value>
            </parameter>
            <parameter>
              <name>testOnReturn</name>
              <value>true</value>
            </parameter>
            <parameter>
              <name>testWhileIdle</name>
              <value>true</value>
            </parameter>testOnBorrow、testOnReturn、testWhileIdle,这些属性的意义是取得、返回对象和空闲时是否进行验证,检查对象是否有效,默认都为false即不验证。所以当使用DBCP时,数据库连接因为某种原因断掉后,再从连接池中取得连接又不进行验证,这时取得的连接实际已经时无效的数据库连接了,只有把这些属性设为true,再提供validationQuery语句就可以保证数据库连接始终有效了,oracle数据库可以使用SELECTCOUNT(*) FROMDUAL,不过DBCP要求validationQuery语句查询的记录集必须不为空,可能这也可以算一个小小的BUG,其实只要validationQuery语句执行通过就可以了。

Weblogic设置方法
进入某个连接池配置页,进入“连接“页,点击高级选项的”show“显示高级选项。
指定 "测试频率" 并启用 "测试保留的连接"、"测试创建的连接" 和 "测试释放的连接"。

测试频率(Test Frequency):  60 秒  

自动数据库连接测试之间的秒数(0 - 32 位正整数)。测试失败的连接将被关闭,然后重新打开以重新建立有效的物理数据库连接。(必须在下面指定测试表名称。)
Thenumber of seconds (between 0 and a positive 32-bit integer) betweenautomatic database connection tests. Connections that fail the test areclosed and reopened to re-establish a valid physical databaseconnection. (You must specify a Test Table Name below.)


测试保留的连接(Test Reserved Connections)
指定 WebLogic Server 是否在将连接提供给客户端之前测试该连接。(必须在下面指定测试表名称。)  
Specifieswhether WebLogic Server tests a connection before giving it to theclient. (You must specify a Test Table Name below.)  

测试创建的连接(Test Created Connections)  
指定 WebLogic Server 是否在创建连接后且将该连接添加到缓冲池中可用连接列表之前对该连接进行测试。(必须指定测试表名称。)    
Specifieswhether WebLogic Server tests a connection after creating it but beforeadding it to the list of connections available in the pool. (You mustspecify a Test Table Name.)

测试释放的连接(Test Released Connections)  
指定 WebLogic Server 是否在将连接返回到该 JDBC 连接缓冲池之前测试该连接。(必须指定测试表名称。)  
Specifieswhether WebLogic Server tests a connection before returning it to thisJDBC connection pool. (You must specify a Test Table Name.)

连接保留超时(Connection Reserve Timeout): 设为 30 秒  
在保留缓冲池连接的调用超时之前的秒数(-1 - 32 位正整数)。如果设置为 -1,则调用永远不会超时。
Thenumber of seconds (between -1 and a positive 32-bit integer) afterwhich a call to reserve a connection from the pool will timeout. Whenset to 0, a call will never timeout. When set to -1, a call willtimeout immediately


重试创建连接的频率(Connection Creation Retry Frequency): 设为 30 秒  
尝试建立与数据库的连接的间隔秒数(0 - 32 位正整数)。适用于在数据库不可用的情况下服务器启动时创建的连接缓冲池。
Thenumber of seconds (between 0 and a positive 32-bit integer) betweenattempts to establish connections to the database. Applies toconnection pools created at server startup when the database isunavailable.

钝化连接超时(Inactive Connection Timeout):
Thenumber of inactive seconds on a reserved connection (between 0 and apositive 32-bit integer) before WebLogic Server reclaims the connectionand releases it back into the connection pool.
服务器申请复议连接并且保留返回给连接池前的钝化时间

测试表名称(Test Table Name):  
oracle的连接池默认为 SQL SELECT 1 FROM DUAL,sybase数据库可以填入一个记录不多的配置表名例如sys_code。
Thename of the database table to use when testing physical databaseconnections. This field is required when you specify a Test Frequencyand enable Test Reserved Connections, Test Created Connections, andTest Released Connections
页: [1]
查看完整版本: tomcat和weblogic服务器在数据库意外断开后重连方法