为什么Hibernate不会返回Wildfly 12上的池连接?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么Hibernate不会返回Wildfly 12上的池连接?相关的知识,希望对你有一定的参考价值。

我使用Hibernate在Weblogic 12上运行了一个应用程序。现在我需要使它与Wildfly 12兼容。我们在这里使用Hibernate作为ORM,配置如下。

现在的问题是,Wildfly的连接池在一些请求之后用完了conns,因为很明显Hibernate没有将元素返回给它。

connection.release_mode在hibernate.cfg.xml中正确设置(我认为)

    <property name="transaction.flush_before_completion">true</property>
    <property name="transaction.auto_close_session">true</property>
    <property name="connection.release_mode">after_statement</property>

我们在EJB中使用容器管理事务,DB是Oracle。

数据源配置如下:

           <datasource jta="true" jndi-name="java:/jdbc/psqlWSDS" pool-name="OracleDS" enabled="true" use-ccm="false">
                <connection-url>jdbc:oracle:thin:@//XXX.XXX.XXX.XXX:1521/XXX</connection-url>
                <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
                <driver>oracle</driver>
                <pool>
                    <min-pool-size>1</min-pool-size>
                    <initial-pool-size>1</initial-pool-size>
                    <max-pool-size>30</max-pool-size>
                </pool>
                <security>
                    <user-name>XXX</user-name>
                    <password>XXX</password>
                </security>
                <validation>
                    <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleValidConnectionChecker"/>
                    <background-validation>true</background-validation>
                    <stale-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleStaleConnectionChecker"/>
                    <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleExceptionSorter"/>
                </validation>
            </datasource>

这是堆栈跟踪:

Caused by: javax.ejb.EJBTransactionRolledbackException: org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection  
at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleInCallerTx(CMTTxInterceptor.java:158)  
at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInCallerTx(CMTTxInterceptor.java:255)  
at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:332)  
at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:238)  
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)  
at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:509)  
at org.jboss.weld.module.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:72)  
at org.jboss.as.weld.ejb.EjbRequestScopeActivationInterceptor.processInvocation(EjbRequestScopeActivationInterceptor.java:89)  
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)  
at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41)  
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)  
at org.jboss.as.ejb3.component.invocationmetrics.WaitTimeInterceptor.processInvocation(WaitTimeInterceptor.java:47)  
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)  
at org.jboss.as.ejb3.security.SecurityContextInterceptor.processInvocation(SecurityContextInterceptor.java:100)  
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)  
at org.jboss.as.ejb3.deployment.processors.StartupAwaitInterceptor.processInvocation(StartupAwaitInterceptor.java:22)  
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)  
at org.jboss.as.ejb3.component.interceptors.ShutDownInterceptorFactory$1.processInvocation(ShutDownInterceptorFactory.java:64)  
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)  
at org.jboss.as.ejb3.component.interceptors.LoggingInterceptor.processInvocation(LoggingInterceptor.java:67)  
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)  
at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50)  
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)  
at org.jboss.as.ejb3.component.interceptors.AdditionalSetupInterceptor.processInvocation(AdditionalSetupInterceptor.java:54)  
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)  
at org.jboss.invocation.ContextClassLoaderInterceptor.processInvocation(ContextClassLoaderInterceptor.java:60)  
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)  
at org.jboss.invocation.InterceptorContext.run(InterceptorContext.java:438)  
at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:619)  
at org.jboss.invocation.AccessCheckingInterceptor.processInvocation(AccessCheckingInterceptor.java:57)  
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)  
at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:53)  
at org.jboss.as.ee.component.ViewService$View.invoke(ViewService.java:198)  
at org.jboss.as.ee.component.ViewDescription$1.processInvocation(ViewDescription.java:185)  
at org.jboss.as.ee.component.ProxyInvocationHandler.invoke(ProxyInvocationHandler.java:81)  
at com.jquick.webservices.components.lock.ejb.LockManagerBusiness$$$view33.hasLock(Unknown Source)  
at com.jquick.webservices.session.DefaultService.hasLock(DefaultService.java:630)  
at com.jquick.webservices.components.um.service.UMService.getBenutzer(UMService.java:1532)  
at sun.reflect.GeneratedMethodAccessor174.invoke(Unknown Source)  
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  
at java.lang.reflect.Method.invoke(Method.java:498)  
at com.jquick.webservices.session.invoker.InvokerBean.doInvokeService(InvokerBean.java:245)  
... 248 more  
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection  
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:149)  
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157)  
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1423)  
at com.jquick.webservices.components.lock.dao.LockDAOHibernate.findByApplicationIdAndItemId(LockDAOHibernate.java:91)  
at com.jquick.webservices.components.lock.ejb.LockManagerEJB.hasLockFromMe(LockManagerEJB.java:247)  
at com.jquick.webservices.components.lock.ejb.LockManagerEJB.hasLock(LockManagerEJB.java:230)  
at sun.reflect.GeneratedMethodAccessor175.invoke(Unknown Source)  
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  
at java.lang.reflect.Method.invoke(Method.java:498)  
at org.jboss.as.ee.component.ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptor.java:52)  
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)  
at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:509)  
at org.jboss.as.weld.interceptors.Jsr299BindingsInterceptor.doMethodInterception(Jsr299BindingsInterceptor.java:90)  
at org.jboss.as.weld.interceptors.Jsr299BindingsInterceptor.processInvocation(Jsr299BindingsInterceptor.java:101)  
at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:63)  
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)  
at org.jboss.as.ejb3.component.invocationmetrics.ExecutionTimeInterceptor.processInvocation(ExecutionTimeInterceptor.java:43)  
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)  
at org.jboss.as.jpa.interceptor.SBInvocationInterceptor.processInvocation(SBInvocationInterceptor.java:47)  
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)  
at org.jboss.as.ee.concurrent.ConcurrentContextInterceptor.processInvocation(ConcurrentContextInterceptor.java:45)  
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)  
at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:40)  
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)  
at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:53)  
at org.jboss.as.ee.component.interceptors.ComponentDispatcherInterceptor.processInvocation(ComponentDispatcherInterceptor.java:52)  
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)  
at org.jboss.as.ejb3.component.pool.PooledInstanceInterceptor.processInvocation(PooledInstanceInterceptor.java:51)  
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)  
at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInCallerTx(CMTTxInterceptor.java:253)  
... 288 more  
Caused by: org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection  
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)  
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)  
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)  
at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:118)  
at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getPhysicalConnection(LogicalConnectionManagedImpl.java:145)  
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.connection(StatementPreparerImpl.java:47)  
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:146)  
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:172)  
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:148)  
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1995)  
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1925)  
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1903)  
at org.hibernate.loader.Loader.doQuery(Loader.java:948)  
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:351)  
at org.hibernate.loader.Loader.doList(Loader.java:2702)  
at org.hibernate.loader.Loader.doList(Loader.java:2685)  
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2517)  
at org.hibernate.loader.Loader.list(Loader.java:2512)  
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:502)  
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:384)  
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216)  
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1490)  
at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1445)  
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1414)  
... 315 more  
Caused by: java.sql.SQLException: javax.resource.ResourceException: IJ000453: Unable to get managed connection for java:/jdbc/psqlWSDS  
at org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:146)  
at org.jboss.as.connector.subsystems.datasources.WildFlyDataSource.getConnection(WildFlyDataSource.java:64)  
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122)  
at org.hibernate.internal.NonContextualJdbcConnectionAccess.obtainConnection(NonContextualJdbcConnectionAccess.java:35)  
at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:115)  
... 335 more  
Caused by: javax.resource.ResourceException: IJ000453: Unable to get managed connection for java:/jdbc/psqlWSDS  
at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.getManagedConnection(AbstractConnectionManager.java:690)  
at org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerImpl.getManagedConnection(TxConnectionManagerImpl.java:430)  
at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.allocateConnection(AbstractConnectionManager.java:789)  
at org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:138)  
... 339 more  
Caused by: javax.resource.ResourceException: IJ000655: No managed connections available within configured blocking timeout (30000 [ms])  
at org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreConcurrentLinkedDequeManagedConnectionPool.getConnection(SemaphoreConcurrentLinkedDequeManagedConnectionPool.java:570)  
at org.jboss.jca.core.connectionmanager.pool.AbstractPool.getTransactionNewConnection(AbstractPool.java:714)  
at org.jboss.jca.core.connectionmanager.pool.AbstractPool.getConnection(AbstractPool.java:613)  
at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.getManagedConnection(AbstractConnectionManager.java:624)  
... 342 more  

我尝试将release_mode更改为其他设置而没有任何效果,Hibernate不会释放与池的连接。

从Wildfly的数据源统计页面刷新连接会释放连接。

所以任何提示,如何让连接池按预期运行将非常感激;-)

最好的祝福

标记。

答案

我发现以我在OpenJPA中的方式注入EntityManager会导致Hibernate创建多个SessionFactory对象,从而导致连接泄漏。参见文档(第1.1节)http://docs.jboss.org/hibernate/orm/5.3/userguide/html_single/Hibernate_User_Guide.html

我更改了我的应用程序以使用单例类创建一个SessionFactory对象,然后在每个查询中获取该工厂对象以创建Session对象。

//Session manager singleton
@Singleton
public class DbSessionManager {
    @PersistenceUnit
    private EntityManagerFactory emf;

    public EntityManager createSession() {
        return emf.createEntityManager();
    } }


//Data access class
public class DbWriter{

    @EJB
    private DbSessionManager db;

    @Override
    public EntityDao save(EntityDao newEnt)  {
        EntityManager em = db.createSession();

        try {
            em.merge(newEnt);
        } catch (NoResultException x) {
            em.persist(newEnt);
        } finally {
            em.close();
        }

        return newEnt;
    } }

以上是关于为什么Hibernate不会返回Wildfly 12上的池连接?的主要内容,如果未能解决你的问题,请参考以下文章

wildfly 10上使用最新的 Hibernate ORM OGM

使用 Infinispan 和 Wildfly 配置 Hibernate Search

WildFly8.2.0.Final、Hibernate、Infinispan:org.hibernate.cache.CacheException:不支持的访问类型 [读写]

JBoss as7 到 WildFly 的 Hibernate 迁移

WildFly + 休眠

Infinispan/JDBC 作为 Wildfly/JBoss 上 Hibernate Search 的后端