开发者

How to perform a DB cleanup operation upon shutdown in an EJB container

I have an EJB app. which basically has to execute a SQL query when it is shutdown. A shutdown hook would presumably work, but that way I can't use injected entitymanager/datasource etc.

Is there a way to provide a shutdown h开发者_StackOverflow中文版ook that can invoke methods on EJB bean?

Our EJB container is JBoss5.1.

Thanks!


for ejbs the approach would be same as what zwei has mentioned, but to add an initializaiton or cleanup ejb with a method having annotation PreDestroy


It looks like this works: (EDIT: For some reason the previously posted answer did not work with JBoss5.1. This works.)

    public class SomeServlet extends GenericServlet {
        public void destroy(){
            InitialContext ctx = null;
            try{
                ctx = new InitialContext();
                DataSource ds = (DataSource)ctx.lookup("java:/someDataSource");
                doStuff();
            }catch(Exception e){
                log.error("Bad things happened",e);
            }

            finally{
                try {
                    ctx.close();
                } catch (NamingException e) {
                    e.printStackTrace();
                }
            }
         }//destroy()
    }//class
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜