简体   繁体   中英

Injecting stateful bean into stateless

I have read that @Stateful bean (SFSB) should be never injected into @Stateless bean (SLSB). But what is the correct way to achieve the following?

@Stateless
public class FirstEJB
{
    @EJB
    private SecondEJB second;

    public void businessMethod()
    {
        second.businessMethod1();
        second.businessMethod2();
    }
}

There is some state that should be persisted between second.businessMethod1() and second.businessMethod2() , so SecondEJB can't be stateless. There is also no easy way to merge businessMethod1() and businessMethod2() methods into single one, because SecondEJB can have more than 2 business methods and they can be called in defferent combinations.

Actually I have tried to make SecondEJB stateful and it seems to work, but it leads to memory leak. There is no SecondEJB methods marked with @Remove annotation, but I have tried @StatefulTimeout with no luck: a lot of SecondEJB instances created and not removed. Can someone explain why it leaks?

Just don't use injection. You can use JNDI lookup of the bean at the moment your buisnessMethod is called and SecondEJB instance will be method scoped variable instantiated on every method call.

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