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

JdbcPool -1

阅读更多
package com.huawei.db;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Vector;

public abstract class JdbcPool implements ConnectionPool,Runnable
{
  protected String driver, url, username, password;
  private int maxConnections;
  private boolean waitIfBusy;

  private Vector<Connection> availableConnections, busyConnections;
  private boolean connectionPending = false;
 
 
  public JdbcPool (
              String driver, String url,
              String username, String password,
              int initialConnections,
              int maxConnections,
              boolean waitIfBusy) throws SQLException
      {

  this.driver = driver;
  this.url = url;
  this.username = username;
  this.password = password;
  this.maxConnections = maxConnections;
  this.waitIfBusy = waitIfBusy;
  if (initialConnections > maxConnections)
  {
  initialConnections = maxConnections;
  }
 
  availableConnections = new Vector<Connection>(initialConnections);
 
  busyConnections = new Vector<Connection>();
  for(int i=0; i<initialConnections; i++)
  {
  availableConnections.addElement(makeNewConnection());
  }

      }

  /*
   *明确地建立一连接。当ConnectionPool初始化时被在前台调用
   *而当ConnectionPool在运行时,该函数在后台线程中被调用
   */
  protected Connection makeNewConnection() throws SQLException
  {
  try
          {
               // Load database driver if not already loaded
               DriverManager.registerDriver((Driver)Class.forName(driver).newInstance());
             
               Connection connection =
               DriverManager.getConnection(url, username, password);
             
               return(connection);
          }
          catch(ClassNotFoundException cnfe)
          {
               // Simplify try/catch blocks of people using this by
               // throwing only one exception type.
               throw new SQLException("Can't find class for driver: " +
                        driver);
          }
          catch(Exception e)
          {
               throw new SQLException("some error getConnection="+e.toString());
          }
  }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics