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

JdbcPool

阅读更多
/*
     *得到数据库连接
     */
public synchronized Connection getConnection() throws SQLException {
// TODO Auto-generated method stub
if (!availableConnections.isEmpty())
{
Connection existingConnection =
                 (Connection)availableConnections.lastElement();
            int lastIndex = availableConnections.size() - 1;
           
            availableConnections.removeElementAt(lastIndex);
           
          
            if (existingConnection.isClosed())
            {
                 notifyAll(); // Freed up a spot for anybody waiting
                 return(getConnection());
            }
            else
            {
                 busyConnections.addElement(existingConnection);
                 //System.out.println("ORACLE connPool["+Thread.currentThread().getName()+" got connection("+existingConnection.hashCode()+")]");
                 return(existingConnection);
            }
}
else
        {

        if ((totalConnections() < maxConnections) && !connectionPending)
            {
                 makeBackgroundConnection();
            }
            else if (!waitIfBusy)
            {
                 throw new SQLException("Connection limit reached");
            }
            // Wait for either a new connection to be established
            // (if you called makeBackgroundConnection) or for
            // an existing connection to be freed up.
            try
            {
            //find bug
            while(availableConnections.isEmpty()){
                 wait();
            }
            }
            catch(InterruptedException ie)
            {}
            // Someone freed up a connection, so try again.
            return(getConnection());
       }   
}

public synchronized void free(Connection conn) {
// TODO Auto-generated method stub
busyConnections.removeElement(conn);
        availableConnections.addElement(conn);
       
        // Wake up threads that are waiting for a connection
        notifyAll();
}

public synchronized void setMaxConnectionNumber(int maxlen) {
// TODO Auto-generated method stub
if (maxlen <= availableConnections.size())
       return ;

    this.maxConnections = maxlen ;
}

  
public synchronized void close() {
// TODO Auto-generated method stub
closeConnections (availableConnections);

        availableConnections = new Vector<Connection>();
       
        closeConnections (busyConnections);
       
        busyConnections = new Vector<Connection>();
}

/*
     *关闭一个连接向量中的所有连接
     */
     private void closeConnections(Vector<Connection> connections)
     {
          try
          {
               for(int i=0; i<connections.size(); i++)
               {
                    Connection connection =
                         (Connection)connections.elementAt(i);
                    if (!connection.isClosed())
                    {
                         connection.close();
                    }
               }
          }
          catch(SQLException sqle)
          {
               // Ignore errors; garbage collect anyhow
               // 不处理错误,让垃圾回收处理
          }
     }
     /*
      *ConnectPool对象的字符串描述
      */
     public synchronized String toString()
     {
          String info =
               "ConnectionPool(" + url + "," + username + ")" +
               ", available=" + availableConnections.size() +
               ", busy=" + busyConnections.size() +
               ", max=" + maxConnections+
              ", code=" + hashCode();
          return(info);
     }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics