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?