Hibernate JDBCExceptionReporter - >连接有效性,超时,自动重新连接

时间:2011-01-29 09:06:15

标签: mysql hibernate jdbc java-ee

我在使用MySQL和MySQL Connector / J(MySQL官方JDBC驱动程序)的Java EE应用程序中遇到Hibernate(3.6.0.-Final)问题。

在我的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

1 个答案:

答案 0 :(得分:2)

这通常在一段时间不活动后发生,并且MySQL在服务器端关闭了连接。但是客户端(您的应用程序)中的连接仍然可用,因此,它最终会变得陈旧。

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