In case others are not clear enough: you are doing it wrong!;)
I agree that this can be confusing, but the only session in the beans beans is stored in the bean proxy that you get from the InititalContext.
The various beans that you get from this context do not use a shared session. In EJB, beans are not stored in an EJB session, but they are this session.
In other words, InitialContext (ctx in your code) is NOT the EJB equivalent for HttpSession.
The worst part is that in your code the user is an EJB bean. It is not right.
The user is a noun in your application. They are represented by JPA entities or simple "normal" java beans. EJBs are designed to implement verbs in your application: Services, DAO, Repositories, etc.
It is assumed that the state in the beans state session will contain model data during the business process (for caching, locking, redundancy, etc.). In no case should this state be model data.
My advice: let your current "design" go. Do not try to fix it, do not try to justify it. Let it, delete your code, do not look back. Read some good books about EJB, and then get started.
Good luck
source share