DBCP 使用

package com.sk.pool;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ResourceBundle;
import org.apache.commons.dbcp.BasicDataSource;
 
/**
* @author LuckyStar
* 2008-2-22
* 下午05:59:21
* test
* LuckyStar
*/
public class PoolTest {
private Connection conn;
public static BasicDataSource ds;
//驱动
private String driver="oracle.jdbc.driver.OracleDriver";
//连接字符串
private String url="jdbc:oracle:thin:@localhost:1521:ml";
//用户名
private String username="mms";
//密码
private String password="mms";
//初始化连接数
private int initSize=5;
/**
 * 初始化 数据库配置文件
 * 读取数据
 * @throws Exception 
 */
private void initSource() throws Exception{
if(ds==null){//初始化时 如果ds为空,则初始化
ResourceBundle rb=ResourceBundle.getBundle("conf/DB");//读取资源
if(rb==null){
throw new Exception("数据库配置文件读取失败,程序终止,请查实conf下是否有DB.properties文件");
}else{
if(!rb.getString("driver").equals(""))
driver= rb.getString("driver");
if(!rb.getString("url").endsWith(""))
url=rb.getString("url");
if(!rb.getString("username").equals(""))
username=rb.getString("username");
if(!rb.getString("password").equals(""))
password=rb.getString("password");
}
//设置dbcp
ds=new BasicDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
ds.setInitialSize(initSize);
}
}
public PoolTest() throws Exception{
System.out.println("连接池初始化...........................................");
this.initSource();
show();
}
/**
 * 获取数据库连接
 * @return Connection
 * @throws Exception 
 */
public Connection getConnection() throws Exception{
System.out.println("getConection.......................................");
show();
if(ds!=null){
return conn=ds.getConnection();
}
throw new Exception("dbcp init error! DBOper line 64");

}
/**
 * 关闭数据库连接
 * @throws SQLException
 */
public void closeConnection() throws SQLException{
if(conn!=null){
this.conn.close();
}
System.out.println("...........................................关闭一个连接");
show();
}
private void show(){
System.out.println("InitialSize"+ds.getInitialSize());
System.out.println("NumberActive"+ds.getNumActive());
System.out.println("NumberIdle"+ds.getNumIdle());

}
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
PoolTest db= new PoolTest();
for(int i=0;i<10;i++){
try {
db.getConnection();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"获取第"+(i+1)+"个链接");
if(ds.getNumActive()>4){
try {
//ds.close();
//System.out.println("......................................DS 关闭");
db.closeConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//db.closeConnection();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
########################################################################################################
调用
#####################################
/**
 * 释放资源
 * @throws SQLException
 */
private void releaseResource() throws SQLException{
if(rs!=null){
rs.close();
rs=null;
}
if(cs!=null){
cs.close();
cs=null;
}
if(conn!=null&&!conn.isClosed()){
db.closeConnection();
}
}
/**
 * 初始化 资源 到 cs,cs须 设置参数
 * @param callSql
 * @throws Exception
 */
private void initResource(String callSql) throws Exception{
if(db==null){
db=new DBOperator();
}
if(conn==null||conn.isClosed()){
conn=db.getConnection();
}
if(cs==null){
cs=conn.prepareCall(callSql);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值