我将 Hibernate 与 MySQL 5.1.30 一起使用。
我有下一个库:
- c3p0-0.0.1.2.jar
- mysql-connector-java-5.0.3-bin.jar
- hibernate3.jar
我使用 hibernate.cfg.xml 进行配置:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/fooDatatbase</property>
<property name="connection.username">foo</property>
<property name="connection.password">foo123</property>
<!-- Use the C3P0 connection pool provider -->
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_periods">3000</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<mapping resource="databaselayer/mail/Mail.hbm.xml"/>
<mapping resource="databaselayer/courses/Course.hbm.xml"/>
<mapping resource="databaselayer/price/Price.hbm.xml"/>
<mapping resource="databaselayer/contact/Contact.hbm.xml"/>
<mapping resource="databaselayer/artists/Musician.hbm.xml"/>
<mapping resource="databaselayer/concerts/Concert.hbm.xml"/>
<mapping resource="databaselayer/welcome/Welcome.hbm.xml"/>
<mapping resource="databaselayer/information/Information.hbm.xml"/>
</session-factory>
</hibernate-configuration>
在JAVA persistence with hibernate一书中,对 c3p0 配置选项进行了解释:
- hibernate.c3p0.min_size这是 C3P0 始终保持就绪的最小 JDBC 连接数
- hibernate.c3p0.max_size这是池中的最大连接数。如果此数字用尽,则在运行时引发异常。
- hibernate.c3p0.timeout您指定从池中删除空闲连接的超时时间(在本例中为 300 秒)。
- hibernate.c3p0.max_statements将被缓存的最大语句数。缓存准备好的语句对于 Hibernate 的最佳性能至关重要。
- hibernate.c3p0.idle_test_periods这是自动验证连接之前的中间时间(以秒为单位)。
我使用 Java 1.5.0_09 和tomcat 6.0。我在 tomcat 中部署了三个应用程序。他们每个人都使用 hibernate 和一个几乎等同于上面显示的配置文件(只有用户名、数据库名、密码和映射资源发生变化)。
不幸的是,使用上述设置,运行几个小时后,我得到了一些令人讨厌的死锁错误,最终杀死了 tomcat。
Jan 22, 2009 3:29:07 PM com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run
WARNING: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@2437d -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!
Jan 22, 2009 3:29:07 PM com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run
WARNING: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@1dc5cb7 -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!
Jan 22, 2009 3:29:07 PM com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run
WARNING: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@9cd2ef -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!
Jan 22, 2009 3:29:07 PM com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run
WARNING: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@4af355 -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!
Jan 22, 2009 3:29:07 PM com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run
WARNING: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@1275fcb -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!
Jan 22, 2009 3:29:35 PM com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run
这似乎是几个人已经遇到的错误。我将设置更改为尝试遵循此处描述的解决方法http://forum.hibernate.org/viewtopic.php?p=2386237为:
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.min_size">0</property>
<property name="hibernate.c3p0.max_size">48</property>
<property name="hibernate.c3p0.timeout">0</property>
<property name="hibernate.c3p0.max_statements">0</property>
使用新设置,我不会遇到死锁,但我会:
WARNING: SQL Error: 0, SQLState: 08S01
Jan 24, 2009 5:53:37 AM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.io.EOFException
STACKTRACE:
java.io.EOFException
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1913)
有谁知道我做错了什么,以及如何正确设置 c3p0?