EclipseLink + JPA Guice Persist and Redeployments

2019-06-13 23:53发布

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条回答
闹够了就滚
2楼-- · 2019-06-14 00:45

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();

查看更多
登录 后发表回答