当前位置:文档之家› JAVA数据库连接池

JAVA数据库连接池

package com.persistent.util;import java.sql.*;public class DBConnector{private Connection connection;private Statement statement;public DBConnector(Connection connection,Statement statement){ this.connection = connection;this.statement = statement;}public Connection getConnection(){return this.connection;}public Statement getStatement(){return this.statement;}public void setConnection(Connection connection){this.connection = connection;}public void setStatement(Statement statement){this.statement = statement;}}package com.persistent.util;import java.io.*;import java.util.*;import java.sql.*;import com.microsoft.util.*;import com.microsoft.jdbc.base.*;import com.microsoft.jdbc.sqlserver.*;public class DBConnectorPool{private String dbDriver;private String dbURL;private String dbUserName;private String dbPassword;private List<DBConnector> freeDBConnectorPool = new LinkedList<DBConnector>();private List<DBConnector> busyDBConnectorPool = new LinkedList<DBConnector>();private int initPoolSize;private int maxPoolSize;public DBConnectorPool(Properties dbProperties) throws IOException {this.dbDriver = dbProperties.getProperty("dbDriver");this.dbURL = dbProperties.getProperty("dbURL");this.dbUserName = dbProperties.getProperty("dbUserName");this.dbPassword = dbProperties.getProperty("dbPassword");this.initPoolSize = Integer.parseInt(dbProperties.getProperty("initPoolSize"));this.maxPoolSize = Integer.parseInt(dbProperties.getProperty("maxPoolSize"));initDBConnectorPool();}private DBConnector createDBConnector(){DBConnector dbConnector = null;try{Class.forName(dbDriver);Connection connection = DriverManager.getConnection(dbURL,dbUserName,dbPassword);Statement statement = connection.createStatement();dbConnector = new DBConnector(connection,statement);}catch(ClassNotFoundException e){e.printStackTrace();}catch(SQLException e){e.printStackTrace();}return dbConnector;}public int initDBConnectorPool(){for(int i=0;i<initPoolSize;i++){freeDBConnectorPool.add(createDBConnector());}return freeDBConnectorPool.size();}public synchronized DBConnector getDBConnector() throws InterruptedException { if(freeDBConnectorPool.isEmpty() && busyDBConnectorPool.size() ==maxPoolSize){System.out.println("Please wait for the free DBConnector.");wait();}else if(freeDBConnectorPool.isEmpty() && busyDBConnectorPool.size() < maxPoolSize){DBConnector dbConnector = createDBConnector();busyDBConnectorPool.add(dbConnector);return dbConnector;}else if(!freeDBConnectorPool.isEmpty()){DBConnector dbConnector = freeDBConnectorPool.remove(0);busyDBConnectorPool.add(dbConnector);return dbConnector;}return getDBConnector();}public synchronized boolean closeDBConnector(DBConnector dbConnector){busyDBConnectorPool.remove(dbConnector);freeDBConnectorPool.add(dbConnector);return true;}public int getFreeDBConnetorCount(){return freeDBConnectorPool.size();}public int getBusyDBConnetorCount(){return busyDBConnectorPool.size();}public boolean clearDBConnectorPool(){freeDBConnectorPool = new LinkedList<DBConnector>();busyDBConnectorPool = new LinkedList<DBConnector>();return true;}public int resetDBConnectorPool(){clearDBConnectorPool();return initDBConnectorPool();}}package test;import java.sql.*;import java.io.*;import java.util.*;import com.dreamer.persistent.util.*;class DBTestor{int id;DBConnectorPool pool;public DBTestor(int id,DBConnectorPool pool){this.id = id;this.pool = pool;}public void run() {try{DBConnector dbConnector = pool.getDBConnector();Connection conn = dbConnector.getConnection();Statement stmt = dbConnector.getStatement();stmt.executeUpdate("INSERT INTO USERS V ALUES('"+id+"','dreamer')");System.out.println("TestorID: "+id +" Inserted OK.");pool.closeDBConnector(dbConnector);}catch(InterruptedException e){e.printStackTrace();}catch(SQLException e){}}}public class PoolTest{public static void main (String[] args) throws IOException {Properties props = new Properties();props.load(new FileInputStream(new File("DBConfig.props")));DBConnectorPool pool = new DBConnectorPool(props);java.util.Date dt = new java.util.Date();long start = dt.getTime();for(int i=0;i<4000;i++){DBTestor testor = new DBTestor(i,pool);testor.run();}dt = new java.util.Date();long end = dt.getTime();System.out.println("======== Cost Time: "+(end-start)+" ========");System.out.println("---------- END -----------");}}。

相关主题