0

私はGuiceを使用してEntityManagerを注入しています。 注入されたentityManagerのtrasactionをコミットすると、BD側に何も起こりません。あなたは何が起こっているのか理解するのを助けてくれますか?ここで@Transactionalを使用してEntityManagerトランザクションをコミットする - Guice

は私のコードです:

のWeb.xml

<filter> 
    <filter-name>guiceFilter</filter-name> 
    <filter-class>com.google.inject.servlet.GuiceFilter</filter-class> 
    <async-supported>true</async-supported> 
    </filter> 

    <filter-mapping> 
    <filter-name>guiceFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
    </filter-mapping> 

    <listener> 
    <listener-class>ca.products.services.InjectorListener</listener-class> 
    </listener> 

InjectorListenerクラス:

public class InjectorListener extends GuiceServletContextListener { 
    @Override 
    protected Injector getInjector() { 

     return Guice.createInjector(
       new PersistenceModule(), 
       new GuiceModule(), 
       new RestModule()); 
    } 
} 

persistenceModuleクラス:

public class PersistenceModule implements Module { 
    @Override 
    public void configure(Binder binder) { 
     binder 
       .install(new JpaPersistModule("manager1") 
         .properties(getPersistenceProperties())); 

     binder.bind(PersistenceInitializer.class).asEagerSingleton(); 
    } 

    private static Properties getPersistenceProperties() { 
     Properties properties = new Properties(); 
     properties.put("hibernate.connection.driver_class", "org.postgresql.Driver"); 
     properties.put("hibernate.connection.url", "jdbc:postgresql://localhost:5432/postgres"); 
     properties.put("hibernate.connection.username", "postgres"); 
     properties.put("hibernate.connection.password", "postgres"); 
     properties.put("hibernate.connection.pool_size", "1"); 
     properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect"); 
     properties.put("hibernate.hbm2ddl.auto", "create"); 

     return properties; 
    } 
} 

GuiceModule C小娘:

public class GuiceModule extends AbstractModule { 

    @Override 
    protected void configure() { 

     bind(MemberRepository.class).to(MemberRepositoryImp.class); 
     bind(ProductRepository.class).to(ProductRepositoryImpl.class); 
     bind(ShoppingBagRepository.class).to(ShoppingBagRepositoryImpl.class); 
    } 
} 

RestModuleクラス:あなたはおそらくする必要が

@Inject 
    private Provider<EntityManager> em; 

    @GET 
    @Transactional 
    @Path("/reset") 
    public void resetData() { 

     logger.info("Processing reset"); 
     try { 

      em.get().getTransaction().begin(); 

      for (int i = 0; i < 10; i++) { 
       em.get().persist(new Product("Product_" + i, "Desc_" + i)); 
      } 
      em.get().flush(); 
      em.get().getTransaction().commit(); 

     } catch (Exception e) { 
      throw new WebApplicationException(Response.Status.FORBIDDEN); 
     } 
    } 

答えて

1

パブリッククラスRestModuleがJerseyServletModule {

@Override 
    protected void configureServlets() { 

     HashMap<String, String> params = new HashMap<>(); 
     params.put(PackagesResourceConfig.PROPERTY_PACKAGES, "ca.products.services"); 
     params.put(JSONConfiguration.FEATURE_POJO_MAPPING, "true"); 
     params.put(ResourceConfig.FEATURE_DISABLE_WADL, "true"); 

     serve("/*").with(GuiceContainer.class, params); 
    } 
} 

と最後にWebサービス(jeresy)コールを拡張永続フィルタを追加します。これにより、トランザクションを手動で管理する必要がなくなります。フィルタを使用しない場合でも、UnitOfWorkを注入してトランザクションを作成することができます。 jpa persistを使用している場合は、userTransactionsを管理してはいけません。

これは、スタートアップ時にカスタムコードとマップバインダービルダーで自動的に起動するライフサイクルを追加するカスタムフィルターです。それは徹底的にしかありません。これはGuice APIの一部ではなく、Springのライフサイクルリスナーに似ています。私は春の依存関係はまったくありません。

@Singleton 
public final class JpaPersistFilter implements Filter { 

    private final UnitOfWork unitOfWork; 
    private final PersistServiceLifecycle persistService; 

    @Inject 
    public JpaPersistFilter(UnitOfWork unitOfWork, PersistServiceLifecycle persistService) { 
     this.unitOfWork = unitOfWork; 
     this.persistService = persistService; 
    } 

    public void init(FilterConfig filterConfig) throws ServletException { 
     // persistService.start(); 
    } 

    public void destroy() { 
     persistService.stop(); 
    } 

    public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, 
      final FilterChain filterChain) throws IOException, ServletException { 

     unitOfWork.begin(); 
     try { 
      filterChain.doFilter(servletRequest, servletResponse); 
     } finally { 
      unitOfWork.end(); 
     } 
    } 

    /** 
    * Extra lifecycle handler for starting and stopping the service. This 
    * allows us to register a {@link Lifecycle} with the 
    * {@link LifecycleListener} and not have to worry about the service being 
    * started twice. 
    * 
    * @author chinshaw 
    * 
    */ 
    @Singleton 
    public static class PersistServiceLifecycle implements Lifecycle { 

     private final PersistService persistService; 

     private volatile boolean isStarted = false; 

     @Inject 
     public PersistServiceLifecycle(PersistService persistSerivce) { 

      this.persistService = persistSerivce; 
     } 

     @Override 
     public boolean isRunning() { 
      return isStarted; 
     } 

     @Override 
     public void start() { 
      if (!isStarted) { 
       persistService.start(); 
       isStarted = true; 
      } 
     } 

     @Override 
     public void stop() { 
      persistService.stop(); 
      isStarted = false; 
     } 
    } 
} 

モジュールにフィルタを追加する例。

@Override 
protected void configureServlets() { 
    filter("/api/*").through(JpaPersistFilter.class); 
} 

トランザクションを管理するための作業単位の使用例。

@Inject 
UnitOfWork unitOfWork; 
public void doSomething() { 
    unitOfWork.begin(); 
    try { 
     dao.saveState(someobject); 
    } finally { 
     unitOfWork.end(); 
    } 
} 
+0

こんにちはChris、私は単純なGuiceFilterを追加し、それは動作します;):filter( "/ *")。through(PersistFilter.class); ありがとう – taboubim

関連する問題