EclipseLink + JPA Guice Persist and Redeployments

跟風遠走 提交于 2020-01-24 01:10:08

问题


I have an infrastructure based on EclipseLink + JPA Guice Persist

When I redeploy the application always I have caching problems with caching Entitys and I have to reboot the server (Oracle Weblogic 11g) .This problem is treated in a this post: https://bugs.eclipse.org/bugs/show_bug.cgi?id=326552 But, maybe is not a bug ¿?¿? ...

I managed to solve the problem as follows :

Originally I have centralized everything in a GuiceModule:

1.Create the module JPAPersist

2.Binding of a Initializer class thas invokes the persistenceService.start()

public class MyGuiceModule implements Module {
    @Override
    public void configure(final Binder binder) {        
        Properties props = _dbConnectionPropertiesForPool();    
        JpaPersistModule jpaModule = new JpaPersistModule(persistenceUnit); 
        jpaModule.properties(props);            
        binder.install(jpaModule);
        binder.bind(JPAInitializer.class).asEagerSingleton();       
    }

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

Everything works fine .... but as I said when redeploy remain cached instances

HOW DO I HAVE SOLVED?

  1. I changed the method JPAInitializer

    public static class JPAInitializer {

        private static PersistService _persistenceService = null;
    
        @Inject
        public JPAInitializer(final PersistService service) {
            _persistenceService = service;          
            _persistenceService.start();            
        }       
        public static void stop() {
            _persistenceService.stop();
        }
    }
    
  2. I created a method stop () that stops the service ..but WTF! I have been forced to save the the injected Persistence service in a static variable :((

  3. From the guice / listener filter that is the entrypoint invoke the stop when the application is undeployed (onContextDestroyed)

    public void contextDestroyed(ServletContextEvent servletContextEvent) { JPAInitializer.stop();
    }

Now, when i redeploy there is no cache issue or problem, and there is no need to restart the server

It works this way, but I do not know if it's all right to create a static instance PesistenceService., so i'm trying to find another way to invoke the stop.....

Any suggestion?


回答1:


Found solution .

  1. Create a inteface to handle Guice Persistence Service :

    interface MyPersistenceServiceHandler {

     public void start();
    
     public void stop(); 
    

    }

    This will be used into the main DB Guice Module :

    binder.bind(MyPersistenceServiceHandler .class)
    .to(JPAPersistenceServiceControl.class) .in(Singleton.class);

    static class JPAPersistenceServiceControl implements MyPersistenceServiceHandler {

         private final PersistService _service;
    
         @Inject
         public JPAPersistenceServiceControl(final PersistService service) {
                _service = service;
         }
         @Override
         public void start() {
                if (_service == null) throw new IllegalStateException("NO persistence service available!");
                _service.start();
         }
         @Override
         public void stop() {
                if (_service == null) throw new IllegalStateException("NO persistence service available!");
                _service.stop();
         }
    

    }

  2. Get the instance in the RESTEndoint/Guice filter through Guice Injector.

    jpaServiceHandler = _myGuiceInjector.getInstance(MyPersistenceServiceHandler .class);

  3. Start the service on contextInitialized : jpaServiceHandler.start();

  4. Stop the service on contextDeproyed : jpaServiceHandler.stop();



来源:https://stackoverflow.com/questions/28604296/eclipselink-jpa-guice-persist-and-redeployments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!