当前位置: 首页 > 工具软件 > Vibur DBCP > 使用案例 >

DBCP 使用

蒋联
2023-12-01
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);
  }
 }
 类似资料: