in my JavaEE 6 application (JBoss 6.0.0.Final) I have a class hierarchy outlined like this
@Stateful public class UserFacade
{
@Inject @MyRepository
private EntityManager em;
}
@Stateful public class UserBl
{
@EJB private UserFacade fUser;开发者_StackOverflow
}
public class MyServlet
{
@EJB private UserBl blUser;
}
The servlet is used during a login process and it works fine. But if the servlet is called from the same browser after an inactivity period of about 10 minutes, the EntityManager em
in UserBl
became NULL
(checked this explicitly before using it).
In a other application (JBoss 5.1.0.GA) I've had a similar issued and solved it by explicitly checking for NULL
and looking up the EntitiyManager
from the context in that case.
I'm wondering if there is a fundamental design flaw or if I missed something. In this scenario @Stateless
Beans are possible, too. But in my understanding this should also work with @Stateful
Beans.
As far as I recall, injection of stateful beans has been problematic (at least in JavaEE 5). It might be a passivation-activation issue.
But you don't really need stateful beans here - the EntityManager
is per-transaction (by default; you can set it to extended which allows it span multiple requests) and a stateless bean is the proper way to go.
精彩评论