`
Xgw123485
  • 浏览: 88450 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

JdbcPool -2

阅读更多
public synchronized int getMaxConnectionNumber()
  {
  return maxConnections;
  }

public int getCurrentConnectionNumber()
  {
      return availableConnections.size();
  }
 
  /*
   *总共的连接数(包括已连接的和未被连接的)
   */
  public synchronized int totalConnections()
  {
      return (availableConnections.size() +
               busyConnections.size());
  }
    
  /*
   *通过创建后台线程,来建立与数据库的连接
   *原因:有可能通过网络连接数据库比较慢,所以不能直接地在前台建立连接
   */
  private void makeBackgroundConnection()
  {
      connectionPending = true;
      try
      {
      Thread connectThread = new Thread(this);
     
      connectThread.start();
      }
      catch(OutOfMemoryError oome)
      {
          // Give up on new connection
      }
  }
     
public void run() {
// TODO Auto-generated method stub
Connection connection = null ;
         try
         {
              connection = makeNewConnection();
              synchronized(this)
              {
                   availableConnections.addElement(connection);
                   notifyAll();
              }
         }
         catch(Exception e)
         { // SQLException or OutOfMemory
              // Give up on new connection and wait for existing one
              // to free up.
             e.printStackTrace();
              return ;
         }finally
         {
        connectionPending = false;
         }

      
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics