In my JavaEE application, I have a @Singleton
class containing some @Scheduled
methods. Furthermore there are methods with @PostConstruct
and @PreDestroy
to set up and clean up the database. (For the sake of simplicity, I just have logging in the example, since that reproduces the problem.) The application has to run on a JBoss EAP 6.3.0.GA server.
While the @PostConstruct
method works fine, @PreDestroy
is not called when I shutdown the server (neither when pressing the stop the server button in eclipse nor when using a shutdown
command from jboss-cli). Here is some code, which reproduces the problem:
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Singleton
@Startup
public class TimerBean {
private static final Logger log = LoggerFactory.getLogger(TimerBean.class);
@PostConstruct
private void postConstruct() {
log.info("PostConstruct called");
}
@PreDestroy
private void preDestroy() {
log.info("PreDestroy called");
}
}
During startup of the server, the @PostConstruct
message appears in the log. But when shutting down the server, no log message appears.
How can I make the server call the @PreDestroy
method?
EDIT: Since the @PreDestroy
method is not the appropriate location to clean up the database, this question is obsolete.
This is not the answer for your question, this is the answer for the question you raised in the last comment. actually i can not think about a right place to do it.someone else might help you to figure it out. but anyway @PostContruct and @PreDestory might not be a part of transaction, that's why it is not good to do DB operations in those methods,
But for your help i attach this which i took from a book (Mastering EJB 3.0),