Tomcat connection pooling, install jdbc driver for web-app

后端 未结 2 844
长情又很酷
长情又很酷 2021-01-03 11:41

I am making a web-app with Tomcat 6 as the container and I\'m trying to use connection pooling. The jdbc driver I am using is jtds-1.2.2.

2条回答
  •  Happy的楠姐
    2021-01-03 11:51

    If you don't have control over the server, then you're lost. Just create the connection pool yourself instead of letting the container do it.

    I suggest to use c3p0 for this (which is far better than Tomcat's builtin DBCP since it's locked to a single thread). Put the c3p0 libraries in the /WEB-INF/lib and create it as per its documentation:

    ComboPooledDataSource dataSource = new ComboPooledDataSource(); 
    dataSource.setDriverClass("org.postgresql.Driver"); 
    dataSource.setJdbcUrl("jdbc:postgresql://localhost/testdb");
    dataSource.setUser("dbuser");
    dataSource.setPassword("dbpassword"); 
    // ...
    
    Connection connection = null;
    // ...
    try {
        connection = dataSource.getConnection();
        // ...
    } finally {
        // ...
        if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {} // Always close resources in finally!
    }
    

提交回复
热议问题