Comment – @ singleton in Java EJB

I have an EJB that needs to be single and stateful because it will become a connection pool My question is:

>If I define an EJB with @ singleton annotation, does it have state by default, or must I define it with @ stateful? > Can I use @ stateless annotation?

Try to find some documents about this but no luck, so anyone with knowledge can share your wisdom, maybe a link or two

Solution

EJB tutorials shows that EJBs can be singleton or stateful or stateless I've never tried to use more than one of these comments, but I believe it's right to use only one of them

From this link:

So your question:

If for state, you mean the ability to maintain its state, the answer is: Yes, a singleton will be the default state

Please remember that there are some special cases where singleton does not behave like singleton. Please read this article In general, if you are not outside the cluster and avoid using the default constructor, you will not run such a risk: you need to always inject it into another EJB or web client using an EJB reference:

@EJB MyEJB myEJB;

Finally, take a look at this part of the Java EE 6 tutorial on EJB life cycle, and explain that the main difference between stateful and other EJBs is the ability of containers to be passivated in their life cycle The difference is that strictly speaking, the saying "single default state" is the main reason for being incorrect, but it is correct in the context of your problem

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>