那位有连接池方面的类,如果有例子,给我提供一个。
解决方案 »
- jstl迭代 map<date,List> 迭代问题
- html:select 删除当前选择项
- js,function问题,function document.onclick()
- 用myeclipse编辑jsp时,提示unkown tag(!)
- jsp 删除数据后页面仍显示
- 急急急,我在jsp页面中调用了一个存储过程,但在页面中显示无法找到页面,在线等待,马上给分。
- 紧急求助一个关于javabean的奇怪问题!!!!
- 大家推荐自己认为最好的一个jsp学习网站,好么?100分相求~~~~~~
- 页面源代码为空。
- 帮我,我买了本 java servlet 的书,但不知道怎么用,想请教一下java servlet 的服务器环境怎么设?怎么装?要装什么软件?
- java调用oracle备份脚本的问题 (急!!死!!人!!)
- 新手提问
发个信息过来就OK了
private Connection connection=null;
private boolean inUser;
public PooledConnection(Connection connection) {
this.connection=connection;
}
public Connection getConnection() {
return connection;
}
public boolean isInUser() {
return inUser;
}
public void setConnection(Connection connection) {
this.connection = connection;
}
public void setInUser(boolean inUser) {
this.inUser = inUser;
} public void close(){
try{
if (connection != null) {
connection.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}public class ConnectionPool {
private static ConnectionPool pcon=new ConnectionPool();
private ArrayList pools=new ArrayList();
private String driver;
private String url;
private String uesrname;
private String password;
private int size=5; private ConnectionPool() {
initialPools();
}
public synchronized static ConnectionPool getConnectionPool(){
if(pcon==null){
pcon=new ConnectionPool();
}
return pcon;
}
//init the pool
private void initialPools() {
try{
InputStream in = this.getClass().getResourceAsStream("/DB.properties");
Properties props = new Properties();
props.load(in);
this.setDriver(props.getProperty("driver"));
this.setUrl(props.getProperty("url"));
this.setUesrname(props.getProperty("username"));
this.setPassword(props.getProperty("password"));
this.setSize(Integer.parseInt(props.getProperty("size"))); Class.forName(driver);
for (int i = 0; i < size; i++) {
Connection con = DriverManager.getConnection(url, uesrname, password);
PooledConnection poolcon = new PooledConnection(con);
poolcon.setInUser(false);
pools.add(poolcon);
}
System.err.println("initialPools().pools.size()= "+pools.size());
}catch(Exception e){
e.printStackTrace();
}
}
//close the connection
public synchronized void close(){
try{
Iterator its=pools.iterator();
while(its.hasNext()){
PooledConnection pool=(PooledConnection)its.next();
pool.close();
}
pools=null;
}catch(Exception e){
e.printStackTrace();
}
}
public synchronized Connection getConnection()throws Exception{
Iterator its=pools.iterator();
while(its.hasNext()){
PooledConnection poolCon=(PooledConnection)its.next();
if(!poolCon.isInUser())
poolCon.setInUser(true);
return poolCon.getConnection();
} Class.forName(driver);
Connection con=DriverManager.getConnection(url,uesrname,password);
PooledConnection poolcon=new PooledConnection(con);
poolcon.setInUser(true);
pools.add(poolcon);
return con;
}
//release connection
public synchronized void releaseConnection(Connection con){
Iterator its=pools.iterator();
while(its.hasNext()){
PooledConnection poolcon=(PooledConnection)its.next();
if(poolcon.getConnection()==con)
{
poolcon.setInUser(false);
break;
}
}
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUesrname() {
return uesrname;
}
public void setUesrname(String uesrname) {
this.uesrname = uesrname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
}