views:

38

answers:

2

Now, with EJB 3.1, we can find the javax.ejb.Singleton annocation that can ensure that this bean is going to be singleton.

Is there a way that i can ensure singleton using stateless beans in EJB 3.0 with some modifications in my code (use of the keyword static, or other way to do that....)

+1  A: 

Is there a way that I can ensure singleton using stateless beans in EJB 3.0 with some modifications in my code (use of the keyword static, or other way to do that....)

No, nothing standard. Your container might provide some specific extensions though (e.g. JBoss has a proprietary @Service annotation).

Pascal Thivent
+3  A: 

If you're able to limit your @Stateless bean pool size to exactly 1, then you can get pretty close to an @Singleton.

The effect would be like having an @Singleton that uses @Lock(WRITE) for all calls (i.e. no concurrency) and does not eagerly startup via @Startup (it will start on first access).

You might still be able to get the effect of @Startup if your platform has the option to eagerly fill @Stateless bean pools.

David Blevins
Ah, smart. Didn't think about this. +1
Pascal Thivent