0

I have an on Object with an annotation

@ApplicationScoped
public class DbGraphConnectionLocator implements ServerStopTask {
...
}

This object create an embedded database.

I have tried to create an hook to shutdown database when i redeploy the application.

So i construct the class

@WebListener
public class UndeployHook implements ServletContextListener{
@Inject
DbGraphConnectionLocator dbGraphConnectionLocator;


@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {

}

@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
    try {
        dbGraphConnectionLocator.executeStopTask();
        } catch (Exception e) {
        e.printStackTrace();
        }
    }
}

Actually the method contextDestoyed is called but the object used in is not the original one DbGraphConnectionLocator created at start up, and this is causing me some trouble.

In my opinion with the annotation

@ApplicationScoped
public class DbGraphConnectionLocator implements ServerStopTask { ....}

The instance of DbGraphConnectionLocator have to be unique at application level so when i use it with a

@Inject
DbGraphConnectionLocator dbGraphConnectionLocator;

I have to found the same instance created on start up by my application, but this is not true another instance was created.

Anyway i resolved adding the

@PreDestroy
public  void shutdownDB(){
    graphDb.shutdown();
}

to DbGraphConnectionLocator. For info i see that this method is called after the contextDestroyed method , and this means that when the contextDestroyed is called the original instance still exist, so i miss something....

any hint?

Krzysztof Wolny
  • 8,626
  • 4
  • 33
  • 44
Antimo
  • 431
  • 7
  • 17
  • The same for me. It seems that web server injects your bean when global bean manager has not started yet so it has to create another instance of the bean – Sergey Jul 25 '18 at 10:51

1 Answers1

1

If the embedded db shall be created at application start and be shut down when the application is stopped, just do the following:

@ApplicationScoped
@Startup
public class AnyNameForYourApplicationClass {

  @PostConstruct
  public void connectDB() {
    // creates the db connection at startup
  }

  @PreDestoy
  public void disconnectDB() {
    // disconnects from the db
  }
}

This should do it. Alternatively you could also use a CDI producer to create an application scoped db connection. Then you can disconnect from the db using a disposer method.

In your case the behavior is correct since when the web listener is called the application must still be alive. So the shutdownDB method with @PreDestroy is called afterwards when the application scoped bean is destroyed. And since you are referencing it in your listener, it must stay alive until the listener is destroyed.

shillner
  • 1,678
  • 11
  • 23
  • Thank for the response, even in my case the PreDestroy resolve the poblem. I didn't understand why with the annotation ApplicationScoped public class DbGraphConnectionLocator implements ServerStopTask { ....} the contextDestoyed is called but the object used in is not the original one DbGraphConnectionLocator created at start up – Antimo Nov 02 '14 at 12:30