让Proxool连接池支持自动重连接
项目使用的连接池是Proxool,有一个问题,如果连接池在运行当中,出现网络或者数据库故障而无法连接到数据库,在恢复正常以后,由于连接是在连接池中持久保存的,会出现连接仍然不可用的情况,这时连接池里的连接实际上都是坏连接,怎么让连接池可以自动重连清除这些坏连接呢?
让Proxool连接池支持自动重连接
其实只要配置了test-before-use 参数,即每次取出连接都检查连接是否可用,就可以做到让连接池实现在故障恢复后自动重连接。实例配置文件如下:
pool
jdbc:mysql://localhost:3306/cds
com.mysql.jdbc.Driver
1
8
1
true
60000
select CURRENT_DATE from dual
< /proxool>
需要注意一点,对于Mysql数据库还必须在连接参数里加上autoReconnect=true 参数,否则即使打开了test-before-use 参数,仍然不能重连接!
===========================================
首先在src目录下创建属性配置文件
proxool.properties
jdbc-0.proxool.alias=test
jdbc-0.proxool.driver-url=jdbc:mysql://localhost:3306/test
jdbc-0.proxool.driver-class=com.mysql.jdbc.Driver
jdbc-0.user=root
jdbc-0.password=coship
jdbc-0.proxool.house-keeping-test-sql=select *
jdbc-0.proxool.maximum-connection-count=10
jdbc-0.proxool.minimum-connection-count=5
在工程中导入一下几个包
proxool-0.9.1.jar
proxool-cglib.jar
commons-logging.jar
mysql-connector-java-5.1.6-bin.jar
编写下面的应用程序
package test.pool;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.ResourceBundle;
import org.logicalcobwebs.proxool.ProxoolException;
import org.logicalcobwebs.proxool.configuration.PropertyConfigurator;
public class mysqlcon {
/**
* @param args
*/
public static void main(String[] args) {
Connection conn = null;
Statement sta=null;
ResultSet rs=null;
String sql="insert into test values ('a','a')";
try {
try {
//PropertyConfigurator.configure(mysqlcon.class.getResource("/")+"proxool.properties");
Properties pro=new Properties();
ResourceBundle resource = ResourceBundle.getBundle("proxool");
pro.put("jdbc-0.proxool.alias",resource.getString("jdbc-0.proxool.alias"));
pro.put("jdbc-0.proxool.driver-url",resource.getString("jdbc-0.proxool.driver-url"));
pro.put("jdbc-0.proxool.driver-class",resource.getString("jdbc-0.proxool.driver-class"));
pro.put("jdbc-0.user",resource.getString("jdbc-0.user"));
pro.put("jdbc-0.password",resource.getString("jdbc-0.password"));
pro.put("jdbc-0.proxool.house-keeping-test-sql",resource.getString("jdbc-0.proxool.house-keeping-test-sql"));
pro.put("jdbc-0.proxool.maximum-connection-count",resource.getString("jdbc-0.proxool.maximum-connection-count"));
pro.put("jdbc-0.proxool.minimum-connection-count",resource.getString("jdbc-0.proxool.minimum-connection-count"));
PropertyConfigurator.configure(pro);
} catch (ProxoolException e) {
e.printStackTrace();
}
conn = DriverManager.getConnection("proxool.test");
sta=conn.createStatement();
rs=sta.executeQuery("select * from test");
while(rs.next()) {
System.out.println(rs.getString("name")+" "+rs.getString("password"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
rs.close();
sta.close();
//conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
======================================
dbname
jdbc:oracle:thin:@127.0.0.1:1521:testdb
oracle.jdbc.driver.OracleDriver
100
10
90000
10
5
true
select sysdate from dual
< /something-else-entirely>