Servlet中jdbc应用高级篇之三

来源:互联网 发布:手机淘宝首页展示位置 编辑:程序博客网 时间:2024/05/18 01:02
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

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

  一、DBConnectionPool的构造

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

  publicDBConnectionPool(Stringname,StringURL,Stringuser,

  Stringpassword,intmaxConn){

  this.name=name;

  this.URL=URL;

  this.user=user;

  this.password=password;

  this.maxConn=maxConn;

  }

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

  二、从池中打开一个连接

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

  publicsynchronizedConnectiongetConnection(){

  Connectioncon=null;

  if(freeConnections.size()>0){

  //PickthefirstConnectionintheVector

  //togetround-robinusage

  con=(Connection)freeConnections.firstElement();

  freeConnections.removeElementAt(0);

  try{

  if(con.isClosed()){

  log("Removedbadconnectionfrom"+name);

  //Tryagainrecursively

  con=getConnection();

  }

  }

  catch(SQLExceptione){

  log("Removedbadconnectionfrom"+name);

  //Tryagainrecursively

  con=getConnection();

  }

  }

  elseif(maxConn==0||checkedOut

  con=newConnection();

  }

  if(con!=null){

  checkedOut++;

  }

  returncon;

  }

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

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

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

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

  privateConnectionnewConnection(){

  Connectioncon=null;

  try{

  if(user==null){

  con=DriverManager.getConnection(URL);

  }

  else{

  con=DriverManager.getConnection(URL,user,password);

  }

  log("Createdanewconnectioninpool"+name);

  }

  catch(SQLExceptione){

  log(e,"Cannotcreateanewconnectionfor"+URL);

  returnnull;

  }

  returncon;

  }

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

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

  publicsynchronizedConnectiongetConnection(longtimeout){

  longstartTime=newDate().getTime();

  Connectioncon;

  while((con=getConnection())==null){

  try{

  wait(timeout);

  }

  catch(InterruptedExceptione){}

  if((newDate().getTime()-startTime)>=timeout){

  //Timeouthasexpired

  returnnull;

  }

  }

  returncon;

  }

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

  四、将一个连接返回池中

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

  publicsynchronizedvoidfreeConnection(Connectioncon){

  //PuttheconnectionattheendoftheVector

  freeConnections.addElement(con);

  checkedOut--;

  notifyAll();

  }

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

  五、关闭

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

  publicsynchronizedvoidrelease(){

  EnumerationallConnections=freeConnections.elements();

  while(allConnections.hasMoreElements()){

  Connectioncon=(Connection)allConnections.nextElement();

  try{

  con.close();

  log("Closedconnectionforpool"+name);

  }

  catch(SQLExceptione){

  log(e,"Cannotcloseconnectionforpool"+name);

  }

  }

  freeConnections.removeAllElements();

  }

  本方法遍历freeConnections向量以

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击