[jboss-user] [EJB3] - How do javax.ejb.Singletons work?

John Lee do-not-reply at jboss.com
Mon Mar 25 15:47:50 EDT 2013

John Lee [https://community.jboss.org/people/jhn134910] created the discussion

"How do javax.ejb.Singletons work?"

To view the discussion, visit: https://community.jboss.org/message/804718#804718

Are singletons implemented using synchronized methods?

I have an EJB singleton, say:

public void MySingleton {

     public void receive(final String string) {

Consider the scenario, where this is running in JBOSS standalone. This singleton shall be called from multiple threads, with names Thread-1, Thread-2 and Thread-3 in that order. Thread-1 calls the blocking method which doesn't return for 10 seconds. Thread-2 arrives at the receive method shortly after Thread-1, followed shortly by Thread-3 and both wait for the lock to be released by Thread-1. Is there anything in the EJB standard regarding singletons that guarantees that Thread-2 shall aquire the lock before Thread-3 once Thread-1 releases it?

>From my testing with synchronized methods I have verified that synchronized methods provide no such guarantee.
I'm going to test this singleton behaviour but if my test shows that Thread-2 is indeed processed before Thread-3 and order is maintained, it shall not tell me that this is guaranteed behaviour, and is merely a consequence of using a particular OS, JVM or EJB container implementation. So does @Singleton offer any guarantees?

If it does, are there any gotchas about using this behavior to queue up client requests against singleton where they wait to acquire the lock?

Reply to this message by going to Community

Start a new discussion in EJB3 at Community

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20130325/496810a3/attachment-0001.html 

More information about the jboss-user mailing list