TransactionRequiredException:Jersey + Guice + JPA没有进行任何事务

时间:2014-04-16 21:27:19

标签: jpa jersey guice guice-3 guice-persist

使用Guice 3.0,遵循本教程:https://code.google.com/p/google-guice/wiki/JPA

GuiceModule类:

public class GuiceModule extends ServletModule {

@Override
protected void configureServlets() {
  // start the service
  bind(JPAInitializer.class).asEagerSingleton();

  // PersistFilter
  filter("/*").through(PersistFilter.class);

  // jpa + guice
  install(new JpaPersistModule("drone-cloud"));

  // bindings
  bind(UserService.class).to(UserServiceImpl.class);
  bind(TwoFactorAuthenticator.class).to(GoogleAuthenticator.class);
  bind(ImageQueue.class).asEagerSingleton();
  bind(DroneCloudServerHandler.class).asEagerSingleton();
 }
}

JPAInitializer.class

import javax.inject.Inject;
import com.google.inject.persist.PersistService;

public class JPAInitializer {
   @Inject
   JPAInitializer(PersistService service) {
      service.start();
   }
}

控制器类:

@Consumes(MediaType.APPLICATION_JSON)
@Path("/mission")
public class MissionController {

   @Inject
   Provider<EntityManager> _em;

   @Path("/new")
   @POST
   @Transactional
   public String newMission(SimpleMission mission) {

      _em.get().persist(mission);
      _em.get().flush();
      _em.get().close();

      return "{ \"mission_id\": \"" + mission.getId() + "\" }";
   }
}

堆栈跟踪:

javax.persistence.TransactionRequiredException:没有正在进行的事务     在org.hibernate.jpa.spi.AbstractEntityManagerImpl.checkTransactionNeeded(AbstractEntityManagerImpl.java:1171)     在org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1332)     在com.drone.mission.controller.MissionController.newMission(MissionController.java:37)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     at java.lang.reflect.Method.invoke(Method.java:606)     at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory $ 1.invoke(ResourceMethodInvocationHandlerFactory.java:81)     at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher $ 1.run(AbstractJavaResourceMethodDispatcher.java:151)     在org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:171)     at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider $ TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:195)     at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:104)     在org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:406)     在org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:350)     在org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:106)     在org.glassfish.jersey.server.ServerRuntime $ 1.run(ServerRuntime.java:259)     在org.glassfish.jersey.internal.Errors $ 1.call(Errors.java:271)     在org.glassfish.jersey.internal.Errors $ 1.call(Errors.java:267)     在org.glassfish.jersey.internal.Errors.process(Errors.java:315)     在org.glassfish.jersey.internal.Errors.process(Errors.java:297)     在org.glassfish.jersey.internal.Errors.process(Errors.java:267)     在org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:320)     在org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236)     在org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1028)     在org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373)     在org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)     在org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)     在org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:219)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)     在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)     在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)     at org.apache.catalina.core.StandardContextValve .__ invoke(StandardContextValve.java:122)     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java)     在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)     在org.apache.catalina.core.StandardHostValve .__ invoke(StandardHostValve.java:170)     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java)     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)     在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)     在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)     在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)     在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)     at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:607)     在org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:313)     在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)     at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)     在java.lang.Thread.run(Thread.java:744)

我做错了什么?

由于

1 个答案:

答案 0 :(得分:1)

问题是,MissionController类是由泽西管理的,不是由Guice管理,所以它没有被JpaLocalTxnInterceptor

拦截

我通过将与DB相关的代码移动到另一个类来解决它,如下所示:

class MissionController {
    @Inject MissionDao dao;
    ....
}


public class MissionDao {
   @Inject Provider<EntityManager> em;

   @Transactional
   public void save(SimpleMission mission) {
      em.get().persist(mission);
   }
   ...
}