defaultconnectionpool.java

来自「java jdk 1.4的源码」· Java 代码 · 共 621 行 · 第 1/2 页

JAVA
621
字号
    //    if ( m_pool.size() < m_PoolMinSize ) { initializePool(); }    // find a connection not in use    for ( int x = 0; x < m_pool.size(); x++ )    {      pcon = (PooledConnection) m_pool.elementAt(x);      // Check to see if the Connection is in use      if ( pcon.inUse() == false )      {        // Mark it as in use        pcon.setInUse(true);        // return the JDBC Connection stored in the        // PooledConnection object        return pcon.getConnection();      }    }    // Could not find a free connection,    // create and add a new one    // Create a new JDBC Connection    Connection con = createConnection();    // Create a new PooledConnection, passing it the JDBC    // Connection    pcon = new PooledConnection(con);    // Mark the connection as in use    pcon.setInUse(true);    // Add the new PooledConnection object to the pool    m_pool.addElement(pcon);    // return the new Connection    return pcon.getConnection();  }  /**   * @param con   * @return   * @throws SQLException   */  public synchronized void releaseConnection( Connection con )throws SQLException  {    // find the PooledConnection Object    for ( int x = 0; x < m_pool.size(); x++ )    {      PooledConnection pcon =        (PooledConnection) m_pool.elementAt(x);      // Check for correct Connection      if ( pcon.getConnection() == con )      {        if (DEBUG)        {          System.out.println("Releasing Connection " + x);        }        if (! isEnabled())        {          con.close();          m_pool.removeElementAt(x);          if (DEBUG)          {            System.out.println("-->Inactive Pool, Closing connection");          }        }        else        {          // Set it's inuse attribute to false, which          // releases it for use          pcon.setInUse(false);        }        break;      }    }  }  /**   * @param con   * @return   * @throws SQLException   */  public synchronized void releaseConnectionOnError( Connection con )throws SQLException  {    // find the PooledConnection Object    for ( int x = 0; x < m_pool.size(); x++ )    {      PooledConnection pcon =        (PooledConnection) m_pool.elementAt(x);      // Check for correct Connection      if ( pcon.getConnection() == con )      {        if (DEBUG)        {          System.out.println("Releasing Connection On Error" + x);        }        con.close();        m_pool.removeElementAt(x);        if (DEBUG)        {          System.out.println("-->Inactive Pool, Closing connection");        }        break;      }    }  }  /**   * @return   * @throws SQLException   */  private Connection createConnection( )throws SQLException  {    Connection con = null;    // Create a Connection    con = DriverManager.getConnection( m_url, m_ConnectionProtocol );    return con;  }  // Initialize the pool  /**   * @return   * @throws IllegalArgumentException   * @throws SQLException   */  public synchronized void initializePool( )throws IllegalArgumentException, SQLException  {     // Check our initial values     if ( m_driver == null )     {       throw new IllegalArgumentException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_DRIVER_NAME_SPECIFIED, null));       // "No Driver Name Specified!");     }     if ( m_url == null )     {       throw new IllegalArgumentException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_URL_SPECIFIED, null));       // "No URL Specified!");     }     if ( m_PoolMinSize < 1 )     {       throw new IllegalArgumentException(XSLMessages.createMessage(XSLTErrorResources.ER_POOLSIZE_LESS_THAN_ONE, null));       // "Pool size is less than 1!");     }     // Create the Connections     // Load the Driver class file     try     {        // We need to implement the context classloader        Class cls = null;        try         {          Method m = Thread.class.getMethod("getContextClassLoader", null);          ClassLoader classLoader = (ClassLoader) m.invoke(Thread.currentThread(), null);          cls = classLoader.loadClass(m_driver);        }         catch (Exception e)         {          cls = Class.forName(m_driver);          }                if (cls == null)          cls = Class.forName(m_driver);        // We have also had problems with drivers unloading        // load an instance that will get freed with the class.        m_Driver = cls.newInstance();     }     catch(ClassNotFoundException e)     {       throw new IllegalArgumentException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_DRIVER_NAME, null));       // "Invalid Driver Name Specified!");     }     catch(Exception e)     {       throw new IllegalArgumentException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_DRIVER_NAME, null));     }     // IF we are not active, don't actuall build a pool yet     // Just set up the driver and periphal items.     if ( !m_IsActive) return;    // Create Connections based on the size member    do    {      Connection con = createConnection();      if ( con != null )      {        // Create a PooledConnection to encapsulate the        // real JDBC Connection        PooledConnection pcon = new PooledConnection(con);        // Add the Connection the pool.        addConnection(pcon);        if (DEBUG) System.out.println("Adding DB Connection to the Pool");      }    }    while (m_pool.size() < m_PoolMinSize);  }  // Adds the PooledConnection to the pool  /**   * @param value   * @return   */  private void addConnection( PooledConnection value )  {    // Add the PooledConnection Object to the vector    m_pool.addElement(value);  }  /**   * @return   * @throws Throwable   */  protected void finalize( )throws Throwable  {    if (DEBUG)    {      System.out.println("In Default Connection Pool, Finalize");    }    // Iterate over the entire pool closing the    // JDBC Connections.    for ( int x = 0; x < m_pool.size(); x++ )    {      if (DEBUG)      {        System.out.println("Closing JDBC Connection " + x);      }      PooledConnection pcon =        (PooledConnection) m_pool.elementAt(x);      // If the PooledConnection is not in use, close it      if ( pcon.inUse() == false ) { pcon.close();  }      else      {        if (DEBUG)        {          System.out.println("--> Force close");        }        // If it still in use, sleep for 30 seconds and        // force close.        try        {          java.lang.Thread.sleep(30000);          pcon.close();        }        catch (InterruptedException ie)        {          if (DEBUG) System.err.println(ie.getMessage());        }      }    }    if (DEBUG)    {      System.out.println("Exit Default Connection Pool, Finalize");    }    super.finalize();  }  /**   * The Pool can be Enabled and Disabled. Disabling the pool   * closes all the outstanding Unused connections and any new   * connections will be closed upon release.   * @param flag Control the Connection Pool. If it is enabled then Connections will actuall be held   * around. If disabled then all unused connections will be instantly closed and as   * connections are released they are closed and removed from the pool.   * @return   */  public void setPoolEnabled( final boolean flag )  {  }}

⌨️ 快捷键说明

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