最近在做一个项目,测试的时候是没有问题的,但是放到服务器上以后,第二天就会出现下面的异常。重启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

转载于:https://www.cnblogs.com/pghWord/p/5153226.html

org.hibernate.exception.JDBCConnectionException: could not execute query相关推荐

  1. Caused by: com.primeton.das.entity.impl.hibernate.exception.GenericJDBCException: could not execute

    Caused by: com.primeton.das.entity.impl.hibernate.exception.GenericJDBCException: could not execute ...

  2. org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

    Could not execute JDBC batch update 这个是不能执行批量更新,看看配置文件里 batch-size 属性有没有设置 Cannot add or update a ch ...

  3. threw exception [Request processing failed; nested exception is org.hibernate.exception.SQLGrammarE

    在这里插入代码片> threw exception [Request processing failed; > nested exception is org.hibernate.exce ...

  4. javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not prep

    2019独角兽企业重金招聘Python工程师标准>>> javax.persistence.PersistenceException: org.hibernate.exception ...

  5. ClickHouse【环境搭建 02】设置用户密码的两种方式(明文+SHA256)及新用户添加及只读模式 Cannot execute query in readonly mode 问题解决

    1.查看user.xml文件可知设置密码的多种方式 <!-- Password could be specified in plaintext or in SHA256 (in hex form ...

  6. jbpm4 org.hibernate.exception.ConstraintViolationException

    昨天同事调试jbpm4的程序时出现了一个极不易察觉的错误,帮忙弄了半天才找到原因. jbpm4使用MySQL作为数据库在调用repositoryService.deleteDeploymentCasc ...

  7. org.hibernate.exception.SQLGrammarException: Error calling Driver#connect

    2019独角兽企业重金招聘Python工程师标准>>> 1.hibernate报错 org.hibernate.exception.SQLGrammarException: Erro ...

  8. Could not execute query against OLE DB provider 'OraOLEDB.Oracle'

    Could not execute query against OLE DB provider 'OraOLEDB.Oracle' 这段错误目前我遇到的状况是在SQL Server上用openquer ...

  9. 报错,could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarExc

    1.问题 {     "timestamp": "2020-11-28 11:18:17",     "status": 404,     ...

最新文章

  1. thinkphp6 接收不到数据_单片机红外接收与红外发射
  2. HBase性能优化方法总结(1):配置优化
  3. 初探目前最火的鸿蒙系统,跑了个 “hello world”!
  4. 微信二次修改微信号_微信支持修改微信号了!功能正式上线,附详细教程
  5. taskset设置CPU affinity
  6. speedtest-cli命令行下测试服务器外网速度
  7. 容纳10万人服务器多少钱_令人尴尬的故事:为什么我的服务器只能容纳10名玩家...
  8. 3842开关电源完整原理图_8大常见开关电源电路分析,让你项目设计更轻松!
  9. 每个tabpage中都有一个dategridview_宇宙中每个原子里都隐藏着一个巨大的秘密
  10. Java Mysql数据库创建视图、索引、备份和恢复
  11. 自学数据科学机器学习,19个数学和统计学公开课推荐
  12. 电商直播运营的思路流程
  13. 调用手机扫描二维码功能
  14. c++教你唱响天空之城(源码奉上)
  15. 常微分方程-差分方程
  16. AMM引入无限网格策略,变无常损失为阿尔法收益
  17. 皮卡丘(pikachu)不安全的文件下载
  18. 深度学习在美团点评的应用
  19. block、多线程与GCD总结
  20. windows mobile 的Start menu 为什么不能透明背景是吧

热门文章

  1. I Love Palindrome String HDU - 6599 回文树+hash
  2. [vuex] unknown mutation type: set_userinfo
  3. 一步一步教你构建一个MPU6050(I2C类)驱动(一)
  4. MM模块库存管理常用表
  5. 交出助残“高分卷”,这家湖南企业答了“三道题”
  6. 将不同数据存储到数据库中_如何将数据存储在兔子洞中
  7. 高端优雅简历模板打包下载(附找工作指南)
  8. 慕测JMeter性能测试——咪咕音乐歌手搜索
  9. 在word中编辑matlab图,基于Matlab在Word中插入函数图形
  10. 机器视觉方面有哪些好的开发平台?各有什么特点?