简体   繁体   English

为Web应用程序获取DBConnection的正确方法

[英]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 我们有一个Web应用程序,它将为1000多个并发用户提供服务。当前,用于获取DB连接的Utility类是

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 : 选项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. 例如,To Tomcat带来了自己的连接池。 Or if you don't use Tomcat maybe you could have a look at C3p0: JDBC DataSources/Resource Pools . 或者,如果您不使用Tomcat,则可以看看C3p0:JDBC DataSources / Resource Pools

see: Instantiating and Configuring a ComboPooledDataSource 请参阅: 实例化和配置ComboPooledDataSource

You should adopt the second approach. 您应该采用第二种方法。 As you say you don't need to keep looking up the datasource in JNDI. 如您所说,您无需一直在JNDI中查找数据源。

I am assuming that you are using a pooled data source as provided by the tomcat-jdbc project, otherwise performance will be terrible. 我假设您使用的是tomcat-jdbc项目提供的池化数据源,否则性能将很糟糕。

The second option initialize dataSource only once when the class is loaded, then it is shared around all DBConnection2 instances. 第二个选项仅在加载类时初始化dataSource一次,然后在所有DBConnection2实例之间共享它。 The first option initialize a new DataSource instance every time you call getDBConnection() . 每次调用getDBConnection()时,第一个选项都会初始化一个新的DataSource实例。 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 我更喜欢使用框架来处理数据库连接,例如Spring JDBChttp : //docs.spring.io/spring/docs/current/spring-framework-reference/html/jdbc.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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