且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

访问无状态内的现有实例有状态,java ee 6

更新时间:2023-12-03 15:56:10

你应该从不 @Stateless @Stateful bean(SFSB) / code> bean(SLSB)。只要客户端存在,SFSB就会存在(客户端是注入SFSB的实例,在本例中是SLSB本身)。然而,SLSB的目的是无状态,并且大多数容器都将它们放在池中。因此,每当SLSB在使用后返回池中时,它将在其他地方重新使用,但它拥有与第一次创建SLSB时相同的SFSB实例!这可能会导致不希望的结果。



此外,每当您从JNDI获得SFSB时,您将获得一个全新的实例,这与SLSB 在别处共享。然后,SFSB的客户端是您从JNDI获得SFSB的当前客户端类实例。您应该自己保留此实例,并重复使用相同的实例,直到您完成对其执行事务为止。其中一种方法是将它自己存储在HTTP会话中,或者存储在您正在使用的MVC框架的会话作用域托管bean中。



功能要求并不完全清楚对我而言,很难给出一个合适的答案如何解决您的特定问题,但我认为您实际上需要将用户存储在HTTP会话中,而不是存储在SFSB中。最常见的初学者对会话bean的错误就是他们错误地将EJB上下文中的会话解释为HTTP会话。



另请参阅此相关答案同样的问题需要更深入的解释: JSF请求作用域bean继续在每个请求上重新创建新的有状态会话bean吗?根据你的问题历史,你对JSF很熟悉,所以这个答案应该很容易理解。 / p>

Is it possible to access a stateful session bean inside a stateless bean?

My problem is that I have a session bean called User and I want to access user info inside a stateless bean...

I am trying like this:

Ejb Side:

@Stateless
public class OfferManagerBean implements OfferManagerLocal, OfferManager
{
    @Resource 
    private SessionContext context;
    @EJB
    private ro.project.ejb.interfaces.User user;
    public String getUsername()
    {
        user = (ro.project.ejb.interfaces.User) context.lookup("java:global/project/projectEJB/User!ro.project.ejb.interfaces.User");
        return user.getUsername();
}

Client side

 User user = (User) ctx.lookup("java:global/project/projectEJB/User!ro.project.ejb.interfaces.User");
 user.setUsername("Alex");

 OfferManager offerManager = (OfferManager) ctx.lookup("java:global/project/projectEJB/OfferManagerBean!ro.project.ejb.interfaces.OfferManager");
 assertEquals(offerManager.getUsername(), "Alex");

The result of this test case is java.lang.AssertionError: expected:<null> but was:<Alex>

it fails.. It seems that how I am requesting the stateful bean is returning me a new instance...

  1. I know why this is not working. Because my test fails :P. I get a new instance..
  2. I want to check certain permissions of the logged in user in EJB because I don't want to count on the client side because I might do a mistake there or I will tell other developers to make a GUI for my project..
  3. I don't want to use Java EE Security beucause I don't know how to make the login in a RCP Application
  4. My main question is: How do I access a session bean (the same one the client own) inside an EJB.. is it possible? And how?

I am asking almost the same thing this guy is asking: Concept for reusable login session in rmi ejb calls

I want to do that but not with JAAS...

Thank you in advance

You should never inject a @Stateful bean (SFSB) in a @Stateless bean (SLSB). A SFSB lives as long as its client lives (the client is the instance where the SFSB is been injected, which is in this case the SLSB itself). SLSB's are however intented to be stateless and most containers have them in a pool. So whenever the SLSB goes back to the pool after use, it will be reused entirely elsewhere, but it holds the same SFSB instance as it was when the SLSB was been created for the first time! This may lead to undesireable results.

Also, everytime when you get the SFSB from JNDI, you will get a brand new instance which is unlike SLSBs not shared elsewhere. The SFSB's client is then the current client class instance where you've got the SFSB from JNDI. You're supposed to keep hold of this instance yourself and reuse the very same instance until you're finished with performing the transaction on it. One of the ways is storing it in the HTTP session yourself or in a session scoped managed bean of the MVC framework you're using.

The functional requirement is not entirely clear to me, so it's hard to give a suitable answer how to solve your particular problem, but I have the impression that you actually need to store the user in the HTTP session, not in a SFSB. The most common beginner's mistake as to session beans is namely that they incorrectly interpret the "session" in EJB context as being the HTTP session.

See also this related answer on a question of the same kind for a more in-depth explanation: JSF request scoped bean keeps recreating new Stateful session beans on every request? According to your question history you're familiar with JSF, so this answer should be easy understandable.