微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法在测试中修复“ EntityManagerFactory已关闭”

如何解决无法在测试中修复“ EntityManagerFactory已关闭”

我正在开发一个用Scala 2.11编写的项目,我们还使用Spring Boot 2.0和maven进行构建(这是一个多模块项目)。这也不是Web应用程序。

为了解决以下问题,我已经坚持了数周,这只有在使用maven对特定模块运行完整的测试集时才会发生:(mvn clean install -pl:workflow)

在运行单个测试甚至在隔离的程序包中进行多个测试时,都不会发生这种情况。

 org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.IllegalStateException: EntityManagerFactory is closed
  at org.springframework.orm.jpa.JpaTransactionManager.dobegin(JpaTransactionManager.java:448)
  at org.springframework.transaction.support.AbstractPlatformTransactionManager.startTransaction(AbstractPlatformTransactionManager.java:400)
  at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)
  at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:574)
  at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:361)
  at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
  at org.springframework.aop.framework.cglibAopProxy$cglibMethodInvocation.proceed(cglibAopProxy.java:749)
  at org.springframework.aop.framework.cglibAopProxy$DynamicAdvisedInterceptor.intercept(cglibAopProxy.java:691)
  at edu.ucdavis.fiehnlab.ms.carrot.core.db.jpa.JPALibraryAccess$$EnhancerBySpringcglib$$773e5887.load(<generated>)
  ...
  Cause: java.lang.IllegalStateException: EntityManagerFactory is closed
  at org.hibernate.internal.SessionFactoryImpl.validateNotClosed(SessionFactoryImpl.java:509)
  at org.hibernate.internal.SessionFactoryImpl.createEntityManager(SessionFactoryImpl.java:588)
  at org.hibernate.internal.SessionFactoryImpl.createEntityManager(SessionFactoryImpl.java:156)
  at org.springframework.orm.jpa.JpaTransactionManager.createEntityManagerForTransaction(JpaTransactionManager.java:466)
  at org.springframework.orm.jpa.JpaTransactionManager.dobegin(JpaTransactionManager.java:391)
  at org.springframework.transaction.support.AbstractPlatformTransactionManager.startTransaction(AbstractPlatformTransactionManager.java:400)
  at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)
  at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:574)
  at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:361)
  at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118)

任何和所有与EntityManager相关的bean都由Spring处理。

我们已经为使用数据库客户端的所有测试添加了@Transactional批注;还添加了@DirtiesContext,没有任何更改。

我将非常感谢您提供有关调试/修复此问题的帮助或指示。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。