2011-01-29 115 views
2

我在使用MySQL和MySQL Connector/J(MySQL的官方JDBC驱动程序)的Java EE应用程序中遇到Hibernate(3.6.0.-Final)问题。Hibernate JDBCExceptionReporter - >连接有效性,超时,自动重新连接

在我hibernate.cfg.xml,我有几行代码:

<hibernate-configuration> 
    <session-factory> 
     <!-- Database connection settings --> 
     <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="connection.url">jdbc:mysql://localhost/mydbtest</property> 
     <property name="connection.username">root</property> 
     <property name="connection.password">mypassword</property> 
     <!-- JDBC connection pool (use the built-in) --> 
     <property name="connection.pool_size">20</property> 
     <!-- SQL dialect --> 
     <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 
     <!-- Enable Hibernate's automatic session context management, in this case 
      the session will be close after each transaction! --> 
     <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> 
     <!-- auto create tables --> 
     <property name="hbm2ddl.auto">none</property> 
     ... 
    </session-factory> 
</hibernate-configuration> 

我更改属性connection.pool_size的值从1到20我这样做是因为有人告诉我(我有这些问题) ,数据未加载并且连接池大小引发错误。此外,

所以我的值改为20,但现在我得到这个错误:

Hibernate: select ... 
WARN : org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: 08S01 
ERROR: org.hibernate.util.JDBCExceptionReporter - 
     The last packet successfully received from the server was 61.428.729 
     milliseconds ago. The last packet sent successfully to the server was 
     61.428. 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. 

所以,我应该怎么办,这些可能的解决方案?解决这些JDBC连接问题的最佳方法是什么?我读了一些关于c3p0,但我是连接池的新手。我不明白这个问题:我有一个20的连接池。那么为什么它会失败?为什么抛出一个异常而不打开一个新的?

非常感谢您提前&最好的问候。

- 更新 -

<property name=“hibernate.c3p0.acquire_increment”>3</property> 
<property name=“hibernate.c3p0.idle_test_period”>14400</property> 
<property name=“hibernate.c3p0.timeout”>25200</property> 
<property name=“hibernate.c3p0.max_size”>15</property> 
<property name=“hibernate.c3p0.min_size”>3</property> 
<property name=“hibernate.c3p0.max_statements”>0</property> 
<property name=“hibernate.c3p0.preferredTestQuery”>select 1;</property> 

- 更新2 -

/etc/my.cnf文件:

[mysqld] 
datadir=/var/lib/mysql 
socket=/var/lib/mysql/mysql.sock 
user=mysql 
# Disabling symbolic-links is recommended to prevent assorted security risks 
symbolic-links=0 

[mysqld_safe] 
log-error=/var/log/mysqld.log 
pid-file=/var/run/mysqld/mysqld.pid 
+0

问题解决了吗?什么改变了它的工作? – 2013-05-04 07:32:37

回答

2

这通常闲置一段时间后发生, MySQL关闭了服务器端的连接。但是客户端(你的应用程序)的连接仍然可用,所以它最终会变得陈旧。

通过使用连接池机制,它负责在需要时打开/关闭连接,避免这种问题。正如错误消息所述,一种解决方案是在URL中使用JDBC选项autoReconnect = true。另一种是配置您的连接池机制,以便在交付给您的应用程序之前打开/测试连接。

+0

我更新了我的问题。我发现了这些`c3p0`配置。这也会起作用吗? – Tim 2011-01-29 09:23:31