Tomcat connection pooling, install jdbc driver for web-app

倖福魔咒の 提交于 2019-11-30 16:11:31

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!
}
Pascal Thivent

To use Tomcat's connection pool, you must copy the JDBC Driver's jar into $CATALINA_HOME/lib (as documented) so that the driver class is visible through the Common class loader or DBCP won't be able to find it, hence the ClassNotFoundException. Tomcat's class loaders hierarchy is illustrated below:

      Bootstrap
          |
       System
          |
       Common
       /     \
  Webapp1   Webapp2 ... 

And libraries from WEB-INF/lib are not visible from the Common class loader (which is a good thing).

If you can't copy your driver into $CATALINA_HOME/lib, you won't be able to use Tomcat's connection pool. In that case, you'll have to use a standalone connection pool (and to bundle it along your driver in WEB-INF/lib). And I second BalusC here, I would use C3P0.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!