Tomcat连接池,为Web应用程序安装jdbc驱动程序

发布于 2024-09-02 09:31:24 字数 270 浏览 7 评论 0原文

我正在制作一个以 Tomcat 6 作为容器的网络应用程序,并且我正在尝试使用连接池。我使用的jdbc驱动程序是jtds-1.2.2
当驱动程序 jar 放置在 ${Catalina_Home}/lib 下时,池工作正常,但我的托管提供商不允许我这样做。

当驱动程序放置在 WEB-INF/lib 中时,我收到 CNF 异常。

有人可以提供一个解决方案,让我不必访问 tomcat 安装吗?

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.
The pool works fine when the driver jar is placed under ${Catalina_Home}/lib, but my hosting provider would not let me do so.

I get a CNF-Exception when the driver is placed in the WEB-INF/lib.

Could someone please provide a solution where I won't have to access the tomcat installation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我乃一代侩神 2024-09-09 09:31:24

如果您无法控制服务器,那么您就会迷失方向。只需自己创建连接池,而不是让容器来完成。

我建议使用 c3p0 (这比 Tomcat 的内置 DBCP 好得多,因为它被锁定到一个单线程)。将 c3p0 库放入 /WEB-INF/lib 中,并按照 其文档

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

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!
}
高速公鹿 2024-09-09 09:31:24

要使用 Tomcat 的连接池,您必须将 JDBC 驱动程序的 jar 复制到 $CATALINA_HOME/lib 中(如 记录),以便驱动程序类通过 通用类加载器否则 DBCP 将无法找到它,因此ClassNotFoundException。 Tomcat 的类加载器层次结构如下图所示:

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

来自 WEB-INF/lib 的库在通用类加载器中是不可见的(这是一件好事)。

如果您无法将驱动程序复制到 $CATALINA_HOME/lib 中,您将无法使用 Tomcat 的连接池。在这种情况下,您必须使用独立的连接池(并将其与 WEB-INF/lib 中的驱动程序捆绑在一起)。我在这里第二个BalusC,我会使用C3P0。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文