Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Create A Connection Pool In the ServletContextListener

843841Feb 22 2004 — edited Dec 1 2005
The Specification says that we should create the connection pool in the ServletContextListener. I have the code for creating a connection pool (see below). How do I create it in the ServletContextListener?
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;
   }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 29 2005
Added on Feb 22 2004
9 comments
547 views