【发布时间】:2018-01-19 03:18:00
【问题描述】:
我的印象是数据库驱动程序(在我的情况下为postgres-x.x.jar')和连接池(c3p0)的库必须驻留在容器的库中(例如,对于 Tomcat7,$CATALINA_HOME/lib) .
但是,官方 C3p0 文档 doesn't provide any information 关于将连接池的 jar 放入容器中与将其放入应用程序的战争中:
将文件 lib/c3p0-0.9.5.2.jar 和 lib/mchange-commons-java-0.2.11.jar 放在 CLASSPATH 中的某个位置(或应用程序的类加载器可以找到的任何其他位置)。而已!
新的 tomcat 安装中的当前问题(java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector,当我在应用程序的 WAR 中有 mchange-commons-java dependency 和 $CATALINA_HOME/lib 中的 c3p0 依赖项时)让我重新审视这个,但我不能查找有关将这些库放在何处的任何权威信息。
通常的应用程序配置
在我的情况下,c3p0 配置是通过应用程序类路径中的 spring bean 进行的:
<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
scope="singleton" destroy-method="close">
<property name="driverClass">
<value>org.postgresql.Driver</value>
</property>
<property name="jdbcUrl">
<value>${jdbc.url}</value>
</property>
<property name="user">
<value>${jdbc.user}</value>
</property>
<property name="password">
<value>${jdbc.pw}</value>
</property>
...
<bean>
如果我在同一个Tomcat 容器中有多个应用程序,每个应用程序都会在其war 中包含一个这样的c3p0 bean。
内存泄漏?
假设postgres.jar 和c3p0.jar 在容器的lib/ 中而不是在战争中是后者会导致内存泄漏。
Postgres 驱动程序泄漏
This user states thatJDBC drivers register themselves in the JVM-wide singleton DriverManager which is shared by all web apps. If you have the same (as in class name) JDBC driver register twice from two different web apps, this might cause your problem. This is even more problematic if your web apps use different versions of the same JDBC driver.
c3p0 中的泄漏
我们在 this comment on Stackoverflow 之后将 c3p0 移至 $CATALINA_HOME/lib(在取消部署应用程序时我们收到了类似的警告)。
它们应该驻留在 Tomcat 还是应用程序的 lib/ 中?
【问题讨论】:
标签: java spring postgresql tomcat c3p0