简体   繁体   中英

What happens when you hold a reference to a stateless EJB on a long life object?

Just a thought experiment. I know it is absolutely NOT how they are supposed to be used, but I want to know how the system would break or cause problems.

Imagine a session or an application scoped or any other kind of object which lifespan is longer than a request. Which has a @Stateless EJB field, received by constructor or a setter, and holds that reference forever.

What happens to that EJB and the container?

Your "stateless EJB field" is a reference to a Stateless EJB.

However, it does not refer directly to the EJB instance. The referred object is a proxy for the EJB.

Every time you invoke a method through that proxy, the container creates or otherwise acquires a reference to an instance of the EJB and then invokes it. Containers may have a pool of these EJB instances so that it can acquire them quickly. It can do this because they are Stateless after all.

If you have:

public class Foo {

    @EJB
    private Bar myStatelessEJB;

    public void doSomething() {
        myStatelessEJB.eat();
        myStatelessEJB.something();
    }

}

Then each invocation of myStatelessEJB may call a completely different instance of Bar .

Therefore it makes no difference how long you keep the reference to the EJB. The container may forget it completely between calls or it may return it to a pool for other clients.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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