📄 poolbean.java
字号:
package com;
import java.io.Serializable;
import java.sql.*;
import java.util.*;
import com.ConnBean;
public class PoolBean implements java.io.Serializable{
private String driver = null;
private String url = null;
private int size = 0;
private String username = new String("");
private String password = new String("");
private Vector pool = null;
private ConnBean connBean=null;
public PoolBean()
{
driver="sun.jdbc.odbc.JdbcOdbcDriver";
url="jdbc:odbc:meeting";
size=5;
username="";
password="";
}
public void setDriver(String value){
if (value!=null) driver=value;
}
public String getDriver(){
return driver;
}
public void setURL(String value ){
if (value!=null) url=value;
}
public String getURL(){
return url;
}
public void setSize(int value){
if (value>1) size=value;
}
public int getSize(){
return size;
}
public void setUsername(String value){
if (value!=null) username=value;
}
public String getUserName(){
return username;
}
public void setPassword(String value){
if (value!=null) password=value;
}
public String getPassword(){
return password;
}
public void setConnBean(ConnBean cb) {
if (cb!=null) connBean=cb;
}
public ConnBean getConnBean() throws Exception{
Connection con = getConnection();
ConnBean cb = new ConnBean(con);
cb.setInuse(true);
return cb;
}
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 Name Specified!");
if (url==null)
throw new Exception("No URL Specified!");
if (size<1)
throw new Exception("Pool size is less than 1!");
try{
Class.forName(driver);
for (int x=0; x<size; x++)
{
Connection con = createConnection();
if (con!=null)
{
ConnBean pcon = new ConnBean(con);
addConnection(pcon);
}
}
}
catch (Exception e)
{
System.err.println(e.getMessage());
throw new Exception(e.getMessage());
}
}
private void addConnection(ConnBean value)
{
if (pool==null) pool=new Vector(size);
pool.addElement(value);
}
public synchronized void releaseConnection(Connection con)
{
for (int x=0; x<pool.size(); x++)
{
ConnBean pcon =
(ConnBean)pool.elementAt(x);
if (pcon.getConnection()==con)
{
System.err.println("Releasing Connection " + x);
pcon.setInuse(false);
break;
}
}
}
public synchronized Connection getConnection()
throws Exception
{
ConnBean pcon = null;
for (int x=0; x<pool.size(); x++)
{
pcon = (ConnBean)pool.elementAt(x);
if (pcon.getInuse()==false)
{
pcon.setInuse(true);
return pcon.getConnection();
}
}
try
{
Connection con = createConnection();
pcon = new ConnBean(con);
pcon.setInuse(true);
pool.addElement(pcon);
}
catch (Exception e)
{
System.err.println(e.getMessage());
throw new Exception(e.getMessage());
}
return pcon.getConnection();
}
public synchronized void emptyPool()
{
for (int x=0; x<pool.size(); x++)
{
System.err.println("Closing JDBC Connection " + x);
ConnBean pcon =
(ConnBean)pool.elementAt(x);
if (pcon.getInuse()==false)
pcon.close();
else
{
try{
java.lang.Thread.sleep(20000);
pcon.close();
}
catch (InterruptedException ie){
System.err.println(ie.getMessage());
}
}
}
}
public int getConnectionSize()
{
if (pool==null)
return 0;
else
return pool.size();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -