如何在EJB容器中关闭时执行数据库清理操作 [英] How to perform a DB cleanup operation upon shutdown in an EJB container

查看:190
本文介绍了如何在EJB容器中关闭时执行数据库清理操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EJB应用程序它基本上必须在关闭时执行SQL查询。一个关机钩子可能会工作,但是我不能使用注入的entitymanager / datasource等。



有没有办法提供一个关闭钩子,可以调用方法EJB bean?



我们的EJB容器是JBoss5.1。



谢谢!

解决方案

看起来像这样工作:
(编辑:由于某些原因,以前发布的答案不适用于JBoss5.1。 >

  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(坏事情发生,e);
}

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


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 hook that can invoke methods on EJB bean?

Our EJB container is JBoss5.1.

Thanks!

解决方案

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

这篇关于如何在EJB容器中关闭时执行数据库清理操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆