⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbconnectionmanager.java

📁 利用Java写的数据库连接池代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        }
        catch (IOException e)
        {
        }
        return returnstr;
}

 /**
 * 此内部类定义了一个连接池.它能够根据要求创建新连接,直到预定的最
 * 大连接数为止.在返回连接给客户程序之前,它能够验证连接的有效性.
 */
 class DBConnectionPool {
 private int checkedOut;
 private Vector freeConnections = new Vector();
 private int maxConn;
 public long maxLiveTime;
 private String name;
 private String password;
 private String URL;
 private String user;

public int getCheckedOut()
{
return checkedOut;
}

public int getfreeconn()
{
return freeConnections.size();
}

 /**
 * 创建新的连接池
 *
 * @param name 连接池名字
 * @param URL 数据库的JDBC URL
 * @param user 数据库帐号,或 null
 * @param password 密码,或 null
 * @param maxConn 此连接池允许建立的最大连接数
 */
 public DBConnectionPool(String name, String URL, String user, String password,
 int maxConn, long maxLiveTime) {
 this.name = name;
 this.URL = URL;
 this.user = user;
 this.password = password;
 this.maxConn = maxConn;
 this.maxLiveTime = maxLiveTime;
 }

 /**
 * 将不再使用的连接返回给连接池
 *
 * @param con 客户程序释放的连接
 */
 public synchronized void freeConnection(JDCConnection con,String classname) {
 // 将指定连接加入到向量末尾
if (con.getUseTime()>=maxLiveTime)
 {
                try
                {
                        //(con.getConnection()).close();
                        log(":::连接到期,从连接池"+name+"删除一个连接::::\r\n"+con.toString());
                        con.close();
                        con=null;
                }
                catch(SQLException e)
                {
                }
 }

 else {
        log(classname+",:::连接驰释放:::\r\n"+con.toString());
        //--------------------------------//
        for(int i = 0;i < freeConnections.size();i++)
        {
          if(freeConnections.get(i) == con)
          {
            log("has a ---------------------------------------------------------------");
            return;
          }
        }
        freeConnections.add(con);
        //--------------------------------//
 }
log("freeConnections.size=" + freeConnections.size());
 checkedOut--;
 notifyAll();

 }
 public synchronized void freeConnection(JDCConnection con ) {
  // 将指定连接加入到向量末尾
 if (con.getUseTime()>=maxLiveTime)
  {
                 try
                 {
                         //(con.getConnection()).close();
                         log(":::连接到期,从连接池"+name+"删除一个连接::::\r\n"+con.toString());
                         con.close();
                         con=null;
                 }
                 catch(SQLException e)
                 {
                 }
  }

  else {
         log(",:::连接驰释放:::\r\n"+con.toString());
         //--------------------------------//
         for(int i = 0;i < freeConnections.size();i++)
         {
           if(freeConnections.get(i) == con)
           {
             log("has a ---------------------------------------------------------------");
             return;
           }
         }
         freeConnections.add(con);
         //--------------------------------//
  }
 log("freeConnections.size=" + freeConnections.size());
  checkedOut--;
  notifyAll();

 }
 /**
 * 从连接池获得一个可用连接.如没有空闲的连接且当前连接数小于最大连接
 * 数限制,则创建新连接.如原来登记为可用的连接不再有效,则从向量删除之,
 * 然后递归调用自己以尝试新的可用连接.
 */
 public synchronized JDCConnection getConnection(String classname) {
   log("getConnection in");
 Connection con = null;
 JDCConnection Jcon = null;
 while(freeConnections.size() > 0)
 {
   Jcon = (JDCConnection) freeConnections.remove(0);
   try {
     if ((Jcon.getConnection()).isClosed()) {
       log(":::从连接池" + name+"删除一个无效连接 isClosed");
     }else{
       return Jcon;
     }
   }
   catch (SQLException e) {
     log(":::从连接池" + name+"删除一个无效连接 " + e.getMessage());
     // 递归调用自己,尝试再次获取可用连接
   }
 }
 //-----------new ---------------------------------//
 try {
   if (user == null) {
     con = DriverManager.getConnection(URL);
   }
   else {
     con = DriverManager.getConnection(URL, user, password);
     Jcon=new JDCConnection(con);
   }
   log(classname+",:::连接池" + name+"创建一个新的连接");
 }
 catch (SQLException e) {
   logerr(e.toString() +URL);
   return null;
 }
//---------------------------------------------------//
// Jcon = newConnection();
 log(classname+",:::从连接池" + name+"取连接::::\r\n"+Jcon.toString());
 return Jcon;
/* if (freeConnections.size() > 0) {
 // 获取向量中第一个可用连接

 Jcon = (JDCConnection) freeConnections.firstElement();
 freeConnections.removeElementAt(0);
 try {

 if ((Jcon.getConnection()).isClosed()) {
 log(":::从连接池" + name+"删除一个无效连接 isClosed");
 // 递归调用自己,尝试再次获取可用连接
 Jcon = getConnection();
 }
 }
 catch (SQLException e) {
 log(":::从连接池" + name+"删除一个无效连接 " + e.getMessage());
 // 递归调用自己,尝试再次获取可用连接
 Jcon = getConnection();
 }
 }

 else if (maxConn == 0 || checkedOut < maxConn)
 {
 Jcon = newConnection();
 }
 if (Jcon != null) {
 checkedOut++;
 }
*/
// log(":::从连接池" + name+"取连接::::\r\n"+Jcon.toString());
// return Jcon;
 }
 public synchronized JDCConnection getConnection( ) {
   log("getConnection in");
 Connection con = null;
 JDCConnection Jcon = null;
 while(freeConnections.size() > 0)
 {
   Jcon = (JDCConnection) freeConnections.remove(0);
   try {
     if ((Jcon.getConnection()).isClosed()) {
       log(":::从连接池" + name+"删除一个无效连接 isClosed");
     }else{
       return Jcon;
     }
   }
   catch (SQLException e) {
     log(":::从连接池" + name+"删除一个无效连接 " + e.getMessage());
     // 递归调用自己,尝试再次获取可用连接
   }
 }
 //-----------new ---------------------------------//
 try {
   if (user == null) {
     con = DriverManager.getConnection(URL);
   }
   else {
     con = DriverManager.getConnection(URL, user, password);
     Jcon=new JDCConnection(con);
   }
   log(",:::连接池" + name+"创建一个新的连接");
 }
 catch (SQLException e) {
   logerr(e.toString() +URL);
   return null;
 }
//---------------------------------------------------//
// Jcon = newConnection();
 log(",:::从连接池" + name+"取连接::::\r\n"+Jcon.toString());
 return Jcon;
/* if (freeConnections.size() > 0) {
 // 获取向量中第一个可用连接

 Jcon = (JDCConnection) freeConnections.firstElement();
 freeConnections.removeElementAt(0);
 try {

 if ((Jcon.getConnection()).isClosed()) {
 log(":::从连接池" + name+"删除一个无效连接 isClosed");
 // 递归调用自己,尝试再次获取可用连接
 Jcon = getConnection();
 }
 }
 catch (SQLException e) {
 log(":::从连接池" + name+"删除一个无效连接 " + e.getMessage());
 // 递归调用自己,尝试再次获取可用连接
 Jcon = getConnection();
 }
 }

 else if (maxConn == 0 || checkedOut < maxConn)
 {
 Jcon = newConnection();
 }
 if (Jcon != null) {
 checkedOut++;
 }
*/
// log(":::从连接池" + name+"取连接::::\r\n"+Jcon.toString());
// return Jcon;
 }

 /**
 * 从连接池获取可用连接.可以指定客户程序能够等待的最长时间
 * 参见前一个getConnection()方法.
 *
 * @param timeout 以毫秒计的等待时间限制
 */
 public synchronized JDCConnection getConnection(long timeout,String classname) {
 long startTime = new Date().getTime();
 JDCConnection con;
 while ((con = getConnection(classname)) == null) {
 try {
 wait(timeout);
 }
 catch (InterruptedException e) {}
 if ((new Date().getTime() - startTime) >= timeout) {
 // wait()返回的原因是超时
 return null;
 }
 }
 return con;
 }
 public synchronized JDCConnection getConnection(long timeout ) {
  long startTime = new Date().getTime();
  JDCConnection con;
  while ((con = getConnection()) == null) {
  try {
  wait(timeout);
  }
  catch (InterruptedException e) {}
  if ((new Date().getTime() - startTime) >= timeout) {
  // wait()返回的原因是超时
  return null;
  }
  }
  return con;
 }
 /**
 * 关闭所有连接
 */
 public synchronized void release() {
 Enumeration allConnections = freeConnections.elements();
 while (allConnections.hasMoreElements()) {
 JDCConnection con = (JDCConnection) allConnections.nextElement();
 try {
 (con.getConnection()).close();
 log(":::关闭连接池" + name+"中的一个连接");
 }
 catch (SQLException e) {
 log(e, ":::无法关闭连接池" + name+"中的连接");
 }
 }
 freeConnections.removeAllElements();
 }

 /**
 * 创建新的连接
 */
 private JDCConnection newConnection() {
 Connection con = null;
 JDCConnection Jcon=null;
 try {
 if (user == null) {
 con = DriverManager.getConnection(URL);
 }
 else {
 con = DriverManager.getConnection(URL, user, password);
 Jcon=new JDCConnection(con);
 }
 log(":::连接池" + name+"创建一个新的连接");
 }
 catch (SQLException e) {
 logerr(e.toString() +URL);
 return null;
 }
 return Jcon;
 }
 }

 public String toString()
 {
        return "aa";
 }
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -