(1)proxool.xml配置项参数
<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
<proxool>
<alias>webDB</alias>
<driver-url>jdbc:oracle:thin:@127.0.0.1:1521:web</driver-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<driver-properties>
<property name="user" value="web"/>
<property name="password" value="123"/>
<property name="autoReconnect" value="true" />
</driver-properties>
<maximum-active-time>120000</maximum-active-time>
<maximum-connection-count>10</maximum-connection-count>
<minimum-connection-count>2</minimum-connection-count>
<house-keeping-sleep-time>120000</house-keeping-sleep-time>
<simultaneous-build-throttle>10</simultaneous-build-throttle>
<prototype-count>5</prototype-count>
<test-before-use>true</test-before-use>
<house-keeping-sleep-time>60000</house-keeping-sleep-time>
<house-keeping-test-sql>select sysdate from dual</house-keeping-test-sql>
</proxool>
</something-else-entirely>
(2)程序
package com.joe.db;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import org.apache.log4j.Logger;
import org.logicalcobwebs.proxool.ProxoolFacade;
import org.logicalcobwebs.proxool.admin.SnapshotIF;
import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;
public class DBConnections {
private static boolean initialized = false;
private boolean autoCommit = true;
private static int activeCount = 0;
static Logger connectionLog = Logger.getLogger(DBConnections.class);
private synchronized void init()
{
try {
//法一,只是用于调试环境
//JAXPConfigurator.configure("/conf/proxool.xml", false);
//法二适用于 :打包成jar后,配置文件在jar包内
/* InputStream in = DBConnections.class.getResourceAsStream("/conf/proxool.xml");
Reader reader = null;
reader = new InputStreamReader(in, "GBK");
JAXPConfigurator.configure(reader, false);*/
//法三适用于 :打包成jar后,配置文件在jar包外
if(initialized)return;
String configFile = System.getProperty("user.dir")+File.separator+"conf/proxool.xml";
JAXPConfigurator.configure(configFile, false);
Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
} catch (Exception e) {
e.printStackTrace();
connectionLog.error(e.toString());
}
initialized = true;
}
public synchronized boolean getInitStatus(){
return initialized;
}
public synchronized Connection getConnection() {
Connection con = null;
if (!getInitStatus()) {
connectionLog.debug("初始化连接池" + getInitStatus());
init();
}
while (true) {
// 2:创建数据库连接,这个参数是一个字符串,是数据源的别名,在配置文件中配置的timalias,参数格式为:proxool.数据源的别名
try {
con = DriverManager.getConnection("proxool.webDB");
connectionLog.debug("获取连接" + con.toString());
con.setAutoCommit(autoCommit);
SnapshotIF snapshot = ProxoolFacade.getSnapshot("webDB", true);
int curActiveCount = snapshot.getActiveConnectionCount();// 获得活动连接数
int availableCount = snapshot.getAvailableConnectionCount();// 获得可得到的连接数
int maxCount = snapshot.getMaximumConnectionCount();// 获得总连接数
if (curActiveCount != activeCount)// 当活动连接数变化时输出信息
{
System.out.println("--连接池信息-----------------------");
System.out.println(curActiveCount + "(active) "
+ availableCount + "(available) " + maxCount
+ "(max)");
System.out.println("----------------------------------");
activeCount = curActiveCount;
}
} catch (Exception e) {
e.printStackTrace();
}
if (con != null) {
break;
} else {
try {
connectionLog.error("数据库连接异常,休眠");
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
return con;
}
public Connection getConnection(String poolName) {
Connection con = null;
if (!getInitStatus())
{
init();
}
// 2:创建数据库连接,这个参数是一个字符串,是数据源的别名,在配置文件中配置的timalias,参数格式为:proxool.数据源的别名
try {
con = DriverManager.getConnection("proxool."+poolName);
con.setAutoCommit(autoCommit);
SnapshotIF snapshot = ProxoolFacade.getSnapshot(poolName, true);
int curActiveCount = snapshot.getActiveConnectionCount();// 获得活动连接数
int availableCount = snapshot.getAvailableConnectionCount();// 获得可得到的连接数
int maxCount = snapshot.getMaximumConnectionCount();// 获得总连接数
if (curActiveCount != activeCount)// 当活动连接数变化时输出信息
{
// System.out.println("--连接池信息-----------------------");
System.out.println(curActiveCount + "(active) " + availableCount+ "(available) " + maxCount + "(max)");
// System.out.println("----------------------------------");
activeCount = curActiveCount;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
//addReport(e.toString());
}
return con;
}
public static void main(String[] args) throws Exception {
Connection DBConn = new DBConnections().getConnection();
Statement stmt = null;
String sql ="select sysdate from dual ";
try {
stmt = DBConn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
connectionLog.info(rs.getObject(1).toString() );
}
stmt.close();
DBConn.close();
} catch (SQLException ex) {
ex.printStackTrace();
connectionLog.error("更新数据库错误");
try {
if (stmt != null) {
stmt.cancel();
}
} catch (SQLException exs) {
ex.printStackTrace();
}
try {
DBConn.rollback();
} catch (SQLException exs) {
ex.printStackTrace();
}
} finally {
if (DBConn != null) {
try {
if (stmt != null)
stmt.close();
if(!DBConn.isClosed())DBConn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
}