最近在做一个项目,测试的时候是没有问题的,但是放到服务器上以后,第二天就会出现下面的异常。重启Tomcat服务器后就正常了,但是下一天还是会出现同样的异常..... 我就查了一些资料最终把问题给解决了!

org.hibernate.exception.JDBCConnectionException: could not execute query
 org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
 org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
 org.hibernate.loader.Loader.doList(Loader.java:2148)
 org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
 org.hibernate.loader.Loader.list(Loader.java:2024)
 org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:375)
 org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:308)
 org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:153)
 org.hibernate.impl.SessionImpl.list(SessionImpl.java:1106)
 org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
 com.schoolstar.dao.impl.UserDaoImpl.findAllverifyOkUsers(UserDaoImpl.java:53)
 com.schoolstar.services.impl.UserServiceImpl.findAllverifyOkUsers

.....................................................

** BEGIN NESTED EXCEPTION **

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
MESSAGE: The last packet successfully received from the server was41577 milliseconds ago.The last packet sent successfully to the server was 41577 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 was41577 milliseconds ago.The last packet sent successfully to the server was 41577 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.
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)

...................

Caused by: java.net.SocketException: Software caused connection abort: socket write error
 at java.net.SocketOutputStream.socketWrite0(Native Method)
 at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
 at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
 at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
 at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
 at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3251)

我找了一下原因发现是: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文档中推荐的),首先导入C3P0的Jar包(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连接池解决此问题的方法:

下面就看看如何配置Proxool:
  1、Hibernate配置文件:

<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别名。

  2、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>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>

<!? 用于测试的SQL语句 一定要写(不知道问什么)?>
<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配置成功。重新启动Tomcat,再次做上述测试,问题解决。
  此外,C3P0或DHCP,还可以参考以下资料:
  http://blog.csdn.net/lip8654/archive/2008/02/26/2121387.aspx
  http://azi.iteye.com/blog/182146
  http://fishyych.iteye.com/blog/90793

org.hibernate.exception.JDBCConnectionException: could not execute query的更多相关文章

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

    SSH项目中出现了 org.hibernate.exception.SQLGrammarException: could not execute query 错误,仔细检查后发现,是把createQu ...

  2. org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query

    原因: 这个问题的解决方案很简单,主要是因为数据库中不存在相关的表或者列. org.springframework.dao.InvalidDataAccessApiUsageException: Pa ...

  3. exception is org.hibernate.exception.DataException: Could not execute JDBC batch update at

    没有什么问题,但是却报了Could not execute JDBC batch update的错,主要是配置文件设置了关联,数据却没有关联造成的,只要数据正确就没有问题. 另外,造成这个原因的还可能 ...

  4. code is 9998;desc is 插入失败exception is org.hibernate.exception.JDBCConnectionException: Could not op

    1.错误描述 [ERROR:]2015-05-05 09:27:12,090 [插入失败] org.hibernate.exception.JDBCConnectionException: Could ...

  5. org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Cannot open con

    org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session f ...

  6. 严重: Could not synchronize database state with session org.hibernate.exception.DataException: Could not execute JDBC batch update

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #ff2600 } p.p2 { margin: 0.0px 0 ...

  7. Hibernate错误:Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update

    报错:Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execu ...

  8. HTTP Status 500 - Request processing failed; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement

    1.什么操作出现:当我在项目中添加产品或者修改时,浏览器出现HTTP Status 500 - Request processing failed; nested exception is org.h ...

  9. 【hibernate】报错:org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.DataException: could not execute statement

    报错如下: org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a ...

随机推荐

  1. Python基础篇-day8

    本节目录1.抽象接口2.静态方法.类方法.属性方法3.类的特殊方法 3.1 __doc__ 表示类的描述信息(注释) 3.2 __module__ 和 __class__ 3.3 __init__ 构 ...

  2. win7提示Xshell5提示缺少msvcp110.dll解决办法

    下载地址: http://www.microsoft.com/zh-CN/download/details.aspx?id=30679 X86和X64的都下载下来,安装好后重启计算机,就OK了

  3. chrom 快捷键 整理版

    chrome窗口和标签页快捷键: Ctrl+N 打开新窗口 Ctrl+T 打开新标签页 Ctrl+Shift+N 在隐身模式下打开新窗口 Ctrl+O,然后选择文件 在谷歌浏览器中打开计算机上的文件 ...

  4. 后台gird表单按钮操作

    1.$this->_removeButton('reset');#########################################删除重置这个按钮.2.$this->_ad ...

  5. 2016年团体程序设计天梯赛-决赛 L1-7. 到底是不是太胖了(10)

    据说一个人的标准体重应该是其身高(单位:厘米)减去100.再乘以0.9所得到的公斤数.真实体重与标准体重误差在10%以内都是完美身材(即 |真实体重-标准体重| < 标准体重x10%).已知市斤 ...

  6. 【瞎搞搞之】 window_x64微信小程序环境搭建

    所需文件地址如下: http://pan.baidu.com/s/1nv0IHhn(ylk7)   1.下载微信开发工具0.7.0_x64 安装完成后,打开程序,进行微信扫码登录 2.下载微信开发工具 ...

  7. 【同行说】Android图片处理技术资料汇总(一)

    对于Android开发的童鞋们来说,图片处理时或多或少都会遇到令人头疼和不满意的问题,今天小编收集了5篇Android图片处理的干货文章,一起来看看吧! 一.Android 高清加载巨图方案 拒绝压缩 ...

  8. ConcurrentHashMap完全解析(jdk6/7,8)

    并发编程实践中,ConcurrentHashMap是一个经常被使用的数据结构,相比于Hashtable以及Collections.synchronizedMap(),ConcurrentHashMap ...

  9. 好友与组--ESFramework 4.0 进阶(11)

    大部分分布式通信系统中,都会涉及到客户端之间相互通信.以及需要将客户端进行分组的功能,或者是类似这方面的需求.ESFramework对这一常见的任务内置了强大的支持,包括从客户端到服务端.一直到Pla ...

  10. Openjudge-计算概论(A)-统计字符数

    描述: 判断一个由a-z这26个字符组成的字符串中哪个字符出现的次数最多输入第1行是测试数据的组数n,每组测试数据占1行,是一个由a-z这26个字符组成的字符串每组测试数据之间有一个空行,每行数据不超 ...