小编Jay*_*man的帖子

Hibernate/MySQL连接超时 - 尝试处理线程池执行器,退出后不释放与C3P0的Hibernate连接

我有一个使用MySQL的Tomcat应用程序和用于ORM的Hibernate.我们的应用程序的性质要求我们必须从NoSQL存储中为每个请求提取和聚合大量分析数据,因此我们将每个请求的提取和聚合分成几个任务,并将这些数据委托给线程池执行服务.

当每个线程执行任务时,它需要查询/更新MySQL有关某些事情,所以它借用了C3P0(我们用于连接池)的Hibernate会话.

基本配置:

    <property name="current_session_context_class">thread</property>
    <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
    <property name="hibernate.connection.shutdown">true</property>
    <property name="hibernate.use_sql_comments">false</property>

<!-- C3p0 Performance Improvements -->
    <property name="hibernate.c3p0.acquire_increment">1</property>
    <property name="hibernate.c3p0.idle_test_period">300</property>
    <property name="hibernate.c3p0.maxConnectionAge">3600</property>
    <property name="hibernate.c3p0.timeout">120</property>
    <property name="hibernate.c3p0.max_size">300</property>
    <property name="hibernate.c3p0.min_size">1</property>
    <property name="hibernate.c3p0.max_statements">100</property>
    <property name="hibernate.c3p0.preferredTestQuery">select 1;</property>
Run Code Online (Sandbox Code Playgroud)

问题是Hibernate请求在8小时后导致MySQL/JDBC连接超时错误(我们配置的MySQL的wait_timeout参数值是默认值,即8小时).我通过将wait_timeout设置为11分钟来复制这个,但是对于8小时的wait_timeout,结果也是相同的:

2013-01-27 20:08:00,088 ERROR [Thread-0] (JDBCExceptionReporter.java:234) - Communications link failure

The last packet successfully received from the server was 665,943 milliseconds ago.  The last packet sent successfully to the server was 6 milliseconds ago.
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) …
Run Code Online (Sandbox Code Playgroud)

java mysql hibernate c3p0 connection-timeout

6
推荐指数
1
解决办法
2304
查看次数

即使在设置c3p0.testConnectionOnCheckout = true之后,与MySQL的数据库连接也会超时

我有一个使用hibernate(v3.6.4)的应用程序,其中连接池由C3P0(v0.9.1.2)提供.

问题是如果我进行数据库查询,如果应用程序进程(以及C3P0池)运行的时间超过MySQL wait_timeout值,则会导致JDBC通信链接失败.为了测试这个问题,我将/etc/mysql/my.cnf中的wait_timeout值设置为600秒:

2013-01-27 20:08:00,088 ERROR [Thread-0] (JDBCExceptionReporter.java:234) - Communications link failure

The last packet successfully received from the server was 665,943 milliseconds ago.  The last packet sent successfully to the server was 6 milliseconds ago.
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.criteria.CriteriaLoader.list(CriteriaLoader.java:119)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1716)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347) 
.....
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 665,943 milliseconds ago.  The last packet sent …
Run Code Online (Sandbox Code Playgroud)

java mysql hibernate jdbc c3p0

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

c3p0 ×2

hibernate ×2

java ×2

mysql ×2

connection-timeout ×1

jdbc ×1