Servlet开发中JDBC的高级应用(2)

2008-02-23 07:48:22来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折



public synchronized Connection getConnection() {
 Connection con = null;
 if (freeConnections.size() > 0) {
  // Pick the first Connection in the Vector
  // to get round-robin usage
  con = (Connection) freeConnections.firstElement();
  freeConnections.removeElementAt(0);
  try {
   if (con.isClosed()) {
    log("Removed bad connection from " name);
    // Try again recursively
    con = getConnection();
   }
  }
  catch (SQLException e) {
   log("Removed bad connection from " name);
   // Try again recursively
   con = getConnection();
  }
 }
 else if (maxConn == 0 || checkedOut < maxConn) {
  con = newConnection();
 }
 if (con != null) {
  checkedOut ;
 }
 return con;
}

  所有空闲的连接对象保存在一个叫freeConnections 的Vector中。如果存在至少一个空闲的连接,getConnection()返回其中第一个连接。下面,将会看到,进程释放的连接返回到freeConnections的末尾。这样,最大限度地避免了数据库因一个连接不活动而意外将其关闭的风险。

  再返回客户之前,isClosed()检查连接是否有效。如果连接被关闭了,或者一个错误发生,该方法递归调用取得另一个连接。

  如果没有可用的连接,该方法检查是否最大连接数被设置为0表示无限连接数,或者达到了最大连接数。如果可以创建新的连接,则创建一个新的连接。否则,返回null。

  方法newConnection()用来创建一个新的连接。这是一个私有方法,基于用户名和密码来确定是否可以创建新的连接。

private Connection newConnection() {
 Connection con = null;
 try {
  if (user == null) {
   con = DriverManager.getConnection(URL);
  }
  else {
   con = DriverManager.getConnection(URL, user, password);
  }
  log("Created a new connection in pool " name);
 }
 catch (SQLException e) {
  log(e, "Can not create a new connection for " URL);
  return null;
 }
 return con;
}

  jdbc的DriverManager提供一系列的getConnection()方法,可以使用url和用户名,密码等参数创建一个连接。

  第二个getConnection()方法带有一个超时参数 timeout,当该参数指定的毫秒数表示客户愿意为一个连接等待的时间。这个方法调用前一个方法。

public synchronized Connection getConnection(long timeout) {
 long startTime = new Date().getTime();
 Connection con;
 while ((con = getConnection()) == null) {
  try {
   wait(timeout);
  }
  catch (InterruptedException e) {}
  if ((new Date().getTime() - startTime) >= timeout) {
   // Timeout has expired
   return null;
  }
 }
 return con;
}

  局部变量startTime初始化当前的时间。一个while循环首先尝试获得一个连接,如果失败,wait()函数被调用来等待需要的时间。后面会看到,Wait()函数会在另一个进程调用notify()或者notifyAll()时返回,或者等到时间流逝完毕。为了确定wait()是因为何种原因返回,我们用开始时间减去当前时间,检查是否大于timeout。如果结果大于timeout,返回null,否则,在此调用getConnection()函数。

  五、将一个连接返回池中

  DBConnectionPool类中有一个freeConnection方法以返回的连接作为参数,将连接返回连接池。

public synchronized void freeConnection(Connection con) {
 // Put the connection at the end of the Vector
 freeConnections.addElement(con);
 checkedOut--;
 notifyAll();
}

  连接被加在freeConnections向量的最后,占用的连接数减1,调用notifyAll()函数通知其他等待的客户现在有了一个连接。

  六、关闭

  大多数servlet引擎提供完整的关闭方法。数据库连接池需要得到通知以正确地关闭所有的连接。DBConnectionManager负责协调关闭事件,但连接由各个连接池自己负责关闭。方法relase()由DBConnectionManager调用。

public synchronized void release() {
 Enumeration allConnections = freeConnections.elements();
 while (allConnections.hasMoreElements()) {
  Connection con = (Connection) allConnections.nextElement();
  try {
   con.close();
   log("Closed connection for pool " name);
  }
  catch (SQLException e) {
   log(e, "Can not close connection for pool " name);
  }
 }
 freeConnections.removeAllElements();
}

  本方法遍历freeConnections向量以关闭所有的连接。

  DBConnetionManager的构造函数是私有函数,以避免其他类创建其实例。

private DBConnectionManager() {

init();

}

  DBConnetionManager的客户调用getInstance()方法来得到该类的单一实例的引用。

static synchronized public DBConnectionManager getInstance() {
if (instance == null) {
 instance = new DBConnectionManager();
}
clients ;
return instance;
}

  连结池使用实例

  单一的实例在第一次调用时创建,以后的调用返回该实例的静态应用。一个计数器纪录所有的客户数,直到客户释放引用。这个计数器在以后用来协调关闭连接池。

  一、初始化

  构造函数调用一个私有的init()函数初始化对象。

private void init() {
 InputStream is = getClass().getResourceAsStream("/db.properties");

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:jspSmartUpload上传下载全攻略

下一篇:J2EE初学者需要理解的问题