2016-03-03 80 views
0

我正在为我的ejb和hibernate.search编写测试,并且需要一些帮助。Arquilian测试方法失败,使用org.hibernate.SessionException

这里是我的EJB:

@Local 
@Path("/info") 
@Produces(HttpConstants.JSON_UTF8) 
@Consumes(MediaType.APPLICATION_JSON) 
public interface InfoService { 
    /** 
    * full text search 
    */ 
    @GET 
    @Path("/search") 
    Response search(@QueryParam("pattern") String pattern); 
} 

@Stateless 
public class InfoServiceImpl implements InfoService, Serializable { 

    private static final long serialVersionUID = -1_000_000L; 

    @PersistenceContext(unitName = "spo") 
    private EntityManager em; 

    private FullTextEntityManager ftem; 

    @Inject 
    private MyService svc; 

    @PostConstruct 
    public void init() { 
     ftem = Search.getFullTextEntityManager(em); 
    } 

    @Override 
    public Response search(String pattern) { 
     QueryBuilder qb = ftem.getSearchFactory().buildQueryBuilder().forEntity(MyEntity.class).get(); 
     Query luceneQuery = qb.keyword().onFields(
       MyEntity_.shortName.getName(), 
       MyEntity_.fullName.getName(), 
       MyEntity_.label.getName() 
     ).matching(pattern).createQuery(); 

     javax.persistence.Query query = ftem.createFullTextQuery(luceneQuery, MyEntity.class); 
     @SuppressWarnings("unchecked") 
     List<MyEntity> found = (List<MyEntity>)query.getResultList(); 

     return svc.createResponse(found); 
    } 

测试类,试图找到一些。首先搜索 - 好吧,第二 - 失败:

public class InfoServiceTest extends Arquillian { 
    @PersistenceContext(unitName = "spo") 
    private EntityManager em; 
    @EJB 
    private InfoService infSrv; 

    @Test 
    //@Transactional //doesn`t work without this annotation! Why ? 
    public void searchTest() { 
     // First call - ok, one item found 
     Response r1 = infSrv.search("car"); 
     // This call fails with 
     // org.hibernate.SessionException: Session is closed! 
     // If test method marked @Transactional - ok, one item found 
     Response r2 = infSrv.search("plane"); 
} 

t understand Why exception org.hibernate.SessionException appears. My class InfoServiceImpl is @Stateless, it小号的方法可以是事务性的,它是EJB容器的责任来处理事务。

这里是堆栈跟踪:

ERROR [org.jboss.as.ejb3.invocation] (default task-18) WFLYEJB0034: EJB Invocation failed on component InfoResourceServiceImpl for method public abstract javax.ws.rs.core.Response ru.pps.mw.graph.http.InfoResourceService.search(java.lang.Object,java.util.List): javax.ejb.EJBException: org.hibernate.SessionException: Session is closed! 
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:187) 
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:277) 
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:327) 
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:239) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340) 
    at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340) 
    at org.jboss.as.ejb3.component.invocationmetrics.WaitTimeInterceptor.processInvocation(WaitTimeInterceptor.java:43) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340) 
    at org.jboss.as.ejb3.security.SecurityContextInterceptor.processInvocation(SecurityContextInterceptor.java:100) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340) 
    at org.jboss.as.ejb3.component.interceptors.ShutDownInterceptorFactory$1.processInvocation(ShutDownInterceptorFactory.java:64) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340) 
    at org.jboss.as.ejb3.component.interceptors.LoggingInterceptor.processInvocation(LoggingInterceptor.java:66) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340) 
    at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340) 
    at org.jboss.as.ejb3.component.interceptors.AdditionalSetupInterceptor.processInvocation(AdditionalSetupInterceptor.java:54) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340) 
    at org.jboss.invocation.ContextClassLoaderInterceptor.processInvocation(ContextClassLoaderInterceptor.java:64) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340) 
    at org.jboss.invocation.InterceptorContext.run(InterceptorContext.java:356) 
    at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:636) 
    at org.jboss.invocation.AccessCheckingInterceptor.processInvocation(AccessCheckingInterceptor.java:61) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340) 
    at org.jboss.invocation.InterceptorContext.run(InterceptorContext.java:356) 
    at org.jboss.invocation.PrivilegedWithCombinerInterceptor.processInvocation(PrivilegedWithCombinerInterceptor.java:80) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340) 
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61) 
    at org.jboss.as.ee.component.ViewService$View.invoke(ViewService.java:195) 
    at org.jboss.as.ee.component.ViewDescription$1.processInvocation(ViewDescription.java:185) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340) 
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61) 
    at org.jboss.as.ee.component.ProxyInvocationHandler.invoke(ProxyInvocationHandler.java:73) 
    at ru.pps.mw.graph.http.InfoResourceService$$$view1.search(Unknown Source) 
    at ru.pps.mw.graph.http.test.InfoResourceServiceTest.searchTest(InfoResourceServiceTest.java:278) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86) 
    at org.testng.internal.MethodInvocationHelper$1.runTestMethod(MethodInvocationHelper.java:197) 
    at org.jboss.arquillian.testng.Arquillian$2.invoke(Arquillian.java:173) 
    at org.jboss.arquillian.container.test.impl.execution.LocalTestExecuter.execute(LocalTestExecuter.java:60) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94) 
    at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99) 
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81) 
    at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:145) 
    at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:116) 
    at org.jboss.arquillian.core.impl.EventImpl.fire(EventImpl.java:67) 
    at org.jboss.arquillian.container.test.impl.execution.ContainerTestExecuter.execute(ContainerTestExecuter.java:38) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94) 
    at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99) 
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81) 
    at org.jboss.arquillian.test.impl.TestContextHandler.createClassContext(TestContextHandler.java:92) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94) 
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88) 
    at org.jboss.arquillian.test.impl.TestContextHandler.createSuiteContext(TestContextHandler.java:73) 
    at sun.reflect.GeneratedMethodAccessor37.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94) 
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88) 
    at org.jboss.arquillian.test.impl.TestContextHandler.createTestContext(TestContextHandler.java:130) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94) 
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88) 
    at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:145) 
    at org.jboss.arquillian.test.impl.EventTestRunnerAdaptor.test(EventTestRunnerAdaptor.java:136) 
    at org.jboss.arquillian.testng.Arquillian.run(Arquillian.java:164) 
    at org.testng.internal.MethodInvocationHelper.invokeHookable(MethodInvocationHelper.java:209) 
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:639) 
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820) 
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128) 
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129) 
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112) 
    at org.testng.TestRunner.privateRun(TestRunner.java:782) 
    at org.testng.TestRunner.run(TestRunner.java:632) 
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:366) 
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361) 
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319) 
    at org.testng.SuiteRunner.run(SuiteRunner.java:268) 
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244) 
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1169) 
    at org.testng.TestNG.run(TestNG.java:1064) 
    at org.jboss.arquillian.testng.container.TestNGTestRunner.execute(TestNGTestRunner.java:53) 
    at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.executeTest(ServletTestRunner.java:170) 
    at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.execute(ServletTestRunner.java:135) 
    at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.doGet(ServletTestRunner.java:98) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) 
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85) 
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62) 
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) 
    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78) 
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) 
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131) 
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57) 
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) 
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46) 
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64) 
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60) 
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77) 
    at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50) 
    at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43) 
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) 
    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61) 
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) 
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) 
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:284) 
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:263) 
    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81) 
    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:174) 
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202) 
    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:793) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: org.hibernate.SessionException: Session is closed! 
    at org.hibernate.internal.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:132) 
    at org.hibernate.internal.SessionImpl.createCriteria(SessionImpl.java:1697) 
    at org.hibernate.search.query.hibernate.impl.CriteriaObjectInitializer.buildUpCriteria(CriteriaObjectInitializer.java:128) 
    at org.hibernate.search.query.hibernate.impl.CriteriaObjectInitializer.initializeObjects(CriteriaObjectInitializer.java:68) 
    at org.hibernate.search.query.hibernate.impl.QueryLoader.executeLoad(QueryLoader.java:88) 
    at org.hibernate.search.query.hibernate.impl.AbstractLoader.load(AbstractLoader.java:58) 
    at org.hibernate.search.query.hibernate.impl.FullTextQueryImpl.list(FullTextQueryImpl.java:201) 
    at org.hibernate.search.jpa.impl.FullTextQueryImpl.getResultList(FullTextQueryImpl.java:153) 
    at ru.pps.mw.graph.http.InfoResourceServiceImpl.search(InfoResourceServiceImpl.java:61) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at org.jboss.as.ee.component.ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptor.java:52) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340) 
    at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:437) 
    at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.delegateInterception(Jsr299BindingsInterceptor.java:70) 
    at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.doMethodInterception(Jsr299BindingsInterceptor.java:80) 
    at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.processInvocation(Jsr299BindingsInterceptor.java:93) 
    at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:63) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340) 
    at org.jboss.as.ejb3.component.invocationmetrics.ExecutionTimeInterceptor.processInvocation(ExecutionTimeInterceptor.java:43) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340) 
    at org.jboss.as.jpa.interceptor.SBInvocationInterceptor.processInvocation(SBInvocationInterceptor.java:47) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340) 
    at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:437) 
    at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:64) 
    at org.jboss.as.weld.ejb.EjbRequestScopeActivationInterceptor.processInvocation(EjbRequestScopeActivationInterceptor.java:83) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340) 
    at org.jboss.as.ee.concurrent.ConcurrentContextInterceptor.processInvocation(ConcurrentContextInterceptor.java:45) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340) 
    at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:21) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340) 
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61) 
    at org.jboss.as.ee.component.interceptors.ComponentDispatcherInterceptor.processInvocation(ComponentDispatcherInterceptor.java:52) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340) 
    at org.jboss.as.ejb3.component.pool.PooledInstanceInterceptor.processInvocation(PooledInstanceInterceptor.java:51) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340) 
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:275) 
    ... 133 more 
+0

可以在这里粘贴完整的异常堆栈跟踪? – Atul

+0

完成。在我的例子中,InfoResourceServiceImpl.java是InfoServiceImpl.java – abkvandrd

回答

0

的原因是

@PostConstruct 
    public void init() { 
     ftem = Search.getFullTextEntityManager(em); 
    } 

你不能老是使用FullTextEntityManager作为类成员一样的EntityManager。 每次它应该是另一个FTEM。你应该这样做:

@Override 
public Response search(String pattern) { 
    FullTextEntityManager ftem = Search.getFullTextEntityManager(em); 
    QueryBuilder qb = ftem.getSearchFactory().buildQueryBuilder().forEntity(MyEntity.class).get(); 
    Query luceneQuery = qb.keyword().onFields(
      MyEntity_.shortName.getName(), 
      MyEntity_.fullName.getName(), 
      MyEntity_.label.getName() 
    ).matching(pattern).createQuery(); 

    javax.persistence.Query query = ftem.createFullTextQuery(luceneQuery, MyEntity.class); 
    @SuppressWarnings("unchecked") 
    List<MyEntity> found = (List<MyEntity>)query.getResultList(); 

    return svc.createResponse(found); 
}