Hibernate / c3p0:连接池已满:"池已经超出"错误

时间:2017-05-17 13:05:15

标签: java hibernate java-ee c3p0

我使用Hibernate和c3p0在我的应用程序中管理DAO。

一段时间后出现问题:应用程序,更确切地说是BDD请求,不起作用。原因:连接池已满。

我有以下消息:

[2017/05/11 10:06:48] [http-bio-0.0.0.0-20443-exec-4] DEBUG (LogicalConnectionImpl.java:226) org.hibernate.engine.jdbc.internal.LogicalConnectionImpl - Obtaining JDBC connection
[2017/05/11 10:06:48] [http-bio-0.0.0.0-20443-exec-4] DEBUG (BasicResourcePool.java:587) com.mchange.v2.resourcepool.BasicResourcePool - acquire test -- pool size: 16; target_pool_size: 16; desired target? 17
[2017/05/11 10:06:48] [http-bio-0.0.0.0-20443-exec-4] DEBUG (BasicResourcePool.java:450) com.mchange.v2.resourcepool.BasicResourcePool - incremented pending_acquires: 1
[2017/05/11 10:06:48] [http-bio-0.0.0.0-20443-exec-4] DEBUG (BasicResourcePool.java:1788) com.mchange.v2.resourcepool.BasicResourcePool - Starting acquisition series. Incremented pending_acquires [1],  attempts_remaining: 30
[2017/05/11 10:06:48] [http-bio-0.0.0.0-20443-exec-4] DEBUG (ThreadPoolAsynchronousRunner.java:236) com.mchange.v2.async.ThreadPoolAsynchronousRunner - com.mchange.v2.async.ThreadPoolAsynchronousRunner@67c8303a: Adding task to queue -- com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@232613e3
[2017/05/11 10:06:48] [http-bio-0.0.0.0-20443-exec-4] DEBUG (BasicResourcePool.java:1390) com.mchange.v2.resourcepool.BasicResourcePool - awaitAvailable(): com.mchange.v2.c3p0.impl.NewPooledConnection@547a79cf
[2017/05/11 10:06:48] [http-bio-0.0.0.0-20443-exec-4] DEBUG (BasicResourcePool.java:1747) com.mchange.v2.resourcepool.BasicResourcePool - trace com.mchange.v2.resourcepool.BasicResourcePool@240ab5e6 [managed: 16, unused: 0, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@547a79cf)
[2017/05/11 10:06:48] [C3P0PooledConnectionPoolManager[identityToken->2v3o059n1wfndya61hxar|3b29a008]-HelperThread-#1] DEBUG (GooGooStatementCache.java:333) com.mchange.v2.c3p0.stmt.GooGooStatementCache - checkinAll(): com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats -- total size: 45; checked out: 0; num connections: 16; num keys: 45
[2017/05/11 10:06:48] [C3P0PooledConnectionPoolManager[identityToken->2v3o059n1wfndya61hxar|3b29a008]-HelperThread-#1] DEBUG (C3P0PooledConnectionPool.java:283) com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool - com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager@58b73780.acquireResource() returning. 
[2017/05/11 10:06:48] [C3P0PooledConnectionPoolManager[identityToken->2v3o059n1wfndya61hxar|3b29a008]-HelperThread-#1] DEBUG (BasicResourcePool.java:1747) com.mchange.v2.resourcepool.BasicResourcePool - trace com.mchange.v2.resourcepool.BasicResourcePool@240ab5e6 [managed: 17, unused: 1, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@547a79cf)
[2017/05/11 10:06:48] [C3P0PooledConnectionPoolManager[identityToken->2v3o059n1wfndya61hxar|3b29a008]-HelperThread-#1] DEBUG (BasicResourcePool.java:471) com.mchange.v2.resourcepool.BasicResourcePool - decremented pending_acquires: 0
[2017/05/11 10:06:48] [C3P0PooledConnectionPoolManager[identityToken->2v3o059n1wfndya61hxar|3b29a008]-HelperThread-#1] DEBUG (BasicResourcePool.java:1825) com.mchange.v2.resourcepool.BasicResourcePool - Acquisition series terminated successfully. Decremented pending_acquires [0],  attempts_remaining: 30
[2017/05/11 10:06:48] [http-bio-0.0.0.0-20443-exec-4] DEBUG (BasicResourcePool.java:1747) com.mchange.v2.resourcepool.BasicResourcePool - trace com.mchange.v2.resourcepool.BasicResourcePool@240ab5e6 [managed: 17, unused: 0, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@547a79cf)
[2017/05/11 10:06:48] [http-bio-0.0.0.0-20443-exec-4] DEBUG (LogicalConnectionImpl.java:232) org.hibernate.engine.jdbc.internal.LogicalConnectionImpl - Obtained JDBC connection

似乎连接永远不会关闭,在日志文件中我们可以看到每个请求都填充了连接池,直到它达到最大值(20,见上面的错误):

hibernate.connection.release_mode=on_close
hibernate.c3p0.validate=true
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50
hibernate.c3p0.preferredTestQuery=SELECT 1;
hibernate.c3p0.idle_test_period=3000
hibernate.c3p0.acquire_increment=1

我为c3p0设置了以下配置:

public static void updateSignatureLogin(String login, int transactionId) {
    createEntityManagerFactory();
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    entityManager.getTransaction().begin();
    IsignSignaturetraceEntity dbsignature = entityManager.find(IsignSignaturetraceEntity.class, transactionId);
    dbsignature.setLogin(login);
    entityManager.persist(dbsignature);
    entityManager.merge(dbsignature);
    entityManager.flush();
    entityManager.getTransaction().commit();
}

这是我如何提出请求的一个例子:

RS

有什么问题?我在c3p0的代码或配置中管理连接/请求的方式?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我在这里看到的一些事情是:

  1. 你正在使用hibernate.connection.release_mode = on_close,它将在hibernate会话期间挂起连接。在此处查看该设置的其他选项:https://access.redhat.com/documentation/en-US/Red_Hat_JBoss_Web_Server/1.0/html/Hibernate_Core_Reference_Guide/transactions-connection-release.html
  2. 您手动管理事务边界,而不是允许事务管理器为您执行此操作。如果您使用的是Spring,则可以配置声明式事务管理。有关详细信息,请参阅此处:http://docs.spring.io/spring-framework/docs/4.2.x/spring-framework-reference/html/transaction.html
  3. 您的连接池最多只有20个连接。检查应用程序使用情况数据以确定这是否是合理的数字。根据您的硬件和使用情况,您甚至可以发现20个连接对于min_size来说太低了。