1

I'm creating a web app using NetBeans and Tomcat 7 and I wanted to create my first connection pool. I've followed all the steps in the tomcat documentation here: http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html

But in my application I'll have many DAO objects with many different methods each, so I'd like to avoid writing the code for looking up the datasource and getting a connection in each and every method that access to the DB, so I've created a class to centralize this operation and return a connection to the different DAO objects.

You can see the code below, but as I said it's my first time wth this, so I'm not sure... does this make sense for you? is there a better way to do something like that? or it's just better to write this code in each method?

And if this does make sense, could I use the Context or even also the DataSource as static attributes to avoid continuous lookups?

Thanks very much!

My ConnectionPool class:

package dao.mysql;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.tomcat.jdbc.pool.DataSource;


public class ConnectionPool {

    public static Connection getConnection() {
        Connection connection = null;
        try {            
            Context initCtx = new InitialContext();
            Context envCtx = (Context) initCtx.lookup("java:comp/env");
            DataSource ds = (DataSource)envCtx.lookup("jdbc/EmployeeDB");

            connection = ds.getConnection();

        } catch (SQLException ex) {
            Logger.getLogger(ConnectionPool.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NamingException ex) {
            Logger.getLogger(ConnectionPool.class.getName()).log(Level.SEVERE, null, ex);
        }
        return connection;
    }
}
MikO
  • 18,243
  • 12
  • 77
  • 109

1 Answers1

1

You can use JDBC Connection Pool as a singleton bean as described here in this article.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thanks @anubhava but I'm confused now, because in fact my first approach was something like what's in the link you posted, and other user said me that I was trying to "reinvent the wheel". Please take a look at my first post here: http://stackoverflow.com/questions/13792469/basic-db-connection-pool-with-java-and-tomcat-7/13792571#13792571 and say me if my first approach was correct. Thanks so much! – MikO Dec 10 '12 at 15:36
  • @MikO: I agree that it is better to use datasource defined via JNDI as you're doing in your code above. However I gave that link to show you how to use connection pooling with ServletContextListener so that you can cleanly initialize and destroy all DB connections. – anubhava Dec 10 '12 at 15:59
  • OK, so my code above is correct and makes sense? using JDNI and also centralizing the lookup in a class? And thanks very much again. – MikO Dec 10 '12 at 16:09
  • 1
    Right it does make sense (and I upvoted your question) for your usecase to centralize it though I would suggest it use in ServletContextListener for clean initialization and destruction. – anubhava Dec 10 '12 at 16:12
  • thanks very much @anubhava! I'll take a look at ServeltContextListener class because I've never used it before... – MikO Dec 10 '12 at 16:14