📄 dbpool.java
字号:
/*
* Copyright (c) 2008-2010 Tanming1003 Inc.
* All rights reserved.
*
* tanming1003<tanming1003@163.com>
*
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of tanming1003 nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package hunnu.edu.cn.product.common.db.pool;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.Stack;
import hunnu.edu.cn.product.common.db.DBUtil;
import hunnu.edu.cn.product.log.LogLog;
/**
* @author tanming1003
* @date 2008-10-10
* @time 下午01:54:30
* @project_name Product
* @package_name hunnu.edu.cn.product.common.db.pool
* @file_name DBPool.java
* @version 1.0
*/
public class DBPool extends Pool
{
private Properties properties;
private Stack<Connection> dbpool;
private static DBPool pool;
private String userName;
private String url;
private String password;
private String driver;
private LogLog log=hunnu.edu.cn.product.log.LogFactory.newInstance().getLog(this.getClass());
private int connections;
private DBPool()
{
dbpool=new Stack<Connection>();
currentSize=0;
connections=0;
properties=DBUtil.getResourceProperties();
}
protected static DBPool newInstance()
{
if(pool==null)
pool=new DBPool();
return pool;
}
public void destroy()
{
log.info("开始清空数据库连接池......");
log.info("数据库连接池的大小为:"+dbpool.size());
for(int i=0;i<dbpool.size();i++)
{
Connection connection=dbpool.get(i);
try
{
connection.close();
connection=null;
}
catch (SQLException e)
{
log.debug(e.getMessage(), e);
}
}
dbpool.clear();
dbpool=null;
log.info("完成清空数据库连接池.....");
}
public synchronized void fill(Object object)
{
if(this.connections<this.maxSize)
{
dbpool.push((Connection)object);
updateSize(dbpool);
this.connections++;
log.info("数据库连接池现在的大小是:"+connections);
this.notifyAll();
}
else
{
System.out.println("数据库连接池已满.......");
Connection conn=(Connection)object;
try
{
log.info("开始关闭数据库连接.........");
conn.close();
log.info("完成关闭数据库连接.........");
}
catch (SQLException e)
{
log.debug(e.getMessage(), e);
}
}
}
public synchronized Object get()
{
if(dbpool!=null&&!dbpool.empty())
{
this.connections--;
return dbpool.pop();
}
else
{
if(this.connections<=this.maxSize)
{
for(int i=0;i<this.minSize;i++)
newConnection();
return get();
}
else
{
System.out.println("数据库连接池已空,请稍等.........");
try
{
this.wait(this.maxWait);
return get();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
return null;
}
public void init()
{
log.info("开始初始化数据库连接池.......");
String maxsize=properties.getProperty("maxSize");
String minsize=properties.getProperty("minSize");
String maxwait=properties.getProperty("maxWait");
userName=properties.getProperty("userName");
password=properties.getProperty("password");
url=properties.getProperty("url");
driver=properties.getProperty("driver");
this.setFull(false);
this.setMaxSize(Integer.valueOf(maxsize));
this.setMinSize(Integer.valueOf(minsize));
this.setMaxWait(Integer.valueOf(maxwait));
try
{
Class.forName(driver);
for(int i=0;i<minSize;i++)
{
Connection connection=DriverManager.getConnection(url,userName, password);
if(connection!=null)
{
dbpool.add(connection);
this.connections++;
}
}
this.setCurrentSize(dbpool.size());
log.info("数据库连接池的大小为:"+dbpool.size());
}
catch (ClassNotFoundException e)
{
log.debug("The driver Class can't be found!", e);
}
catch (SQLException e)
{
log.debug("SQLException:", e);
}
log.info("成功初始化数据库连接池....... ");
}
/**
* @return the properties
*/
public Properties getProperties()
{
return properties;
}
/**
* @param properties the properties to set
*/
public void setProperties(Properties properties)
{
this.properties = properties;
}
/**
* @return the dbpool
*/
public Stack<Connection> getDbpool()
{
return dbpool;
}
/**
* @param dbpool the dbpool to set
*/
public void setDbpool(Stack<Connection> dbpool)
{
this.dbpool = dbpool;
}
/**
* @return the userName
*/
public String getUserName()
{
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName)
{
this.userName = userName;
}
/**
* @return the url
*/
public String getUrl()
{
return url;
}
/**
* @param url the url to set
*/
public void setUrl(String url)
{
this.url = url;
}
/**
* @return the password
*/
public String getPassword()
{
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password)
{
this.password = password;
}
/**
* @return the driver
*/
public String getDriver()
{
return driver;
}
/**
* @param driver the driver to set
*/
public void setDriver(String driver)
{
this.driver = driver;
}
public void updateSize(Stack<Connection> dbpool)
{
if(dbpool!=null)
this.currentSize=dbpool.size();
else
this.currentSize=0;
}
/**
* @return the connections
*/
public int getConnections()
{
return connections;
}
/**
* @param connections the connections to set
*/
public void setConnections(int connections)
{
this.connections = connections;
}
public void newConnection()
{
if(this.connections<=this.maxSize)
{
try
{
Class.forName(this.driver);
Connection connection=DriverManager.getConnection(url,userName,password);
dbpool.add(connection);
updateSize(dbpool);
this.connections++;
}
catch (ClassNotFoundException e)
{
log.debug("Class can't be found!", e);
}
catch (SQLException e)
{
log.debug("SQLException:", e);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -