?? connpool.java
字號:
package connection;
import java.io.Serializable;
import java.sql.*;
import java.util.*;
public class ConnPool implements java.io.Serializable{
private String driver = null; //數(shù)據(jù)庫驅動程序對象
private String url = null; //數(shù)據(jù)源的位置
private int size = 0; //連接池的最大連接數(shù)目
private String username = ""; //數(shù)據(jù)源的用戶名
private String password = ""; //數(shù)據(jù)源的密碼
private DbConn dc=null;
private Vector pool = null; //連接池中的連接列表
public ConnPool(){}
//設置數(shù)據(jù)庫驅動程序
public void setDriver(String driver){
if (driver!=null) this.driver=driver;
}
//獲取數(shù)據(jù)庫驅動程序
public String getDriver(){
return driver;
}
//設置數(shù)據(jù)源的位置
public void setURL(String url){
if (url!=null) this.url=url;
}
//獲取數(shù)據(jù)源的位置
public String getURL(){
return url;
}
//設置最大連接數(shù)
public void setSize(int size){
if (size>1) this.size=size;
}
//獲取最大連接數(shù)
public int getSize(){
return size;
}
//設置數(shù)據(jù)源的用戶名
public void setUsername(String username){
if (username!=null) this.username=username;
}
//獲取數(shù)據(jù)源的用戶名
public String getUserName(){
return username;
}
//設置數(shù)據(jù)源的密碼
public void setPassword(String password){
if (password!=null) this.password=password;
}
//獲取數(shù)據(jù)源的密碼
public String getPassword(){
return password;
}
//設置用于單個連接任務的DbConn對象
public void setConnBean(DbConn dc){
if (dc!=null) this.dc=dc;
}
//獲取用于單個連接任務的DbConn對象
public DbConn getConnBean() throws Exception{
Connection conn = getConnection();
DbConn dc = new DbConn(conn); //實例化DbConn類
dc.setInuse(true); //設置此連接可用
return dc;
}
//創(chuàng)建到數(shù)據(jù)庫的連接
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) //如果沒有設置數(shù)據(jù)源的位置
throw new Exception("No URL Proviced!");
if (size<1) //如果當前沒有可用的連接
throw new Exception("Connection Pool Size is less than 1!");
try{
Class.forName(driver);
for (int i=0; i<size; i++){
//創(chuàng)建連接
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){
//尋找到指定連接,將其置為未使用狀態(tài)
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++){
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());
}
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -