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

 ̄綄美尐妖づ 提交于 2019-12-02 01:43:45

Hibernate uses a primitive connection pooling mechanism, which is not suitable for production purposes (it even says that in the log file!). You should always use a connection pooling mechanism, be it the one provided by your container, be it a mechanism bundled in your application (c3p0, for instance). The exception you are seeing is because MySQL closed an "old" connection which Hibernate is still using. You can try to add a JDBC URL option, like "autoReconnect", but that's really not a long-term solution.

Ideally, you should configure your container to supply the connections to your application through JNDI. This is easily done with a "-ds.xml" file in JBoss or with a context.xml for Tomcat.

Once I get a similary looking exception, when I used a connection pool and the connection was closed after some time, but the pool did not recognized it.

What helped me in this case was to add a validation query to my data source:

<bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
  ...
  <property name="validationQuery" value="select 1"/>
</bean>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!