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

前端 未结 2 476
醉梦人生
醉梦人生 2021-01-23 05:15

I\'ve got a J2EE application which uses JSP pages as front end and Struts2 as controller. I also use Hibernate to map my objects into a MySql DB. when I deploy the application o

相关标签:
2条回答
  • 2021-01-23 05:37

    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.

    0 讨论(0)
  • 2021-01-23 06:03

    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>
    
    0 讨论(0)
提交回复
热议问题