0 Replies Latest reply on Mar 25, 2013 3:47 PM by jhn134910

    How do javax.ejb.Singletons work?

    jhn134910

      Are singletons implemented using synchronized methods?

       

      I have an EJB singleton, say:

       

      @Singleton

      public void MySingleton {

           ...

       

           public void receive(final String string) {

                x.callMyBlockingMethod(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?