Jonathan Baker wrote:

> Unfortunately, no.  I just wasn't proofreading my mail.  The correct code
> should read:
>
>   try {
>     InitialContext ic = new InitialContext();
>     DataSource ds = (DataSource) ic.lookup(...);
>     Connection c = ds.getConnection();
>
>     try {
>         ...
>
> The point I was trying to make is that using try/finally blocks can keep
> your connections from being stranded.  It also guarantees your statement
> objects will close, freeing up memory even if there was an exception thrown
> during processing.
>
> I hope that clears things up!
>
> Jonathan Baker
> Internet Applications Division
> Sybase, Inc.
> [...]

Hi :-)  Thanks very much for your email!   and sorry for bothering you with
another question:

0
I am trying to develop a simple DBConnection pool, I want to use it in:
*  my Servlet class which is put in the Servlet container of my EJB container
*   my Session Bean
the following is my code, if you have time, can you give me some direction
about it ? -> tell me if my code is right or not?   Thanks!

1  my code
   [a]    ********************  the simple DBConnection pool  *********************

public class outsideDBCPHoder implements java.io.Serializable {
private static outsideDBCPHoder onlyOneInstance;
private java.util.Vector pool=new java.util.Vector();

private outsideDBCPHoder() {}


   public static synchronized outsideDBCPHoder getOnlyOneInstance() {
      if (onlyOneInstance == null) {
      onlyOneInstance = new outsideDBCPHoder();
      }
   return onlyOneInstance;
   }


   public synchronized java.sql.Connection getDBConnection() {
      java.sql.Connection con = null;
      if ( pool.isEmpty() ) {
      con = ...;  //make a new DBConnection
      } else {
      con = (java.sql.Connection) pool.elementAt(0);
      pool.removeElementAt(0);
   }
   return con;
   }

   public synchronized void putbackDBConnection( java.sql.Connection con ) {
      if (pool.size() < 8) {  // 8 is the MAX number of my pool, it can be any
value(10, 100...)
      pool.addElement(con);
   }else{
   ... //close this con
   con=null;
   }
   return;
   }

   public synchronized void closeAllDBConnection() {
   String con=null;
      if ( !pool.isEmpty() ) {
   con=(java.sql.Connection)pool.elementAt(0);
   ...// cole the con
    con=null;
    pool.removeElementAt(0);
   }
   }

   protected synchronized void finalize(){
   closeAllDBConnection();
   }
}

[b]    ********how to use this simple DBConnection pool  in Servlet class********

public class servlet_outsideDBCPHoder extends HttpServlet {
private static outsideDBCPHoder myDBCP
                      =outsideDBCPHoder.getOnlyOneInstance();

    //init ...

    _service/doGet/doPost{
   java.sql.Connection con= myDBCP.getDBConnection();

   ...// use con

   myDBCP.putbackDBConnection(con);
   }

   protected void finalize(){
   myDBCP.closeAllDBConnection();
   }

}

[c]    outsideDBCPHoder is a *SingleTonBean* ->
   *  the reason for making it  as a SingleTon is:
       try to avoid the problem led by loading/unloading MyServlet several
       times, but perhaps it is not necessary, I am not sure.
   *  the reason for that make it Serializable is:
      from the emails in Servlet-List, bean class will be loaded by
      the same ClassLoader, I also don't know if it is nessary.

If you have time,  please give me some directions,
Thanks very much && anyway && in advance!  :-)


Bo
Nov.17, 2000

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to