Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use @WebService, @Stateless and @Singleton altogether in one EJB 3 bean?

I'm using EJB 3 and JBoss AS 6.0.0.Final. I have a stateless session bean with the annotations @Stateless and @WebService. When I add the annotation @Singleton, the deployment is in error showing the message:

...name=ServiceBean, service=ejb3 is already installed

What can I do to avoid the deployment error?

like image 893
Julia Avatar asked Dec 05 '25 14:12

Julia


1 Answers

You can use @WebService and @Stateless or @WebService and @Singleton in the same bean which makes perfectly sense if you want to expose a POJO as both a web service and EJB.

Don't see much sense in using @Stateless and @Singleton in the same bean. When you use @Singleton, you are creating an EJB with all the EJB capabilities (transaction management, security, etc) exactly as with @Stateless. The only difference is how the container manages the EJB lifecycle:

  • @Stateless: the EJB instance is created immediately after the first request and when the request ends, the EJB is pooled and ready for reuse if another request comes in. However, if all the pooled instances are being used in the moment another request comes in for the same bean, the container creates a new instance of the same to serve that new request.
  • @Singleton: the EJB instance is created after the first request (by default - see @Startup to override this behavior) comes in and that will be the only instance created by the container. If another request wants to use the same EJB, the container will never create a new instance of it - the instance previously created will be used. It is like a @Stateless EJB with a pool size of 1 :) Aspects like concurrency are important when using these, but this is probably out of the scope of this post.
like image 159
Gonzalo Garcia Lasurtegui Avatar answered Dec 07 '25 03:12

Gonzalo Garcia Lasurtegui



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!