Difference between @Stateless and @Singleton

泄露秘密 提交于 2019-11-29 23:54:20

You're seeing the same output because there is only one client accessing the EJB at a time. The application server is able to recycle the same stateless EJB object for each call. If you try a concurrent access – multiple clients at the same time - you'll see new stateless instances appearing.

Note that, depending on the server load, even two consecutive method invocations made by the same client may end up in different stateless EJB objects!

For a singleton EJB, there will no difference – there is always only one instance per application, no matter how many clients try to access it.

According to the Oracle Documentation:

Singleton session beans offer similar functionality to stateless session beans but differ from them in that there is only one singleton session bean per application, as opposed to a pool of stateless session beans, any of which may respond to a client request. Like stateless session beans, singleton session beans can implement web service endpoints.

Singletons can't be passivated:

Like a stateless session bean, a singleton session bean is never passivated and has only two stages, nonexistent and ready for the invocation of business methods(...)

The documentation explains when to use each kind of bean, and Singleton beans has the following:

A single enterprise bean needs to be accessed by multiple threads concurrently.

The application needs an enterprise bean to perform tasks upon application startup and shutdown.

So, for your example there's no difference between the two annotations.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!