在尝试按照我能找到的所有示例配置 c3p0 后,我仍然遇到 JDBC 超时问题。我正在使用 JPA、Hibernate、Tomcat、MySQL [AWS RDS]。这是 persistence.xml 的片段:
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://url..." />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider"/>
<property name="hibernate.c3p0.acquire_increment" value="4" />
<property name="hibernate.c3p0.idle_test_period" value="3000" />
<property name="hibernate.c3p0.max_size" value="100" />
<property name="hibernate.c3p0.max_statements" value="15" />
<property name="hibernate.c3p0.min_size" value="5" />
<property name="hibernate.c3p0.timeout" value="100" />
以及错误日志:
09:18:51.776 WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: 08S01
09:18:51.777 ERROR org.hibernate.util.JDBCExceptionReporter - The last packet successfully received from the server was 38,491,585 milliseconds ago. ...
我从这个博客中找到了答案:http://blog.hpxn.net/2009/02/05/using-c3p0-and-hibernate-3/ http://blog.hpxn.net/2009/02/05/using-c3p0-and-hibernate-3/
persistence.xml 中的属性是错误的,您应该以 hibernate 开头拼写它们:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="mypersistenceunitname">
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<!-- Important -->
<property name="hibernate.connection.provider_class"
value="org.hibernate.connection.C3P0ConnectionProvider" />
<property name="hibernate.c3p0.max_size" value="100" />
<property name="hibernate.c3p0.min_size" value="0" />
<property name="hibernate.c3p0.acquire_increment" value="1" />
<property name="hibernate.c3p0.idle_test_period" value="300" />
<property name="hibernate.c3p0.max_statements" value="0" />
<property name="hibernate.c3p0.timeout" value="100" />
</properties>
</persistence-unit>
</persistence>
然后,你会看到这个神话般的日志出现:
2011-08-15 08:58:33 com.mchange.v2.c3p0.C3P0Registry banner
INFO: Initializing c3p0-0.9.1 [built 16-January-2007 14:46:42; debug? true; trace: 10]
2011-08-15 08:58:33 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
INFO: Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@f3a94e12 [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@6297f706 ....
问候,
Stéphane(抱歉赏金)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)