站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2EE 专题Servlet中jdbc应用高级篇之三

Servlet中jdbc应用高级篇之三

摘要: Servlet中jdbc应用高级篇之三
内容: DBConnectionPool类代表一个由url标识的数据库连接池。前面,我们已经提到,jdbc的url由三个部分组成:协议标识(总是jdbc),子协议标识(例如,odbc.oracle),和数据库标识(跟特定的数据库有关)。连接池也具有一个名字,供客户程序引用。另外,连接池还有一个用户名,一个密码和一个最大允许连接数。如果web应用允许所有的用户使用某些数据库操作,而另一些操作是有限制的,则可以创建两个连接池,具有同样的url,不同的user name和password,分别处理两类不同的操作权限。现把DBConnectionPool详细介绍如下:

  一、DBConnectionPool的构造


  构造函数取得上述的所有参数:

public DBConnectionPool(String name, String URL, String user,

String password, int maxConn) {

this.name = name;

this.URL = URL;

this.user = user;

this.password = password;

this.maxConn = maxConn;

}

  将所有的参数保存在实例变量中。


  二、从池中打开一个连接


  DBConnectionPool提供两种方法来检查连接。两种方法都返回一个可用的连接,如果没有多余的连接,则创建一个新的连接。如果最大连接数已经达到,第一个方法返回null,第二个方法则等待一个连接被其他进程释放。

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向量以关闭所有的连接。 Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd, scwcd DBConne
↑返回目录
前一篇: Servlet中jdbc应用高级篇之六
后一篇: Servlet中jdbc应用高级篇之四