????/**
????* ??????????????е???????????????????????????? ?????п????????????? null
????*
????* @return ?????????????????????
????*/
????private Connection findFreeConnection() throws SQLException {
????Connection conn = null;
????PooledConnection pConn = null;
????// ?????????????????е????
????Enumeration enumerate = connections.elements();
????// ???????е??????????п????????
????while (enumerate.hasMoreElements()) {
????pConn = (PooledConnection) enumerate.nextElement();
????if (!pConn.isBusy()) {
????// ?????????????????????????????????????
????conn = pConn.getConnection();
????pConn.setBusy(true);
????// ???????????????
????if (!testConnection(conn)) {
????// ???????????????????????????μ??????
????// ???滻?????????????????????????????? null
????try {
????conn = newConnection();
????} catch (SQLException e) {
????System.out.println(" ????????????????? " + e.getMessage());
????return null;
????}
????pConn.setConnection(conn);
????}
????break; // ???????????????????????
????}
????}
????System.out.println("--------------:?????????????????????findFreeConnection??");
????return conn;// ??????????????????
????}
????/**
????* ??????????????????????????????????????? false ??????÷??? true
????*
????* @param conn
????* ?????????????????
????* @return ???? true ????????????? false ?????????
????*/
????private boolean testConnection(Connection conn) {
????try {
????// ?ж???????????
????if (testTable.equals("")) {
????// ????????????????????????? setAutoCommit() ????
????// ???ж??????????????????????????????????????? ??
????// ??????????????ò?????????????
????conn.setAutoCommit(true);
????} else {// ?в??????????ò???????
????// check if this connection is valid
????Statement stmt = conn.createStatement();
????stmt.execute("select count(*) from " + testTable);
????}
????} catch (SQLException e) {
????// ?????????????????????????????????????? false;
????closeConnection(conn);
????return false;
????}
????// ???????????? true
????System.out.println("--------------:???????????????????");
????return true;
????}
????/**
????* ????????????????????????????У????????????????С? ??????????????????????????????????????????????
????*
????* @param ?踐?????????е????????
????*/
????public void returnConnection(Connection conn) {
????System.out.println("--------------:???????????е????????");
????// ?????????????????????д????????????????????
????if (connections == null) {
????System.out.println(" ??????????????????????????????? !");
????return;
????}
????PooledConnection pConn = null;
????Enumeration enumerate = connections.elements();
????// ??????????е?????????????????????????????
????while (enumerate.hasMoreElements()) {
????pConn = (PooledConnection) enumerate.nextElement();
????// ???????????е??????????????
????if (conn == pConn.getConnection()) {
????// ????? ?? ????????????????
????pConn.setBusy(false);
????break;
????}
????}
????}
????/**
????* ?????????????е????????
????*
????*/
????public synchronized void refreshConnections() throws SQLException {
????// ????????????′???
????if (connections == null) {
????System.out.println(" ????????????????? !");
????return;
????}
????PooledConnection pConn = null;
????Enumeration enumerate = connections.elements();
????while (enumerate.hasMoreElements()) {
????// ?????????????
????pConn = (PooledConnection) enumerate.nextElement();
????// ??????????? 5 ?? ??5 ?????????
????if (pConn.isBusy()) {
????wait(5000); // ?? 5 ??
????}
????// ???????????????μ????????????
????closeConnection(pConn.getConnection());
????pConn.setConnection(newConnection());
????pConn.setBusy(false);
????}
????}
????/**
????* ?????????????е?????????????????
????*/
????public synchronized void closeConnectionPool() throws SQLException {
????// ??????????????????????????
????if (connections == null) {
????System.out.println(" ????????????????? !");
????return;
????}
????PooledConnection pConn = null;
????Enumeration enumerate = connections.elements();
????while (enumerate.hasMoreElements()) {
????pConn = (PooledConnection) enumerate.nextElement();
????// ???????? 5 ??
????if (pConn.isBusy()) {
????wait(5000); // ?? 5 ??
????}
????// 5 ??????????
????closeConnection(pConn.getConnection());
????// ??????????????????
????connections.removeElement(pConn);
????}
????// ??????????
????connections = null;
????}
????/**
????* ???????????????
????*
????* @param ????????????????
????*/
????private void closeConnection(Connection conn) {
????try {
????conn.close();
????} catch (SQLException e) {
????System.out.println(" ???????????????? " + e.getMessage());
????}
????}
????/**
????* ??????????????????
????*
????* @param ???????????
????*/
????private void wait(int mSeconds) {
????try {
????Thread.sleep(mSeconds);
????} catch (InterruptedException e) {
????}
????}
????/**
????*
????* ??????????????????????????????? ??????????????????????????????????????????????????? ????????????
????*/
????class PooledConnection {
????Connection connection = null;// ?????????
????boolean busy = false; // ??????????????????????????????????
????// ??????????????? Connection ??????? PooledConnection ????
????public PooledConnection(Connection connection) {
????this.connection = connection;
????}
????// ?????????е?????
????public Connection getConnection() {
????return connection;
????}
????// ??????????????
????public void setConnection(Connection connection) {
????this.connection = connection;
????}
????// ??????????????
????public boolean isBusy() {
????return busy;
????}
????// ?????????????????
????public void setBusy(boolean busy) {
????this.busy = busy;
????}
????}
????}
???????????????????
????package com.test.test;
????import java.sql.Connection;
????import java.sql.DriverManager;
????import java.sql.ResultSet;
????import java.sql.SQLException;
????import java.sql.Statement;
????import com.test.util.ConnectionPoolUtil;
????public class Test {
????/**
????* @param args
????* @throws Exception
????*/