?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????ó???????????????
?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????Ч???????????ò????????????????????Ч?????????á?
??????????????????????????????????????????????????????????????????????????????????????磺???????????getConnection ????????????????????????releaseConnection ??????????????????????????й????????????????????????????????????????????
????????????????????????????
????1?????????
????????????????????????????????????????????????????????????????????????????????????????????????????????л??????????????????????????????????????????/????????????
????2???????????????
?????????????????????????У????????????????????????????????б??á???????????????????????????ɡ????????????????????????????п????????????????????????????????????????????????????????????????
????3???????????????????????????й?
??????????????????????????У????????????????ó???趨??????????????????????????????????????????????п??????????й???
????д??????????????С??????????ο?????
??????????????Java??????????MySQL????jar???????????????????????£?
????package com.test.util;
????import java.sql.Connection;
????import java.sql.DatabaseMetaData;
????import java.sql.Driver;
????import java.sql.DriverManager;
????import java.sql.SQLException;
????import java.sql.Statement;
????import java.util.Enumeration;
????import java.util.Vector;
????public class ConnectionPoolUtil {
????private String jdbcDriver = ""; // ?????????
????private String dbUrl = ""; // ???? URL
????private String dbUsername = ""; // ??????????
????private String dbPassword = ""; // ????????????
????private String testTable = ""; // ????????????????????????????в????
????private int initialConnections = 10; // ??????????С
????private int incrementalConnections = 5;// ??????????????С
????private int maxConnections = 50; // ????????С
????private Vector connections = null; // ???????????????????????? ?? ????? null
????// ???д??????? PooledConnection ??
????/**
????* ??????
????*
????* @param jdbcDriver
????* String JDBC ??????
????* @param dbUrl
????* String ????? URL
????* @param dbUsername
????* String ??????????????
????* @param dbPassword
????* String ??????????????????
????*
????*/
????public ConnectionPoolUtil(String jdbcDriver?? String dbUrl?? String dbUsername?? String dbPassword) {
????this.jdbcDriver = jdbcDriver;
????this.dbUrl = dbUrl;
????this.dbUsername = dbUsername;
????this.dbPassword = dbPassword;
????System.out.println("--------------:ConnectionPoolUtil");
????}
????/**
????* ??????????????С
????*
????* @return ?????????п????????????
????*/
????public int getInitialConnections() {
????return this.initialConnections;
????}
????/**
????* ??????????????С
????*
????* @param ???????ó??????????????????
????*/
????public void setInitialConnections(int initialConnections) {
????this.initialConnections = initialConnections;
????}
????/**
????* ??????????????????С ??
????*
????* @return ??????????????С
????*/
????public int getIncrementalConnections() {
????return this.incrementalConnections;
????}
????/**
????* ??????????????????С
????*
????* @param ??????????????С
????*/
????public void setIncrementalConnections(int incrementalConnections) {
????this.incrementalConnections = incrementalConnections;
????}
????/**
????* ??????????д?????????????
????*
????* @return ??????д?????????????
????*/
????public int getMaxConnections() {
????return this.maxConnections;
????}
????/**
????* ??????????д?????????????
????*
????* @param ??????????д??????????????
????*/
????public void setMaxConnections(int maxConnections) {
????this.maxConnections = maxConnections;
????}
????/**
????* ??????????????????
????*
????* @return ???????????????
????*/
????public String getTestTable() {
????return this.testTable;
????}
????/**
????* ???ò?????????
????*
????* @param testTable
????* String ??????????
????*/
????public void setTestTable(String testTable) {
????this.testTable = testTable;
????}
????/**
????*
????* ????????????????????????е????????????????????? initialConnections ????????
????*/
????public synchronized void createPool() throws Exception {
????// ??????????д???
????// ??????????????????????????????? connections ???????
????if (connections != null) {
????return; // ?????????????????
????}
????// ????? JDBC Driver ????????????????
????Driver driver = (Driver) (Class.forName(this.jdbcDriver).newInstance());
????DriverManager.registerDriver(driver); // ??? JDBC ????????
????// ????????????????? ?? ?????? 0 ?????
????connections = new Vector();
????// ???? initialConnections ???????????????????
????createConnections(this.initialConnections);
????System.out.println(" ?????????????????? ");
????}
????/**
????* ?????? numConnections ????????????????? ?? ??????Щ???? ???? connections ??????
????*
????* @param numConnections
????* ????????????????????
????*/
????@SuppressWarnings("unchecked")
????private void createConnections(int numConnections) throws SQLException {
????// ????????????????????????
????for (int x = 0; x < numConnections; x++) {
????// ?????????е???????????????????????????????? maxConnections
????// ???????? maxConnections ? 0 ?????????????????????????
????// ????????????????????????
????if (this.maxConnections > 0 && this.connections.size() >= this.maxConnections) {
????break;
????}
????// add a new PooledConnection object to connections vector
????// ??????????????????У????? connections ?У?
????try {
????connections.addElement(new PooledConnection(newConnection()));
????} catch (SQLException e) {
????System.out.println(" ????????????????? " + e.getMessage());
????throw new SQLException();
????}
????System.out.println(" ?????????????? ......");
????}
????}
????/**
????* ????????μ?????????????????
????*
????* @return ????????′??????????????
????*/
????private Connection newConnection() throws SQLException {
????// ????????????????
????Connection conn = DriverManager.getConnection(dbUrl?? dbUsername?? dbPassword);
????// ??????????δ????????????????????????????????????????
????// ???????????
????// connections.size()==0 ???????????????????
????if (connections.size() == 0) {
????DatabaseMetaData metaData = conn.getMetaData();
????int driverMaxConnections = metaData.getMaxConnections();
????// ???????? driverMaxConnections ??? 0 ??????????????д?
????// ??????????????????????????????
????// driverMaxConnections ???????????????????????????????????????
????// ????????????????????????????????????????????? ?? ???????????
????// ??????????????????????
????if (driverMaxConnections > 0 && this.maxConnections > driverMaxConnections) {
????this.maxConnections = driverMaxConnections;
????}
????}
????System.out.println("--------------:????????μ??????????");
????return conn; // ??????????μ??????????
????}
????/**
????* ??????? getFreeConnection() ????????????????????????? ?? ????????п???????????????????????????????????
????* ????????????С?????????????????????????????
????*
????* @return ????????????????????????
????*/
????public synchronized Connection getConnection() throws SQLException {
????// ???????????????
????if (connections == null) {
????return null; // ????????????????? null
????}
????Connection conn = getFreeConnection(); // ????????????????????
????// ???????п?????????????????е?????????????
????while (conn == null) {
????// ?????????
????// System.out.println("Wait");
????wait(250);
????conn = getFreeConnection(); // ???????????????????????????
????// getFreeConnection() ?????? null
????// ???????????????????????????????
????}
????System.out.println("--------------:????????????");
????return conn;// ???????????????
????}
????/**
????* ????????????????? connections ?з????????????????????????? ?????п?????????????????????????
????* incrementalConnections ???? ?????????????????????????????????С? ????????????е??????????????У????? null
????*
????* @return ?????????????????????
????*/
????private Connection getFreeConnection() throws SQLException {
????// ????????л???????????????????
????Connection conn = findFreeConnection();
????if (conn == null) {
????// ??????????????п????????
????// ?????Щ????
????createConnections(incrementalConnections);
????// ???′???в???????п???????
????conn = findFreeConnection();
????if (conn == null) {
????// ???????????????ò????????????????? null
????return null;
????}
????}
????System.out.println("--------------:????????????????????");
????return conn;
????}