简体   繁体   中英

Proper way of obtaining the DBConnection for a Web Application

We have a Web Application which will serve more than 1000 concurrent users Currently , the Utility class for obtaining the DB Connnection is

public static Connection getDBConnection()
       {
          Connection conn = null;
          try
          {
             InitialContext ctx = new InitialContext();
             DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/MyLocalDB");
             try
             {
                conn = ds.getConnection();
             }
             catch ( SQLException sqlEx )
             {
                System.out.println( "cannot get JDBC connection: " + sqlEx ); 
             }
          }
          catch ( NamingException nEx )
          {
             nEx.printStackTrace();
          }
          return conn;
       }

Option 2 :

 public class DBConnection2 {
    private static DataSource dataSource;
    static {
        try {
              dataSource = (DataSource) new InitialContext().lookup("java:/comp/env/jdbc/MyLocalDB");
        } catch (NamingException e) {
            try {
                throw new Exception("'jndifordbconc' not found in JNDI",e);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }

    public static Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }

    }

}

Please let me know whats the better option (I guess its second operation as lookup is a costly operation and i am doing it only for once in a application .)

please share your views .

On the second approach you should be sure to use connection pooling. Otherwise users will have to wait for others.

For example To Tomcat brings it own connection pool. Or if you don't use Tomcat maybe you could have a look at C3p0: JDBC DataSources/Resource Pools .

see: Instantiating and Configuring a ComboPooledDataSource

You should adopt the second approach. As you say you don't need to keep looking up the datasource in JNDI.

I am assuming that you are using a pooled data source as provided by the tomcat-jdbc project, otherwise performance will be terrible.

The second option initialize dataSource only once when the class is loaded, then it is shared around all DBConnection2 instances. The first option initialize a new DataSource instance every time you call getDBConnection() . Thus the second option has better performance. But please be pay attention to connection release issue. Make sure close the connection if you choose the first option. I prefer use a framework to handle DB connection, like Spring JDBC : http://docs.spring.io/spring/docs/current/spring-framework-reference/html/jdbc.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM