1 packagecom.micheal.util;2
3 importjava.sql.Connection;4 importjava.sql.Driver;5 importjava.sql.DriverManager;6 importjava.sql.SQLException;7 importjava.util.LinkedList;8
9 public classDBConnectionPool {10 private staticDBConnectionPool instance;11
12 privateConnFactory conf;13
14 privateDriver driver;15
16 private String driverClassName="com.mysql.jdbc.Driver";17
18 private String url = "jdbc:mysql://127.0.0.1:3306/userlogin";19
20 private String user = "root";21
22 private String password = "";23
24 private int maxConnNum = 100;25
26 private int initConnNum = 10;27
28 private int freeConnNum = 0;29
30 private int activeConnNum = 0;31
32 private LinkedList freeConnection = new LinkedList();33
34 private LinkedList activeConnection = new LinkedList();35
36 privateDBConnectionPool(){37 this.loadDriver();38 this.createPool();39 }40 //释放所有链接
41 public synchronized booleanreleaseAllConnection(){42 System.err.println("Release all Connection!");43 try{44 while(this.freeConnection.size()>0){45 this.freeConnection.poll().close();46 --this.freeConnNum;47 }48 while(this.activeConnection.size()>0){49 this.activeConnection.poll().close();50 --this.activeConnNum;51 }52 return true;53 }catch(SQLException e){54 e.printStackTrace();55 System.out.println("releaseAllConnection is failed!");56 return false;57 }58 }59 //释放一个链接
60 public synchronized booleanreleaseConnection(Connection conn){61 System.err.println("Release a Connection!");62 try{63 if(conn.isClosed()) {64 --this.activeConnNum;65 return true;66 } else{67 this.freeConnection.add(conn);68 ++this.freeConnNum;69 --this.activeConnNum;70 notifyAll();71 return true;72 }73 } catch(SQLException e) {74 e.printStackTrace();75 System.out.println("Release Connection failed!");76 return false;77 }78 }79 //从链接池获取一个空闲链接,带时间限制
80 public synchronized Connection getConnection(longtimeOut){81 Connection conn;82 while((conn=this.getConnection())==null){83 try{84 wait(timeOut);85 } catch(InterruptedException e) {86 //TODO Auto-generated catch block
87 e.printStackTrace();88 }89 }90 returnconn;91 }92 //从链接池获取一个链接
93 public synchronizedConnection getConnection(){94 System.err.println("getConnection from Pool!");95 Connection conn;96 if(this.freeConnNum>0){97 conn = this.freeConnection.poll();98 --this.freeConnNum;99 try{100 if(conn.isClosed()) {101 conn = this.getConnection();102 }else{103 this.activeConnection.add(conn);104 ++this.activeConnNum;105 }106 } catch(SQLException e) {107 e.printStackTrace();108 System.out.println("getConnection faild!");109 return null;110 }111 }else if(this.activeConnNum
122 public staticDBConnectionPool getInstance(){123 if(instance == null){124 instance = newDBConnectionPool();125 }126 returninstance;127 }128 //创建一个链接池
129 private voidcreatePool() {130 this.conf = newConnFactory(url, user, password);131 System.err.println("Create DBConnectionPool!");132 Connection conn;133 for (int i = 0; i < initConnNum; ++i) {134 if ((conn = conf.newConnection()) != null) {135 this.freeConnection.add(conn);136 ++this.freeConnNum;137 }else{138 --i;139 }140 }141 }142 //装载数据库驱动
143 private voidloadDriver() {144 try{145 driver =(Driver) Class.forName(driverClassName)146 .newInstance();147 DriverManager.registerDriver(driver);148 System.err.println("Installed Driver:"+driverClassName);149 } catch(Exception e) {150 System.out.println("Can not installed Driver:"+driverClassName);151 e.printStackTrace();152 }153 }154
155 public intgetActiveConnNum() {156 returnactiveConnNum;157 }158 public void setActiveConnNum(intactiveConnNum) {159 this.activeConnNum =activeConnNum;160 }161 public intgetFreeConnNum() {162 returnfreeConnNum;163 }164 public void setFreeConnNum(intfreeConnNum) {165 this.freeConnNum =freeConnNum;166 }167 public intgetMaxConnNum() {168 returnmaxConnNum;169 }170 public void setMaxConnNum(intmaxConnNum) {171 this.maxConnNum =maxConnNum;172 }173
174 }