2015-01-13 145 views
7

我在一個webapp(hibernate-core-4.3.8.Final和spring 3.2.11.RELEASE)上使用hibernate的spring。 我正在使用hikaricp(v 2.2.5)作爲連接池impl,它檢測連接泄漏並打印下面的堆棧跟蹤。我使用spring的聲明式事務劃分,所以我假定資源的管理和清理是通過spring/hibernate完成的。因此,我認爲spring或hibernate是檢測到連接泄漏的原因。春季或休眠連接泄漏

基本上,有一個定時器在啓動時調用一個標有@Transactional註釋的spring bean。

@Transactional public class InvoiceCycleExporter { 
public runExportInvoiceCycleJob(){ 
    //this method when called is **sometimes** leaking a connection .... 
} } 

你能幫我追蹤連接泄漏的來源嗎?

我appcontext.xml配置爲數據源,連接池的,但EntityManager低於低於

<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig"> 
    <property name="jdbcUrl" value="${jdbc.url}"/> 
    <property name="username" value="${jdbc.user}"/> 
    <property name="password" value="${jdbc.password}"/> 
    <property name="maximumPoolSize" value="${jdbc.maximumPoolSize}"/> 
    <property name="driverClassName" value="org.postgresql.Driver"/> 
    <property name="leakDetectionThreshold" value="${jdbc.leakDetectionThreshold}"/> 
</bean> 

<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="shutdown"> 
    <constructor-arg ref="hikariConfig"/> 
</bean> 

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">   
    <property name="dataSource" ref="dataSource"/> 
    <property name="persistenceUnitName" value="velosPU"/> 
    <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>  //more stuff .... 
</bean> 

堆棧跟蹤:

2015-01-13 14:25:00.123 [Hikari Housekeeping Timer (pool HikariPool-0)] WARN com.zaxxer.hikari.util.LeakTask - Connection leak detection triggered, stack trace follows 
java.lang.Exception: null 
     at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final] 
     at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:380) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final] 
     at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:228) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final] 
     at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:171) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final] 
     at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final] 
     at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:162) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final] 
     at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1435) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final] 
     at org.hibernate.jpa.internal.TransactionImpl.begin(TransactionImpl.java:61) ~[hibernate-entitymanager-4.3.8.Final.jar:4.3.8.Final] 
     at org.springframework.orm.jpa.DefaultJpaDialect.beginTransaction(DefaultJpaDialect.java:70) ~[spring-orm-3.2.11.RELEASE.jar:3.2.11.RELEASE] 
     at org.springframework.orm.jpa.vendor.HibernateJpaDialect.beginTransaction(HibernateJpaDialect.java:61) ~[spring-orm-3.2.11.RELEASE.jar:3.2.11.RELEASE] 
     at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:378) ~[spring-orm-3.2.11.RELEASE.jar:3.2.11.RELEASE] 
     at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:372) ~[spring-tx-3.2.11.RELEASE.jar:3.2.11.RELEASE] 
     at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:417) ~[spring-tx-3.2.11.RELEASE.jar:3.2.11.RELEASE] 
     at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:255) ~[spring-tx-3.2.11.RELEASE.jar:3.2.11.RELEASE] 
     at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94) ~[spring-tx-3.2.11.RELEASE.jar:3.2.11.RELEASE] 
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[spring-aop-3.2.11.RELEASE.jar:3.2.11.RELEASE] 
     at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:633) ~[spring-aop-3.2.11.RELEASE.jar:3.2.11.RELEASE] 
     at com.ukfuels.velos.services.bl.internalinterface.impl.bl.invoicing.**InvoiceCycleExporter (this is the spring bean marked with the transactional annotation)**$$EnhancerBySpringCGLIB$$519c078f.runExportInvoiceCycleJob(<generated>) ~[spring-core-3.2.11.RELEASE.jar:na] 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_65] 
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_65] 
     at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:67) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.impl.ProcessorEndpoint.onExchange(ProcessorEndpoint.java:103) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.impl.ProcessorEndpoint$1.process(ProcessorEndpoint.java:71) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.processor.SendProcessor$2.doInAsyncProducer(SendProcessor.java:122) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.impl.ProducerCache.doInAsyncProducer(ProducerCache.java:298) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:117) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:99) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:72) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:99) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.processor.interceptor.BacklogTracerInterceptor.process(BacklogTracerInterceptor.java:84) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:99) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.processor.interceptor.TraceInterceptor.process(TraceInterceptor.java:91) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.processor.RedeliveryErrorHandler.processErrorHandler(RedeliveryErrorHandler.java:391) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:273) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.processor.RouteContextProcessor.processNext(RouteContextProcessor.java:46) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.processor.interceptor.DefaultChannel.process(DefaultChannel.java:335) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.processor.RouteContextProcessor.processNext(RouteContextProcessor.java:46) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.processor.UnitOfWorkProcessor.processAsync(UnitOfWorkProcessor.java:150) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.processor.UnitOfWorkProcessor.process(UnitOfWorkProcessor.java:117) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.processor.RouteInflightRepositoryProcessor.processNext(RouteInflightRepositoryProcessor.java:48) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:99) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:72) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.processor.loadbalancer.QueueLoadBalancer.process(QueueLoadBalancer.java:44) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:99) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.processor.loadbalancer.QueueLoadBalancer.process(QueueLoadBalancer.java:71) ~[camel-core-2.11.4.jar:2.11.4] 
     at org.apache.camel.component.quartz.QuartzEndpoint.onJobExecute(QuartzEndpoint.java:113) ~[camel-quartz-2.11.4.jar:2.11.4] 
     at org.apache.camel.component.quartz.CamelJob.execute(CamelJob.java:61) ~[camel-quartz-2.11.4.jar:2.11.4] 
     at org.quartz.core.JobRunShell.run(JobRunShell.java:223) ~[quartz-1.8.6.jar:na] 
     at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549) ~[quartz-1.8.6.jar:na] **(a timer is triggered)** 

回答

0

的問題是在大這裏討論:

https://github.com/brettwooldridge/HikariCP/issues/34

縮小問題範圍:

  1. 嘗試彈簧4與Hibernate 4
  2. 嘗試另一個數據源,看看問題仍然存在。
+0

感謝您的回覆。我在使用c3p0數據源之前已經發生了hikari數據源和泄漏事件。所以我猜測連接是否被postgres過早刪除(不太可能是因爲postgres.conf和linux os中的tcp_keep_alive相關設置沒有提示),或者spring/hibernate沒有正確釋放連接。我正在使用最新的hibernate作爲當前的4.3.8.Final。我不確定我是否可以升級到最新的春季,但會進一步調查 – abdel

+0

,這也可能與春天的bug SPR-12280有關。 – abdel

9

這裏是一些信息,我認爲可能是一般

  • 排除誤報警調試連接泄漏有用。例如,如果您有一個可能運行5分鐘的事務(例如,在生成大型報告時) 並且您已將'leakDetectionThreshold'參數(或任何等效於您選擇的池實施的參數)設置爲4分鐘,則任何時間超過4分鐘的交易都會被報告爲泄漏,儘管它可能不是真正的泄漏(交易可能在5分鐘後正常完成並充分釋放所有連接)。
  • 如果您使用連接池但發現自己的連接不足偶爾然後想想如何配置您的池。在我的情況下,我配置了'maximumPoolSize'= 100的池。 Hikari會自動默認'minimumIdle'配置(HikariCP試圖在池中保留的最小空閒連接數),與'maximumPoolSize'的配置相同,因此在啓動池時將初始化所有最多100個連接。但是這意味着當'maxLifetime'(池中連接的最大生存期)激活時,所有連接必須在同一時間更新。這導致陡峭暫時減少可用連接。在我的日誌中,我偶爾會看到以下幾行。

    HikariPool-0(總= 100,INUSE = 0,無濟於事= 100,等待= 0)
    HikariPool-0(總= 4,INUSE = 0,無濟於事= 4,等待= 0)
    HikariPool- 0(total = 100,inUse = 0,avail = 100,waiting = 0)

    第二行,其中可用連接數降到只有4,即達到'maxLifetime'並且需要更新連接。因此,在配置池時,請採取某種方式,以便連接在不同時間到期。在我的情況下,我只是將'minimumIdle'更改爲40,這意味着當服務器負載增加時,新連接將增量爲獲得(觀察您的池impl是否提供等價於minToAcquire屬性),因此這些連接將有不同的過期日期。

  • 您的連接'maxLifetime'需要小於您的數據庫分配/期望的連接數,以便最終不會導致池中的連接無效。 更新:某些數據庫可能會在一段時間後強制斷開連接。例如,postgres有一個'connectionTimeout'和'socketTimeout'選項。因此,在應用程序的連接池中,您不希望保持連接的時間超過此db-enforced-connection-timeout,否則您將保留無效/已丟棄的連接。

+0

你能詳細說說「數據庫分配/期望連接」嗎? – user7294900

+1

對於遲到的回覆感到抱歉。基本上有些數據庫可能會在某段時間後強制斷開連接。例如,postgres有一個'connectionTimeout'和'socketTimeout'選項。因此,在您的應用程序的連接池中,您不希望保持連接的時間超過此db-enforced-connection-timeout,否則您將保持無效/已斷開的連接 – abdel

+0

謝謝 - 已更新。 – abdel