Java Spring 3.2+;Hibernate 4.3.4+;jUnit:EntityManagerFactory已关闭

Java Spring 3.2+;Hibernate 4.3.4+;jUnit:EntityManagerFactory已关闭,java,spring,hibernate,jpa,hibernate-entitymanager,Java,Spring,Hibernate,Jpa,Hibernate Entitymanager,我正在为我的Spring3.2.8+Hibernate4.3.4应用程序编写一些junit测试。我的测试类带有以下注释: @DirtiesContext(classMode=ClassMode.AFTER_EACH_TEST_METHOD) @Transactional public class UserServiceImplTest { ... ... } 但是,尝试使用实体管理器的方面引发了以下错误: org.springframework.dao.InvalidDataAcc

我正在为我的Spring3.2.8+Hibernate4.3.4应用程序编写一些junit测试。我的测试类带有以下注释:

@DirtiesContext(classMode=ClassMode.AFTER_EACH_TEST_METHOD)
@Transactional
public class UserServiceImplTest {
   ...
   ...
}
但是,尝试使用实体管理器的方面引发了以下错误:

org.springframework.dao.InvalidDataAccessApiUsageException: EntityManagerFactory is closed; nested exception is java.lang.IllegalStateException: EntityManagerFactory is closed
如果我查看日志,我会在第二次测试开始时看到以下错误消息:

2014-04-02 16:36:48,891 [main] WARN  org.hibernate.jpa.internal.EntityManagerFactoryRegistry - HHH000436: Entity manager factory name (default) is already registered.  If entity manager will be clustered or passivated, specify a unique value for property 'hibernate.ejb.entitymanager_factory_name'
我意识到这只是一个警告,但我想知道这是否是问题的一部分。我不明白为什么它抱怨工厂已经注册,而
@DirtiesContext
应该确保在开始第二次测试之前清除上下文

在我的第一次测试结束时,我确实看到上下文正在被清理:

2014-04-02 16:36:10,733 [main] INFO  springframework.test.context.transaction.TransactionalTestExecutionListener - Rolled back transaction after test execution for test context [TestContext@1ff221ad testClass = UserServiceImplTest, testInstance = com.ia.service.UserServiceImplTest@5180f53d, testMethod = testUpdateRoles@UserServiceImplTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@6964a198 testClass = UserServiceImplTest, locations = '{classpath:META-INF/spring/applicationContext*.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{test}', resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.test.context.web.WebDelegatingSmartContextLoader', parent = [null]]]
2014-04-02 16:36:10,735 [main] INFO  springframework.web.context.support.GenericWebApplicationContext - Closing org.springframework.web.context.support.GenericWebApplicationContext@53c3dcdc: startup date [Wed Apr 02 16:35:00 EDT 2014]; root of context hierarchy
2014-04-02 16:36:10,736 [main] INFO  springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2ff0a910: defining beans [dataSource,entityManagerFactory,org.springframework.transaction.config.internalTransactionAspect,hibernateJpaVendorAdapter,transactionManager,org.springframework.data.repository.core.support.RepositoryInterfaceAwareBeanPostProcessor#0,org.springframework.data.jpa.repository.support.EntityManagerBeanDefinitionRegistrarPostProcessor#0,org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor#0,rateRepository,userRepositoryImpl,userRepository,patchRepository,org.springframework.security.filterChains,org.springframework.security.filterChainProxy,org.springframework.security.web.DefaultSecurityFilterChain#0,org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler#0,org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource#0,org.springframework.security.access.vote.AffirmativeBased#0,org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor#0,org.springframework.security.methodSecurityMetadataSourceAdvisor,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.security.web.PortMapperImpl#0,org.springframework.security.web.PortResolverImpl#0,org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0,org.springframework.security.authentication.ProviderManager#0,org.springframework.security.web.context.NullSecurityContextRepository#0,org.springframework.security.web.savedrequest.NullRequestCache#0,org.springframework.security.access.vote.AffirmativeBased#1,org.springframework.security.web.access.intercept.FilterSecurityInterceptor#0,org.springframework.security.web.access.DefaultWebInvocationPrivilegeEvaluator#0,org.springframework.security.authentication.AnonymousAuthenticationProvider#0,org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint#0,org.springframework.security.userDetailsServiceFactory,org.springframework.security.web.DefaultSecurityFilterChain#1,org.springframework.security.authentication.dao.DaoAuthenticationProvider#0,org.springframework.security.authentication.DefaultAuthenticationEventPublisher#0,org.springframework.security.authenticationManager,basicAuthenticationFilter,authenticationEntryPoint,defaultWebSecurityExpressionHandler,org.springframework.context.support.PropertySourcesPlaceholderConfigurer#0,org.springframework.context.config.internalBeanConfigurerAspect,contactDataOnDemand,userDataOnDemand,testBase.BeanRegistry,securityService,patchServiceImpl,rateServiceImpl,userServiceImpl,appConfig,viewConfig,webContextFilter,patchEngineContextListener,patchEngineImpl.PatchEngineInitializer,patchEngineImpl,initializeUsersRolesAndPermissionsPatch,tilesContainerFactory,viewMetaPreparer,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,applicationProperties,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,org.springframework.data.auditing.AuditingHandler#0,org.springframework.data.jpa.domain.support.AuditingEntityListener,org.springframework.data.jpa.domain.support.AuditingBeanFactoryPostProcessor,org.springframework.data.auditing.AuditingHandler#1,auditorProvider,objectMapper,org.springframework.orm.jpa.SharedEntityManagerCreator#0]; root of factory hierarchy
2014-04-02 16:36:10,754 [main] INFO  org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean - Closing JPA EntityManagerFactory for persistence unit 'default'
所以我不知道为什么它会抱怨EntityManagerFactory已经注册了。此外,我不确定当我试图访问EMF时为什么它是关闭的


我是否需要做一些特殊的事情来确保在每次测试中仍然可以访问它?我确实发现这与问题有关,但这并不能帮助我确定发生这种情况的原因。

似乎有人试图在设置spring上下文之前或在处理它之后获取持久性上下文


您是否有一些junit@before或@after注释的方法来访问持久性?

经过大量调试后,发现问题出在使用EntityManagerFactory的方面。假设方面是在Spring上下文之外实例化的,当上下文刷新时,方面的引用不会更新。因此,方面保存了一个对一个确实已关闭的EMF的引用——第一个单元测试中的原始EMF。现在我只需要弄清楚如何让Spring在更新上下文时更新/刷新引用。

否-在
@before
@after
方法中没有任何内容。但是经过一系列的调试,我终于意识到这是我的方面的问题;它从第一个上下文中维护EntityManagerFactory bean引用,在上下文刷新后不更新引用。@Gab-我会的,但我需要2天才能接受自己的答案。:)你能想出一种刷新方面引用的方法吗?@AnkitBatra这是几年前的事了,所以我记不清了,但我确实记得,当我不想让方面保留上下文信息时,我很难让它们保留上下文信息。我努力解决这个问题,但是有很多静态数据是在方面初始化的(由于AJ的工作方式),我无法“撤销”。我相信我唯一能绕过这个问题的方法就是启动多个fork,每个测试都在一个独立的fork中运行。@Eric感谢您回来。