我的代码不断抛出“ EntityManagerFactory已关闭”异常。
奇怪的是-它仅在我们的生产机器上才这样做。当我在本地计算机上以完全相同的配置运行代码时,同时连接到完全相同的数据库-错误不会出现。另一个奇怪的事情是-即使不断抛出异常,Hibernate看起来还是可以忽略它们并成功完成事务。过了一会儿,它无法完成任何事务,我被迫重新启动应用程序以使其再次运行(重新启动后例外仍然存在)。
我将EntityManager注入到我的DAO类中,如下所示:
@PersistenceContext
private EntityManager em;
我使用这些DAO的方式大多来自@Transactional注释的服务方法。
堆栈跟踪:
2018-08-02 14:27:29,695 ERROR [stderr] (PG-JDBC EventLoop (6)) org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.IllegalStateException: EntityManagerFactory is closed
2018-08-02 14:27:29,695 ERROR [stderr] (PG-JDBC EventLoop (6)) at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:450)
2018-08-02 14:27:29,695 ERROR [stderr] (PG-JDBC EventLoop (6)) at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:378)
2018-08-02 14:27:29,695 ERROR [stderr] (PG-JDBC EventLoop (6)) at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:474)
2018-08-02 14:27:29,695 ERROR [stderr] (PG-JDBC EventLoop (6)) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:289)
2018-08-02 14:27:29,695 ERROR [stderr] (PG-JDBC EventLoop (6)) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
2018-08-02 14:27:29,695 ERROR [stderr] (PG-JDBC EventLoop (6)) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
2018-08-02 14:27:29,695 ERROR [stderr] (PG-JDBC EventLoop (6)) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
2018-08-02 14:27:29,695 ERROR [stderr] (PG-JDBC EventLoop (6)) at xxx.services.examination.service.ExaminationDocumentService$$EnhancerBySpringCGLIB$$ba40e12e.markDocumentError(<generated>)
2018-08-02 14:27:29,695 ERROR [stderr] (PG-JDBC EventLoop (6)) at xxx.services.examination.listener.ExaminationListener.notification(ExaminationListener.java:68)
2018-08-02 14:27:29,696 ERROR [stderr] (PG-JDBC EventLoop (6)) at com.impossibl.postgres.system.BasicContext.reportNotification(BasicContext.java:745)
2018-08-02 14:27:29,696 ERROR [stderr] (PG-JDBC EventLoop (6)) at com.impossibl.postgres.protocol.v30.ProtocolImpl.receiveNotification(ProtocolImpl.java:1143)
2018-08-02 14:27:29,696 ERROR [stderr] (PG-JDBC EventLoop (6)) at com.impossibl.postgres.protocol.v30.ProtocolImpl.dispatch(ProtocolImpl.java:781)
2018-08-02 14:27:29,696 ERROR [stderr] (PG-JDBC EventLoop (6)) at com.impossibl.postgres.protocol.v30.MessageHandler.channelRead(MessageHandler.java:45)
2018-08-02 14:27:29,696 ERROR [stderr] (PG-JDBC EventLoop (6)) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363)
2018-08-02 14:27:29,696 ERROR [stderr] (PG-JDBC EventLoop (6)) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:349)
2018-08-02 14:27:29,696 ERROR [stderr] (PG-JDBC EventLoop (6)) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:341)
2018-08-02 14:27:29,696 ERROR [stderr] (PG-JDBC EventLoop (6)) at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:293)
2018-08-02 14:27:29,696 ERROR [stderr] (PG-JDBC EventLoop (6)) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:267)
2018-08-02 14:27:29,696 ERROR [stderr] (PG-JDBC EventLoop (6)) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363)
2018-08-02 14:27:29,696 ERROR [stderr] (PG-JDBC EventLoop (6)) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:349)
2018-08-02 14:27:29,696 ERROR [stderr] (PG-JDBC EventLoop (6)) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:341)
2018-08-02 14:27:29,696 ERROR [stderr] (PG-JDBC EventLoop (6)) at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1334)
2018-08-02 14:27:29,696 ERROR [stderr] (PG-JDBC EventLoop (6)) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363)
2018-08-02 14:27:29,696 ERROR [stderr] (PG-JDBC EventLoop (6)) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:349)
2018-08-02 14:27:29,696 ERROR [stderr] (PG-JDBC EventLoop (6)) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:926)
2018-08-02 14:27:29,696 ERROR [stderr] (PG-JDBC EventLoop (6)) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:129)
2018-08-02 14:27:29,697 ERROR [stderr] (PG-JDBC EventLoop (6)) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:642)
2018-08-02 14:27:29,697 ERROR [stderr] (PG-JDBC EventLoop (6)) at io.netty.channel.nio.NioEventLoop.processSelectedKeysPlain(NioEventLoop.java:527)
2018-08-02 14:27:29,697 ERROR [stderr] (PG-JDBC EventLoop (6)) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:481)
2018-08-02 14:27:29,697 ERROR [stderr] (PG-JDBC EventLoop (6)) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:441)
2018-08-02 14:27:29,697 ERROR [stderr] (PG-JDBC EventLoop (6)) at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858)
2018-08-02 14:27:29,697 ERROR [stderr] (PG-JDBC EventLoop (6)) at java.lang.Thread.run(Thread.java:748)
2018-08-02 14:27:29,697 ERROR [stderr] (PG-JDBC EventLoop (6)) Caused by: java.lang.IllegalStateException: EntityManagerFactory is closed
2018-08-02 14:27:29,697 ERROR [stderr] (PG-JDBC EventLoop (6)) at org.hibernate.internal.SessionFactoryImpl.validateNotClosed(SessionFactoryImpl.java:531)
2018-08-02 14:27:29,697 ERROR [stderr] (PG-JDBC EventLoop (6)) at org.hibernate.internal.SessionFactoryImpl.createEntityManager(SessionFactoryImpl.java:613)
2018-08-02 14:27:29,697 ERROR [stderr] (PG-JDBC EventLoop (6)) at org.hibernate.internal.SessionFactoryImpl.createEntityManager(SessionFactoryImpl.java:154)
2018-08-02 14:27:29,697 ERROR [stderr] (PG-JDBC EventLoop (6)) at org.springframework.orm.jpa.JpaTransactionManager.createEntityManagerForTransaction(JpaTransactionManager.java:468)
2018-08-02 14:27:29,697 ERROR [stderr] (PG-JDBC EventLoop (6)) at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:389)
2018-08-02 14:27:29,698 ERROR [stderr] (PG-JDBC EventLoop (6)) ... 31 more
(END)
与休眠相关的bean的我的Spring配置(config.xml)如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<jpa:repositories base-package="xxx.services.examination.dao"/>
<context:annotation-config/>
<tx:annotation-driven/>
<!-- POSTGRES -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="listenerDataSource"/>
<property name="packagesToScan">
<array>
<value>xxx.services.examination</value>
</array>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<!--<property name="showSql" value="true" />-->
<property name="databasePlatform" value="org.hibernate.dialect.PostgreSQL95Dialect"/>
</bean>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
</bean>
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="poolName" value="springHikariCP" />
<property name="dataSource" ref="innerDataSource"/>
<property name="connectionTimeout" value="60000"/>
<property name="maximumPoolSize" value="${postgres.max.connection.pool}"/>
</bean>
<bean name="innerDataSource" class="com.impossibl.postgres.jdbc.PGDataSource">
<property name="host" value="${postgres.host}"/>
<property name="port" value="${postgres.port}"/>
<property name="database" value="${postgres.database}"/>
<property name="user" value="${postgres.user}"/>
<property name="password" value="${postgres.password}"/>
</bean>
<bean id="listenerDataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg ref="hikariConfig" />
</bean>
<!-- END POSTGRES -->
</beans>
我的web.xml看起来像这样:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>xxx</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
休眠版本:5.3.1.Final 春季版本:5.0.6.RELEASE HikariCP版本:3.1.0
答案 0 :(得分:0)
最后,事实证明这是由机器的配置引起的。 ulimits太低,单个用户运行了太多“繁重”的过程,这导致了极限的快速饱和。向上移动极限可以解决问题。