You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Luke (Terry) Vanderfluit" <lu...@chipcity.com.au> on 2004/09/11 05:14:56 UTC

RE: [OFF-TOPIC]Yoav --> RE: Some pretty basic Tomcat ConnectionPooling Questions????

Hi Yoav and all,

Thanks for your reply,

> But you went a bit too far: the DataSource lookup is potentially
> expensive.  That you can do in the init() method and keep a reference to
> the DataSource, because keeping that reference doesn't use a connection
> resource.
> Then in your servlet methods, get a connection from the DataSource, use
> it, and release it.
> In your servlet destroy method, null out your DataSource reference. 
> So the DataSource lookup is done once, the DataSource reference is kept
> as a private non-static member variable of the servlet class, and the
> Connenctions are used only within methods, they're not class member
> variables.

So now I have changed my code to:
1. Declaration of private global variables:
<code>
   private Context ctx = null;
   private DataSource ds = null;
   private Connection conn;
</code>

2. an init() method:
<code>
// "init" does DataSource lookup
   public void init(ServletConfig config) throws ServletException {
      super.init(config);
      try {
         ctx = new InitialContext();
         if(ctx == null) {
            throw new Exception("No Context");
         }
         ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mb");
      } // end try block
      catch(Exception e) {
         e.printStackTrace();
      }
   } // end init()
</code>

3. an openConnection() method:
<code>
   private void openConnection() {
      try {
         if(ds != null) {
            conn = ds.getConnection();
            if(conn != null) {
               message = "Got Connection to DB " + conn.toString();
               }
            }
         } // end try block
      catch(Exception e) {
         e.printStackTrace();
         }
      } //end method openConnection()
</code>

4. a destroy() method that nulls the DataSource:
<code>
   public void destroy() {
   ds = null;
   }
</code>

<remarks>
-the conn.close() is called in the methods that call openConnection().
-I'm thinking of doing an 'include' for the openConnection method, so I
don't have the code for the same method sitting in multiple classes.
Would that be a good idea? (maintainability, yes but in terms of
overhead?)
</remarks>

Would this be the 'leanest' scenario for a database connection?
thanks again,
Luke

-- 
========================
Luke (Terry) Vanderfluit 
Mobile: 0421 276 282     
========================


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


RE: [OFF-TOPIC]Yoav --> RE: Some pretty basic Tomcat ConnectionPooling Questions????

Posted by Caroline Jen <ji...@yahoo.com>.
This is what I do and would like to have feedbacks:

import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DBConnection 
{
   public static Connection getDBConnection() throws
SQLException
   {
      Connection conn = null;

      try
      {
         InitialContext ctx = new InitialContext();
         DataSource ds = ( DataSource ) ctx.lookup(
"java:comp/env/jdbc/MySQLDB" );

         try 
         {
            conn = ds.getConnection();
         }
         catch( SQLException e )
         {  
            System.out.println( "Open connection
failure: " + e.getMessage() );
         }
      }
      catch( NamingException nEx )
      {
         nEx.printStackTrace();
      }
      return conn;
   }
}

--- "Luke (Terry) Vanderfluit" <lu...@chipcity.com.au>
wrote:

> Hi Yoav and all,
> 
> Thanks for your reply,
> 
> > But you went a bit too far: the DataSource lookup
> is potentially
> > expensive.  That you can do in the init() method
> and keep a reference to
> > the DataSource, because keeping that reference
> doesn't use a connection
> > resource.
> > Then in your servlet methods, get a connection
> from the DataSource, use
> > it, and release it.
> > In your servlet destroy method, null out your
> DataSource reference. 
> > So the DataSource lookup is done once, the
> DataSource reference is kept
> > as a private non-static member variable of the
> servlet class, and the
> > Connenctions are used only within methods, they're
> not class member
> > variables.
> 
> So now I have changed my code to:
> 1. Declaration of private global variables:
> <code>
>    private Context ctx = null;
>    private DataSource ds = null;
>    private Connection conn;
> </code>
> 
> 2. an init() method:
> <code>
> // "init" does DataSource lookup
>    public void init(ServletConfig config) throws
> ServletException {
>       super.init(config);
>       try {
>          ctx = new InitialContext();
>          if(ctx == null) {
>             throw new Exception("No Context");
>          }
>          ds =
> (DataSource)ctx.lookup("java:comp/env/jdbc/mb");
>       } // end try block
>       catch(Exception e) {
>          e.printStackTrace();
>       }
>    } // end init()
> </code>
> 
> 3. an openConnection() method:
> <code>
>    private void openConnection() {
>       try {
>          if(ds != null) {
>             conn = ds.getConnection();
>             if(conn != null) {
>                message = "Got Connection to DB " +
> conn.toString();
>                }
>             }
>          } // end try block
>       catch(Exception e) {
>          e.printStackTrace();
>          }
>       } //end method openConnection()
> </code>
> 
> 4. a destroy() method that nulls the DataSource:
> <code>
>    public void destroy() {
>    ds = null;
>    }
> </code>
> 
> <remarks>
> -the conn.close() is called in the methods that call
> openConnection().
> -I'm thinking of doing an 'include' for the
> openConnection method, so I
> don't have the code for the same method sitting in
> multiple classes.
> Would that be a good idea? (maintainability, yes but
> in terms of
> overhead?)
> </remarks>
> 
> Would this be the 'leanest' scenario for a database
> connection?
> thanks again,
> Luke
> 
> -- 
> ========================
> Luke (Terry) Vanderfluit 
> Mobile: 0421 276 282     
> ========================
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> tomcat-user-help@jakarta.apache.org
> 
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


RE: [OFF-TOPIC]RE: Some pretty basic Tomcat ConnectionPooling Questions????

Posted by "Luke (Terry) Vanderfluit" <lu...@chipcity.com.au>.
Hi,

further to this thread I have now implemented the database connection as
follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get a database connection via an external class
see below the code that does this:

1. the init() method that gets the DataSource from an external
connection.
2. a method that queries the database and gets the connection from an
external class.
3. the DBConnection class itself
4. a destroy method that sets the DataSource to null within the calling
class.

I'm pretty sure that doing 'conn = DBConnection.getDBConnection()' is a
good move so as not duplicate code.

<question>
However, I'm not sure about 'ds = DBConnection.getDataSource()'...
that might just as well go as code in the init() method itself.
What is more efficient?
</question>

<question>
Alistair had some interesting comments, namely that
1. We should rename the DBConnection class to DBConnectionFactory. I
have often wondered what a factory is in this sense, could you
elaborate?
2. Adding a finalize() method in the external class 
--> wouldn't that nullify the DataSource before we are finished with it?
--> isn't that doubling up with the destroy() method?
--> how much better would it be to nullify the DataSource in a
finalize() method, a destroy() method or both?
3. I'd like to find out more about 'adding a static reference to itself
and adding a static method called getInstance()' 
--> how much more efficient would that be?
--> how do I implement that?
</question>

This is very interesting and I'm sure it leads to a lot cleaner runnning
of the application.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1. The init() method withing the calling servlet:
// "init" does DataSource lookup
   public void init(ServletConfig config) throws ServletException {
      super.init(config);
      try {ds = DBConnection.getDataSource();}
      catch (Exception e) {}
   } // end init()
===============
2. A method within the calling servlet that uses the external class to
set up a Database connection:
// this method populates a bean
   private void populateCLBean() {
      conn = DBConnection.getDBConnection();
      try {
         Statement stmt = conn.createStatement();
         ResultSet rst = stmt.executeQuery("select * from category;");
         while(rst.next()) {
            Category c  = new Category();
            c.setCategoryName(rst.getString("categoryname"));
            clBean.addCategory(c);
            } // end while block
         rst.close();
         stmt.close();
         conn.close();
         } // end try block
      catch(Exception e) {
         }
      } // end method populateCLBean()
================
3. The external class:
package mb;

import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DBConnection
{
   private static String message;
   private static Connection conn = null;
   private static InitialContext ctx = null;
   private static DataSource ds = null;
   public static DataSource getDataSource() {
      try {
         ctx = new InitialContext();
         ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mb");
      } // end try block
      catch(Exception e) {
      } // end catch block
      return ds;
   } // end method getDataSource()

   public static Connection getDBConnection() {
      try {
         conn = ds.getConnection();
      } // end try block
      catch(Exception e) {
      }
      return conn;
   } // end method getDBConnection()
} // end class DBConnection
==================
4. The destroy method within the calling class.
   public void destroy() {
   ds = null;
   } //end method destroy()
==================
-- 
========================
Luke (Terry) Vanderfluit 
Mobile: 0421 276 282     
========================


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


RE: [OFF-TOPIC]Yoav --> RE: Some pretty basic Tomcat ConnectionPooling Questions????

Posted by "Luke (Terry) Vanderfluit" <lu...@chipcity.com.au>.
Hi,

You are right!
that statement should be there!

kind regards,
Luke

On Tue, 2004-09-14 at 06:08, Caroline Jen wrote:
> I saw your Tomcat connection pool class.
> 
> Your class opens and gets a 'conn' object from the
> connection pool.  Where in your code "returns" the
> 'conn' object for use?  Should there be a statemenet
> like:
> 
>      return conn;
> 
> somewhere?
> 
> 1. Declaration of private global variables:
> <code>
>    private Context ctx = null;
>    private DataSource ds = null;
>    private Connection conn;
> </code>
> 
> 2. an init() method:
> <code>
> // "init" does DataSource lookup
>    public void init(ServletConfig config) throws
> ServletException {
>       super.init(config);
>       try {
>          ctx = new InitialContext();
>          if(ctx == null) {
>             throw new Exception("No Context");
>          }
>          ds =
> (DataSource)ctx.lookup("java:comp/env/jdbc/mb");
>       } // end try block
>       catch(Exception e) {
>          e.printStackTrace();
>       }
>    } // end init()
> </code>
> 
> 3. an openConnection() method:
> <code>
>    private void openConnection() {
>       try {
>          if(ds != null) {
>             conn = ds.getConnection();
>             if(conn != null) {
>                message = "Got Connection to DB " +
> conn.toString();
>                }
>             }
>          } // end try block
>       catch(Exception e) {
>          e.printStackTrace();
>          }
>       } //end method openConnection()
> </code>
> 
> 4. a destroy() method that nulls the DataSource:
> <code>
>    public void destroy() {
>    ds = null;
>    }
> </code>
> --- "Luke (Terry) Vanderfluit" <lu...@chipcity.com.au>
> wrote:
> 
> > Hi Yoav and all,
> > 
> > Thanks for your reply,
> > 
> > > But you went a bit too far: the DataSource lookup
> > is potentially
> > > expensive.  That you can do in the init() method
> > and keep a reference to
> > > the DataSource, because keeping that reference
> > doesn't use a connection
> > > resource.
> > > Then in your servlet methods, get a connection
> > from the DataSource, use
> > > it, and release it.
> > > In your servlet destroy method, null out your
> > DataSource reference. 
> > > So the DataSource lookup is done once, the
> > DataSource reference is kept
> > > as a private non-static member variable of the
> > servlet class, and the
> > > Connenctions are used only within methods, they're
> > not class member
> > > variables.
> > 
> > So now I have changed my code to:
> > 1. Declaration of private global variables:
> > <code>
> >    private Context ctx = null;
> >    private DataSource ds = null;
> >    private Connection conn;
> > </code>
> > 
> > 2. an init() method:
> > <code>
> > // "init" does DataSource lookup
> >    public void init(ServletConfig config) throws
> > ServletException {
> >       super.init(config);
> >       try {
> >          ctx = new InitialContext();
> >          if(ctx == null) {
> >             throw new Exception("No Context");
> >          }
> >          ds =
> > (DataSource)ctx.lookup("java:comp/env/jdbc/mb");
> >       } // end try block
> >       catch(Exception e) {
> >          e.printStackTrace();
> >       }
> >    } // end init()
> > </code>
> > 
> > 3. an openConnection() method:
> > <code>
> >    private void openConnection() {
> >       try {
> >          if(ds != null) {
> >             conn = ds.getConnection();
> >             if(conn != null) {
> >                message = "Got Connection to DB " +
> > conn.toString();
> >                }
> >             }
> >          } // end try block
> >       catch(Exception e) {
> >          e.printStackTrace();
> >          }
> >       } //end method openConnection()
> > </code>
> > 
> > 4. a destroy() method that nulls the DataSource:
> > <code>
> >    public void destroy() {
> >    ds = null;
> >    }
> > </code>
> > 
> > <remarks>
> > -the conn.close() is called in the methods that call
> > openConnection().
> > -I'm thinking of doing an 'include' for the
> > openConnection method, so I
> > don't have the code for the same method sitting in
> > multiple classes.
> > Would that be a good idea? (maintainability, yes but
> > in terms of
> > overhead?)
> > </remarks>
> > 
> > Would this be the 'leanest' scenario for a database
> > connection?
> > thanks again,
> > Luke
> > 
> > -- 
> > ========================
> > Luke (Terry) Vanderfluit 
> > Mobile: 0421 276 282     
> > ========================
> > 
> > 
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> > tomcat-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail:
> > tomcat-user-help@jakarta.apache.org
> > 
> > 
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around 
> http://mail.yahoo.com
-- 
========================
Luke (Terry) Vanderfluit 
Mobile: 0421 276 282     
========================


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


RE: [OFF-TOPIC]Yoav --> RE: Some pretty basic Tomcat ConnectionPooling Questions????

Posted by Caroline Jen <ji...@yahoo.com>.
I saw your Tomcat connection pool class.

Your class opens and gets a 'conn' object from the
connection pool.  Where in your code "returns" the
'conn' object for use?  Should there be a statemenet
like:

     return conn;

somewhere?

1. Declaration of private global variables:
<code>
   private Context ctx = null;
   private DataSource ds = null;
   private Connection conn;
</code>

2. an init() method:
<code>
// "init" does DataSource lookup
   public void init(ServletConfig config) throws
ServletException {
      super.init(config);
      try {
         ctx = new InitialContext();
         if(ctx == null) {
            throw new Exception("No Context");
         }
         ds =
(DataSource)ctx.lookup("java:comp/env/jdbc/mb");
      } // end try block
      catch(Exception e) {
         e.printStackTrace();
      }
   } // end init()
</code>

3. an openConnection() method:
<code>
   private void openConnection() {
      try {
         if(ds != null) {
            conn = ds.getConnection();
            if(conn != null) {
               message = "Got Connection to DB " +
conn.toString();
               }
            }
         } // end try block
      catch(Exception e) {
         e.printStackTrace();
         }
      } //end method openConnection()
</code>

4. a destroy() method that nulls the DataSource:
<code>
   public void destroy() {
   ds = null;
   }
</code>
--- "Luke (Terry) Vanderfluit" <lu...@chipcity.com.au>
wrote:

> Hi Yoav and all,
> 
> Thanks for your reply,
> 
> > But you went a bit too far: the DataSource lookup
> is potentially
> > expensive.  That you can do in the init() method
> and keep a reference to
> > the DataSource, because keeping that reference
> doesn't use a connection
> > resource.
> > Then in your servlet methods, get a connection
> from the DataSource, use
> > it, and release it.
> > In your servlet destroy method, null out your
> DataSource reference. 
> > So the DataSource lookup is done once, the
> DataSource reference is kept
> > as a private non-static member variable of the
> servlet class, and the
> > Connenctions are used only within methods, they're
> not class member
> > variables.
> 
> So now I have changed my code to:
> 1. Declaration of private global variables:
> <code>
>    private Context ctx = null;
>    private DataSource ds = null;
>    private Connection conn;
> </code>
> 
> 2. an init() method:
> <code>
> // "init" does DataSource lookup
>    public void init(ServletConfig config) throws
> ServletException {
>       super.init(config);
>       try {
>          ctx = new InitialContext();
>          if(ctx == null) {
>             throw new Exception("No Context");
>          }
>          ds =
> (DataSource)ctx.lookup("java:comp/env/jdbc/mb");
>       } // end try block
>       catch(Exception e) {
>          e.printStackTrace();
>       }
>    } // end init()
> </code>
> 
> 3. an openConnection() method:
> <code>
>    private void openConnection() {
>       try {
>          if(ds != null) {
>             conn = ds.getConnection();
>             if(conn != null) {
>                message = "Got Connection to DB " +
> conn.toString();
>                }
>             }
>          } // end try block
>       catch(Exception e) {
>          e.printStackTrace();
>          }
>       } //end method openConnection()
> </code>
> 
> 4. a destroy() method that nulls the DataSource:
> <code>
>    public void destroy() {
>    ds = null;
>    }
> </code>
> 
> <remarks>
> -the conn.close() is called in the methods that call
> openConnection().
> -I'm thinking of doing an 'include' for the
> openConnection method, so I
> don't have the code for the same method sitting in
> multiple classes.
> Would that be a good idea? (maintainability, yes but
> in terms of
> overhead?)
> </remarks>
> 
> Would this be the 'leanest' scenario for a database
> connection?
> thanks again,
> Luke
> 
> -- 
> ========================
> Luke (Terry) Vanderfluit 
> Mobile: 0421 276 282     
> ========================
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> tomcat-user-help@jakarta.apache.org
> 
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org