【发布时间】:2013-02-06 07:40:38
【问题描述】:
我正在使用带有 c3p0 的 Hibernate(用于连接池)。
我已经使用 persistence.xml 文件设置了我的连接详细信息。这是我的 persistence.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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 persistence_2_0.xsd">
<persistence-unit name="LBSV1.0" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>xx.dataobject.CompanyDO</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.connection.url" value="jdbc:mysql://xx.xx.xx.xx:xxxx/lbs?autoReconnect=true"/>
<property name="hibernate.connection.username" value="username"/>
<property name="hibernate.connection.password" value="password"/>
<property name="hibernate.generate_statistics" value="true"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.OSCacheProvider"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.c3p0.min_size" value="5"/>
<property name="hibernate.c3p0.max_size" value="20"/>
<property name="hibernate.c3p0.timeout" value="1800"/>
<property name="hibernate.c3p0.max_statements" value="50"/>
<property name="hibernate.c3p0.idle_test_period" value="300"/>
<property name="c3p0.preferredTestQuery" value="select * from status"/>
<property name="c3p0.testConnectionOnCheckout" value="true"/>
<property name="c3p0.debugUnreturnedConnectionStackTraces" value="true"/>
</properties>
</persistence-unit>
应用程序运行良好。应用程序正在获取、插入、删除和更新数据。但如果应用程序空闲超过 8 小时,则会出现以下错误:
05 Feb 2013 19:40:25 49640738 [http-8080-8] ERROR root - [SubscriberDataManager]****** Enable to get Subscriber Details due to : The last packet successfully received from the server was 49,165,385 milliseconds ago. The last packet sent successfully to the server was 49,165,386 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
在 URL 中添加了建议的“autoReconnect=true”。我还搜索了连接超时问题,并在“persistence.xml”文件中添加了所需的属性值,但错误仍然存在。
这是 c3p0 的初始化日志条目:
06 Feb 2013 12:00:56 149355789 [http-8080-1] INFO com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource - Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@9263d39c [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@77f9bf08 [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, debugUnreturnedConnectionStackTraces -> true, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> z8kfsx8s1cc19kywfwqwq|1bdcbb2, idleConnectionTestPeriod -> 300, initialPoolSize -> 5, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 1800, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 50, maxStatementsPerConnection -> 0, minPoolSize -> 5, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@7a7a7ef8 [ description -> null, driverClass -> null, factoryClassLocation -> null, identityToken -> z8kfsx8s1cc19kywfwqwq|1b8737f, jdbcUrl -> jdbc:mysql://**.**.**.**:****/lbs?autoReconnect=true, properties -> {user=******, password=******, autocommit=true, release_mode=auto} ], preferredTestQuery -> select * from status, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> true, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false; userOverrides: {} ], dataSourceName -> null, factoryClassLocation -> null, identityToken -> z8kfsx8s1cc19kywfwqwq|68097d, numHelperThreads -> 3 ]
谁能告诉我
persistence.xml 文件中的设置有什么问题?
或者是 Hibernate 造成了连接设置问题?
【问题讨论】:
-
你的意思是 JPA,而不是 OpenJPA?
-
我正在为 JPA 使用 OpenJPA jar。 OpenJPA 是 Apache Software Foundation 对 JPA 的实现。
-
我知道 OpenJPA 是什么,这就是我问这个问题的原因。在您的 persistence.xml 中,很明显您使用的是
Hibernate ,而不是 OpenJPA。