2017-05-17 135 views
1

我使用Hibernate和c3p0在我的應用程序中管理DAO。Hibernate/c3p0:連接池已滿:「池已經最大化」錯誤

一段時間後有問題:應用程序,更確切地說是BDD請求,不起作用。原因:連接池已滿。

我有以下消息:

com.mchange.v2.resourcepool.BasicResourcePool - acquire test -- pool is already maxed out. [managed: 20; max: 20] 
[email protected] [managed: 20, unused: 0, excluded: 0] (e.g. [email protected]) 

看來,連接永遠不會關閉,在日誌文件中,我們可以看到,直到它達到最大(20連接池充滿在每個請求,看上述錯誤):

[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 - [email protected]: Adding task to queue -- com[email protected]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(): [email protected] 
[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 [email protected] [managed: 16, unused: 0, excluded: 0] (e.g. [email protected]) 
[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.imp[email protected]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 [email protected] [managed: 17, unused: 1, excluded: 0] (e.g. [email protected]) 
[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 [email protected] [managed: 17, unused: 0, excluded: 0] (e.g. [email protected]) 
[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 

我已經設置了以下配置爲C3P0:

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 

這是我如何提出請求的示例:

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(); 
} 

問題是什麼?我在c3p0的代碼或配置中管理連接/請求的方式?

謝謝你的幫助。

回答

1

有幾件事情我在這裏看到:

  1. 您使用hibernate.connection.release_mode = on_close將掛在連接的休眠會話的持續時間。在這裏尋找該設置的附加選項: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個連接。檢查應用程序使用情況數據以確定這是否是合理的數字。根據您的硬件和使用情況,即使是min_size,也可能發現20個連接太低。
+0

謝謝你的迴應。我改變了一些屬性:'release_mode = after_transaction' 'hibernate.c3p0.timeout = 300'和'hibernate.c3p0.idle_test_period = 120'。 在日誌文件中,我看到它檢查連接,但它沒有改變,我已經有錯誤「maxed out error」: com.mchange.v2.resourcepool.BasicResourcePool - BEGIN檢查過期的資源。 com.mchange.v2.resourcepool.BasicResourcePool - 完成檢查已過期的資源。 池已經超出。 [託管:20;最大:20]' – Moranbzh

+0

我認爲連接池沒有「清理」,連接仍然存在。我已將最大連接數保持在20個以進行測試。目前,增加連接的最大數目不是解決方案,在任何時候池會滿。 – Moranbzh

+1

儘量讓'hibernate.c3p0.timeout' - 當連接過期並銷燬較大的'hibernate.c3p0.idle_test_period'時 - 儘早檢測問題。也可以使用jstack來查看池線是否堵塞。 – divanov