您的位置:首页 > 产品设计 > UI/UE

org.hibernate.exception.JDBCConnectionException: could not execute queryMySQL数据库连接超时

2016-04-30 08:59 495 查看
最近写了一个报名的小页面放在了服务器上,早上访问的时候竟然发现又出现了错误:

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

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

百度翻译是:成功从服务器收到最后一包是34231574毫秒之前。成功发送到服务器的最后一个数据包是34231574毫秒前。比服务器配置值的wait_timeout”。你应该考虑过期和/或测试连接之前使用的有效性在您的应用程序,增加客户端超时的服务器配置值,或使用连接器/J连接属性”autoreconnect =真”来避免这个问题。

3.java.net.SocketException: Software caused connection abort: socket write error

这个是因为MySQL数据库的“wait_timeout"时间默认为28800秒,也就是8小时,超过8小时没有客户端进行链接操作的话,MySQL数据库会自动断开外部连接,当再次访问时,就会报以上错误,再第二次访问的时候连接就正常了,也就能正常访问了,由于写的匆忙,没有使用数据库连接池,使用数据库连接池就可以在连接数据库时测试连接有效性,从而解决上面的问题了;

Hibernate支持如下的连接池:

   DriverManagerConnectionProvider:代表由Hibernate提供的默认的数据库连接池

   C3P0ConnectionProvider:代表C3P0连接池

   ProxoolConnectionProvider:代表Proxool连接池

   DBCPConnectionProvider:代表DBCP连接池

   DatasourceConnectionProvider:代表在受管理环境中由容器提供的数据源

  其中,默认连接池并不支持在分配一个连接时,测试其有效与否的功能,而C3P0、Proxool、DBCP都提供了这样的功能,正好可以解决上述问题。综合考虑各个连接池的效率、稳定性、易用性,在这以Proxool为例,关于C3P0的相关配置请移步到之前写的一篇博文 数据库已更新,前台查询信息不同步更新问题解决方法

接下来说一下Proxool连接池的配置:

①首先在Hibernate.cfg.xml中:

<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”>true</property>
<property name=”dialect”>org.hibernate.dialect.MySQLDialect</property>

<mapping resource=”com/soft/pojo/User.hbm.xml” />
…
</session-factory>


其中各属性含义如下:

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

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

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

②然后是引用的proxool.xml配置文件

<?xml version=”1.0″ encoding=”UTF-8″?>
<!--  the proxool configuration can be embedded within your own application’s.
Anything outside the “proxool” tag is ignored.  -->
<something-else-entirely>
<proxool>

<!-- proxool别名 -->
<alias>mysql</alias>

<!-- 数据库连接Url  -->
<driver-url>
jdbc:mysql://localhost/yourDatebase?useUnicode=true&characterEncoding=UTF-8
</driver-url>

<!-- JDBC驱动名称  -->
<driver-class>com.mysql.jdbc.Driver</driver-class>

<!-- 数据库连接帐号  -->
<driver-properties>
<property name=”user” value=”root” />
<property name=”password” value=”password” />
</driver-properties>

<!-- proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回收,超时的销毁  -->
<house-keeping-sleep-time>90000</house-keeping-sleep-time>

<!-- 指因未有空闲连接可以分配而在队列中等候的最大请求数,超过这个请求数的用户连接就不会被接受  -->
<maximum-new-connections>20</maximum-new-connections>

<!-- 最少保持的空闲连接数  -->
<prototype-count>3</prototype-count>

<!-- 允许最大连接数,超过了这个连接,再有请求时,就排在队列中等候,最大的等待请求数由maximum-new-connections决定  -->
<maximum-connection-count>50</maximum-connection-count>

<!-- 最小连接数 -->
<minimum-connection-count>3</minimum-connection-count>

<!--  在分配连接前后是否进行有效性测试,这个是解决本问题的关键 -->
<test-before-use>true</test-before-use>
<test-after-use>true</test-after-use>

<!-- 用于测试的SQL语句 一定要写(不知道问什么) -->
<house-keeping-test-sql>SELECT CURRENT_USER</house-keeping-test-sql>

</proxool>
</something-else-entirely>


③下载和安装Proxool的jar包

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

  Proxool配置完成。重新启动Tomcat,再次做上述测试,问题解决。

④ 如果有小伙伴们感觉配置比较费劲的话,那可以使用C3P0连接池,看这:数据库已更新,前台查询信息不同步更新问题解决方法

将代码直接copy到Hibernate.cfg.xml的session-factory标签中即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: