Search code examples
javajakarta-eeglassfishstateless-session-bean

synchronized method in stateless session bean not working as expected in glassfish


I have a war file deployed in glassfish. We have some stateless session beans and we have 1 synchronized method in it.

However, I am noticing that more than 1 thread is able to enter the synchronized method concurrently. Is it possible that glassfish is instantiating 2 instances of this bean class? Is there any way around this?


Solution

  • Yes, of course it's possible. The spec even mandates that concurrent calls are handled by different instances.: this is one of the services offered by the container: it makes sure that concurrent calls are handled concurrently, and not sequentially, and you're free to implement your sesssion bean without caring about thread-safety (for example, by using instance variables), because the container takes care of it.

    What you want is a singleton.