自己写的一个连接池,经过不断改进,自认为还算是运行的比较可以的。贴出来,分享一下。
- import java.util.ArrayList;
- import java.util.List;
- import java.sql.*;
- public class ConnectionPool {
- /*******Inner Class ConnectionClass*******/
- public class ConnectionWorker extends Thread {
- ConnectionPool pool;
- public ConnectionWorker(ConnectionPool connPool)
- {
- super("ConnectionWorker");
- pool=connPool;
- }
- @Override
- public void run()
- {
- pool.increase();
- pool.unLock();
- System.out.print("ConnectionWorker:Add Connection Done!" + pool.getNum()+ " Left\n");
- }
- }
- /********Inner Class Define Ends********/
- private List<Connection> pool;
- static final int NumWhenStart = 8;
- static final int NumWhenIncrease = 8;
- static final int NumJudgeOver =50;
- static final int NumLowCase = 6;
- static private String host = "DB Server IP";
- static private String user = "DB User";
- static private String pwd = "Password";
- static private String database = "DB name";
- static private String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
- static private String dbURL = "jdbc:sqlserver://" + host
- + ":1433; DatabaseName=" + database;
- private static ConnectionPool instance = null;
- private ConnectionWorker worker;
- private Boolean lock = false;
- public static ConnectionPool getInstance() { // singleton ,return instance
- if (instance == null) {
- synchronized (ConnectionPool.class) {
- if (instance == null) {
- instance = new ConnectionPool();
- }
- }
- }
- return instance;
- }
- private ConnectionPool() { // constructor
- pool = new ArrayList<Connection>();
- for (int i = 0; i < NumWhenStart; i++)
- pool.add(generateConnection());
- }
- // if the pool is full or the connection is closed
- // return false and drop the connection
- synchronized public boolean putConn(Connection conn) {
- try {
- if (conn == null || conn.isClosed()) {
- System.out.print("DBCP Reporting:Bad Connection Droped\n");
- return false;
- }
- if (pool.size() >= NumJudgeOver) {
- conn.close();
- System.out
- .print("DBCP Reporting:Pool Full, Connection Droped\n");
- return false;
- }
- boolean res = pool.add(conn);
- System.out.print("DBCP Reporting:Connection In! " + pool.size()
- + " Left\n");
- return res;
- } catch (SQLException e) {
- e.printStackTrace();
- return false;
- }
- }
- synchronized public Connection getConn() throws PoolDryException {
- Connection conn = null;
- int i = 0;
- while (true) {
- if (pool.size() < NumLowCase) {
- if(!lock)
- {
- setLock();
- System.out.print("DBCP Reporting: Low Case, Worker run\n");
- worker = new ConnectionWorker(this);
- worker.start();
- }
- }
- while (pool.size() <= 0) {
- if (i > 40) {
- throw new PoolDryException("Wait too long,quit");
- }
- try {
- this.wait(100);
- i++;
- System.out.print("DBCP Reporting: Pool Dry,Waiting. "
- + i + " \n");
- } catch (InterruptedException e) {
- e.printStackTrace();
- return null;
- }
- }
- conn = pool.remove(0);
- if (validateConn(conn)) {
- System.out.print("DBCP Reporting:Connection Out " + getNum()
- + " Connection Left\n");
- return conn;
- }
- if (pool.size() <= 0) {
- throw new PoolDryException("Pool Dry");
- }
- }
- }
- public int getNum() {
- return pool.size();
- }
- private boolean validateConn(Connection conn) // return true if the
- // connection is useful
- {
- if (conn == null)
- return false;
- try {
- Statement stmt = conn.createStatement();
- if (!stmt.execute("select 1"))
- return false;
- stmt.close();
- } catch (Exception e) {
- System.out.print("DBCP Reporting: Validate Connection Fail:"
- + e.getMessage() + "\n");
- return false;
- }
- return true;
- }
- protected int increase() {
- int ori = pool.size();
- Connection conn = null;
- for (int i = 0; i < NumWhenIncrease; i++) {
- conn = ConnectionPool.generateConnection();
- if (conn != null)
- add(conn);
- }
- return pool.size() - ori;
- }
- private void setLock() {
- this.lock=true;
- }
- protected void unLock() {
- this.lock=false;
- }
- private boolean add(Connection conn)
- {
- synchronized(this.pool)
- {
- return pool.add(conn);
- }
- }
- private static Connection generateConnection() // generate connection and
- // return,throw Exception
- {
- try {
- Class.forName(driverName);
- return DriverManager.getConnection(dbURL, user, pwd);
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- }
附PoolDryException.java
- public class PoolDryException extends Exception {
- private static final long serialVersionUID = 1L;
- public PoolDryException(String error)
- {
- super(error);
- }
- }
Support By wzzcn.net
本文出自 “我的代码库” 博客,请务必保留此出处http://wenjianwzz.blog.51cto.com/1600442/325686