`
xiongzhenhui
  • 浏览: 209696 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

org.hibernate.exception.JDBCConnectionException: could not execute query

 
阅读更多

项目异常如下(省略了一部分):

org.hibernate.exception.JDBCConnectionException: could not execute query

…………..

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException

MESSAGE: The last packet successfully received from the server was56939 milliseconds ago.The last packet sent successfully to the server was 56939 milliseconds ago, which 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.

 

STACKTRACE:

 

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was56939 milliseconds ago.The last packet sent successfully to the server was 56939 milliseconds ago, which 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.

………………..

我找了一下原因发现是:MySQL对所有连接的有效时间默认为28800秒,正好8小时,也就是说,如果一个连接8小时没有请求和操作,就会自动断开(即使修改了MySQL连接的有效时间,问题在这里无法得到根本解决);而Hibernate中并没有配置连接池,使用的是它自带的也就是DriverManagerConnectionProvider。而这个连接池不支持在分配一个连接时,测试其有效与否的功能(不过根据异常提示可以再Jdbc连接的URL中加入属性'autoReconnect=true'),因此这个连接池并不知道它所管理的连接中是否有被MySQL断开的。如果一个程序要使用数据库连接,而Hibernte的连接池分配一个已经被MySQL断开了的给程序使用,那么便会出现错误。

我查了一下Hibernate支持如下的连接池:

1.DriverManagerConnectionProvider:代表由Hibernate提供的默认的数据库连接池    2.C3P0ConnectionProvider:代表C3P0连接池

3.ProxoolConnectionProvider:代表Proxool连接池

4.DBCPConnectionProvider:代表DBCP连接池

其他3个数据连接池都提供检查连接是否有效的功能,正好是可以解决上面的问题。这里我采用C3P0连接池(Hibernate文档中推荐的),首先导入C3P0Jar包(c3p0.jar),再在Hibernate配置中加入:

<property name="hibernate.connection.provider_class">

               org.hibernate.connection.C3P0ConnectionProvider

            </property>

            <property name="c3p0.acquire_increment">1</property>

            <property name="c3p0.idle_test_period">300</property>

            <property name="c3p0.max_size">20</property>

           <property name="c3p0.max_statements">100</property>

            <property name="c3p0.min_size">5</property>

            <property name="c3p0.timeout">90</property>

            <property name="c3p0.preferredTestQuery ">select 1 from user where id=1</property>

            <property name="c3p0.idleConnectionTestPeriod ">18000</property>           

           <property name="c3p0.maxIdleTime">25000</property>         

           <property name="c3p0.testConnectionOnCheckout">true</property>

Ok问题解决,另附使用Proxool连接池的配置方法:

1Hibernate配置文件:

<session-factory>

<property name="hibernate.connection.provider_class">org.hibernate.connection.ProxoolConnectionProvider</property>

<property name="hibernate.proxool.xml">proxool.xml</property>

<property name="hibernate.proxool.pool_alias">mysql</property>

<property name="show_sql">false</property>

<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<mapping resource="com/lab1000/jcom/pojo/Admin.hbm.xml">

...

</session-factory>

  其中各属性含义如下:

hibernate.connection.provider_class:指明使用Proxool连接池

hibernate.proxool.xml:指明Proxool配置文件所在位置,这里与Hibernate的配置文件在同一目录下

hibernate.proxool.pool_alias:指明要使用的proxool.xml中定义的proxool别名。

  2Proxool配置文件(proxool.xml):

<something-else-entirely>

<proxool>

<alias>mysql</alias>

<driver-url>

jdbc:mysql://localhost/yourDatebase?useUnicode=true&characterEncoding=UTF-8

</driver-url>

<driver-class>com.mysql.jdbc.Driver</driver-class>

<driver-properties>

<property name="user" value="root">

<property name="password" value="password">

</driver-properties>

<house-keeping-sleep-time>90000</house-keeping-sleep-time>

<maximum-new-connections>20</maximum-new-connections>

<prototype-count>3</prototype-count>

<maximum-connection-count>20</maximum-connection-count>

<minimum-connection-count>3</minimum-connection-count>

<test-before-use>true</test-before-use>

<test-after-use>true</test-after-use>

<house-keeping-test-sql>SELECT CURRENT_USER</house-keeping-test-sql>

</proxool>

</something-else-entirely>

  3、下载和安装Proxool的包文件

  下载地址:http://proxool.sourceforge.net/download.html

  下载后并解压后,将其中lib文件夹下的jar文件拷贝到你站点的WEB-INF/lib

  自此,Proxool配置成功。

 

分享到:
评论

相关推荐

    Hibernate配置常见错误

    错误表现:如“org.hibernate.exception.JDBCConnectionException: Cannot open connection”,表明Hibernate无法建立与数据库的连接。 解决方案:检查数据库URL、用户名和密码是否正确。此外,还需确认数据库服务...

    Hibernate+c3p0连接池SQLServer 2000

    [http60-Processor3][org.apache.struts.action.RequestProcessor][WARN] - Unhandled Exception thrown: class org.hibernate.exception.JDBCConnectionException [http60-Processor1][org.hibernate.util....

    MySQL数据库服务器下C3P0连接池的配置.pdf

    项目的应用程序部署好后,每过一段时间(比如一个晚上),应用程序没有被调用,然后一旦程序访问MySQL数据库就会抛出异常:java代码org.hibernation.exception.JDBCConnectionException:could not execute query。...

    MySQL+Hibernate下连接空闲8小时自动断开问题解决方案.docx

    在本文中,出现了一个javax.servlet.ServletException: org.hibernate.exception.JDBCConnectionException 异常,这是由于连接idle时间太长导致的。这个异常是 Hibernate 在试图执行查询时抛出的,表示JDBC 连接已经...

    Hibrenate 学习摘记

    - **JDBCConnectionException**:当使用 JDBC 遇到连接问题时,如无法建立连接或连接超时等,可能会抛出此异常。 - **SQLGrammarException**:如果执行的 SQL 语句语法有误,则会抛出该异常。 - **...

Global site tag (gtag.js) - Google Analytics