access existing instance stateful inside stateless, java ee 6

后端 未结 4 1099
旧巷少年郎
旧巷少年郎 2020-12-06 03:41

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

4条回答
  •  遥遥无期
    2020-12-06 04:10

    In general it is possible to access certain existing stateful session bean inside stateless bean. It can be for example given as argument to the business method of stateless session bean.

    But what you are trying cannot work. Reason is that both, dependendy injection (@EJB ) and lookup (ctx.lookup...) are guaranteed to call newInstance and as consequence you will have new instance.

    This is explained in specification with following words:

    A session bean instance’s life starts when a client obtains a reference to a stateful session bean instance through dependency injection or JNDI lookup, or when the client invokes a create method on the session bean’s home interface. This causes the container to invoke newInstance on the session bean class to create a new session bean instance.

提交回复
热议问题