简体   繁体   English

在Maven项目中注入有状态EJB Bean

[英]Inject stateful EJB beans in a maven project

I don't understand one thing that I hope someone of you could explain me. 我不明白我希望你们中的某人能向我解释的一件事。 I have a maven enterprise project developed with glassfish. 我有一个用glassfish开发的Maven企业项目。

I use the insert code netbeans function (right click) to call bean in a servlet and in particular the annotation 我使用插入代码netbeans函数(右键单击)来调用servlet中的bean,尤其是注释

@EJB

I don't understand why when I call a stateful session bean through Insert Code function in netbeans the bean is called through JNDI. 我不明白为什么当我通过netbeans中的插入代码功能调用有状态会话bean时,是通过JNDI调用bean的原因。 Here what I mean 这是我的意思

private BookingBeanInterface lookupBookingBeanLocal() {
        try {
            Context c = new InitialContext();
            return (BookingBeanInterface) c.lookup("java:global/it.volaconnoi_volaconnoi-webapp-ear_ear_1.0-SNAPSHOT/it.volaconnoi_volaconnoi-webapp-ejb_ejb_1.0-SNAPSHOT/BookingBean!it.volaconnoi.logic.BookingBeanInterface");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }`

The above function hasn't been wrote by me 上面的功能还不是我写的

I can't inject a stateful session bean through EJB? 我无法通过EJB注入有状态会话bean?

Here is the solution to the problem: 这是解决问题的方法:

As you probably already know a single Servlet instance is used to handle multiple requests from multiple clients so the Stateful EJB should not be injected directly in the Servlet and kept as an instance property, or we will face obvious thread-safety related issues. 您可能已经知道单个Servlet实例用于处理来自多个客户端的多个请求,因此有状态EJB不应直接注入Servlet中并作为实例属性保存,否则我们将面临与线程安全相关的明显问题。 In our case we are fetching it from JNDI inside doGet method and storing it in the HTTP session so each user will have it's own Sateful EJB instance. 在我们的例子中,我们是从doGet方法中的JNDI获取它并将其存储在HTTP会话中的,这样每个用户都将拥有它自己的Sateful EJB实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM