Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Hibernate/mysql连接问题_Java_Mysql_Hibernate - Fatal编程技术网

Java Hibernate/mysql连接问题

Java Hibernate/mysql连接问题,java,mysql,hibernate,Java,Mysql,Hibernate,我们正在使用Hibernate3ORM开发一个JavaStruts2框架Web应用程序。我们使用mysql作为数据库 我们在日志中发现了一些与数据库相关的异常。与数据库的连接在配置的时间之前超时。以下是我们发现的公共例外情况 104343235 [pool-6-thread-19] ERROR org.hibernate.util.JDBCExceptionReporter - The last packet successfully received from the server was

我们正在使用Hibernate3ORM开发一个JavaStruts2框架Web应用程序。我们使用mysql作为数据库

我们在日志中发现了一些与数据库相关的异常。与数据库的连接在配置的时间之前超时。以下是我们发现的公共例外情况

104343235 [pool-6-thread-19] ERROR org.hibernate.util.JDBCExceptionReporter  - The last packet successfully received from the server was 100,838,460 milliseconds ago.  The last packet sent successfully to the server was 100,838,461 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.

org.hibernate.exception.JDBCConnectionException: could not execute query
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:99)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.loader.Loader.doList(Loader.java:2536)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
    at org.hibernate.loader.Loader.list(Loader.java:2271)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:452)
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1268)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
    at com.myproject.model.dao.entities.EntitiesDAO.getByIds(EntitiesDAO.java:148)

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 100,838,460 milliseconds ago.  The last packet sent successfully to the server was 100,838,461 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.
    at sun.reflect.GeneratedConstructorAccessor345.newInstance(Unknown Source)104343242 [pool-6-thread-16] ERROR org.hibernate.util.JDBCExceptionReporter  - The last packet successfully received from the server was 100,838,544 milliseconds ago.  The last packet sent successfully to the server was 100,838,544 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.
104343242 [pool-6-thread-16] ERROR org.hibernate.util.JDBCExceptionReporter  - The last packet successfully received from the server was 100,838,544 milliseconds ago.  The last packet sent successfully to the server was 100,838,544 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.

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)
    at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3829)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2449)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2629)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2719)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2318)
    at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)
    at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1953)
    at org.hibernate.loader.Loader.doQuery(Loader.java:802)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
    at org.hibernate.loader.Loader.doList(Loader.java:2533)
    ... 21 more
每天我们都会重新启动我们的应用服务器,作为对问题的临时修复


任何帮助都将不胜感激。

之所以会出现这种情况,是因为MySQL服务器会在超时后终止连接,而不进行任何活动。 您需要在dbcp配置中添加几行代码 尝试添加以下参数:

validationQuery="SELECT 1"
testOnBorrow="true
工作原理:连接池在返回连接之前尝试运行validationQuery。如果validationQuesry失败,dbcp将放弃连接,创建一个新连接并返回它

下面是一个例子:

<Resource   name="jdbc/cooldatabase"
            description="Strandls.com license database"
            auth="Container"
            type="javax.sql.DataSource"
            factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/cooldatabase?autoReconnect=true"
            username="cooluser"
            password="coolpassword"
            initialSize="0"
            maxActive="20"
            maxIdle="10"
            minIdle="0"
            maxWait="-1"
            validationQuery="SELECT 1"
            testOnBorrow="true"
            poolPreparedStatements="true"
            removeAbandoned="true"
            removeAbandonedTimeout="60"
            logAbandoned="true"/>


您可以在此处找到完整的详细信息:

将连接池与Hibernate一起使用。例如,
c3p0

我面临着同样的问题。您是如何解决这个问题的?如何为c3p0连接池实现相同的功能?到目前为止,我从未使用过c3p0,但根据讨论,有一个名为testConnectionOnCheckout的属性:
true
是完整的讨论:源:
testConnectionOnCheckout
必须在
c3p0中设置。属性
,默认值为false。不要使用它,这个功能非常昂贵。如果设置为true,将在每次连接签出时执行操作,以验证连接是否有效。更好的选择是使用c3p0定期验证连接。idleConnectionTestPeriod。我们使用的是-300,这是您提到的还是另一个?我的错,c3p0。idle\u测试\u period是覆盖c3p0.idleConnectionTestPeriod的休眠配置键。