?? connpool.java
字號:
package chapter10;
import java.io.Serializable;
import java.sql.*;
import java.util.*;
public class ConnPool implements java.io.Serializable{
private String driver = null;
private String url = null;
private int size = 0;
private String username = "";
private String password = "";
private DbConn dc=null;
private Vector pool = null;
public ConnPool(){}
public void setDriver(String driver){
if (driver!=null) this.driver=driver;
}
public String getDriver(){
return driver;
}
public void setURL(String url){
if (url!=null) this.url=url;
}
public String getURL(){
return url;
}
public void setSize(int size){
if (size>1) this.size=size;
}
public int getSize(){
return size;
}
public void setUsername(String username){
if (username!=null) this.username=username;
}
public String getUserName(){
return username;
}
public void setPassword(String password){
if (password!=null) this.password=password;
}
public String getPassword(){
return password;
}
//以上主要是設置其數據的連接參數
public void setConnBean(DbConn dc){
if (dc!=null) this.dc=dc;
}
public DbConn getConnBean() throws Exception{
Connection conn = getConnection();
DbConn dc = new DbConn(conn);
dc.setInuse(true);
return dc;
}
private Connection createConnection() throws Exception{
Connection con = null;
con = DriverManager.getConnection(url,username,password);
return con;
}
public synchronized void initializePool() throws Exception{
if (driver==null)
throw new Exception("No Driver Provided!");
if (url==null)
throw new Exception("No URL Proviced!");
if (size<1)
throw new Exception("Connection Pool Size is less then 1!");
try{
Class.forName(driver);
for (int i=0; i<size; i++){
Connection con = createConnection();
if (con!=null){
DbConn dc = new DbConn(con);
addConnection(dc);
}
}
}catch (Exception e){
System.err.println(e.getMessage());
throw new Exception(e.getMessage());
}
}
private void addConnection(DbConn conn){
if (pool==null) pool=new Vector(size);
pool.addElement(conn);
}
public synchronized void releaseConnection(Connection con){
for (int i=0; i<pool.size(); i++){
DbConn connBean = (DbConn)pool.elementAt(i);
if (connBean.getConnection()==con){
System.err.println("Release No." + i + " Connection!");
connBean.setInuse(false);
break;
}
}
}
public synchronized Connection getConnection() throws Exception{
DbConn dc = null;
for (int i=0; i<pool.size(); i++){
dc = (DbConn)pool.elementAt(i);
if (dc.getInuse()==false){
dc.setInuse(true);
Connection con = dc.getConnection();
return con;
}
}
try{
Connection con = createConnection();
dc = new DbConn(con);
dc.setInuse(true);
pool.addElement(dc);
}catch (Exception e){
System.err.println(e.getMessage());
throw new Exception(e.getMessage());
}
return dc.getConnection();
}
public synchronized void emptyPool(){
for (int i=0; i<pool.size(); i++){
System.err.println("Close No." + i + " JDBC Connection!");
DbConn connBean = (DbConn)pool.elementAt(i);
if (dc.getInuse()==false)
dc.close();
else{
try{
java.lang.Thread.sleep(20000);
dc.close();
}catch (InterruptedException ie){
System.err.println(ie.getMessage());
}
}
}
}
}
//連接數據庫的javabean
class DbConn implements java.io.Serializable{
private Connection conn = null;
private boolean inuse = false;
public DbConn(){}
public DbConn(Connection conn){
if (conn!=null) this.conn = conn;
}
public Connection getConnection(){
return conn;
}
public void setConnection(Connection conn){
this.conn = conn;
}
public void setInuse(boolean inuse){
this.inuse = inuse;
}
public boolean getInuse(){
return inuse;
}
public void close(){
try{
conn.close();
}catch (SQLException sqle){
System.err.println(sqle.getMessage());
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -