Announcement Announcement Module
Collapse
No announcement yet.
Is Spring singletn thread safe? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is Spring singletn thread safe?

    I am using spring to create a singleton. The Spring config file looks as below
    Code:
    <bean id="batchProcessor" singleton="true"
    	    class="com.rodean.BatchProcessor">
    	</bean>
    Is it thread safe or in other words multiple clients could access the batchProcessor instance at the same time.

    In a regular way I do some thing like this
    Code:
    public class Singleton &#123;
    
        private static Singleton instance;
        
        public synchronized static Singleton getInstance&#40;&#41; &#123;
            if&#40; instance == null &#41; &#123;
                instance = new Singleton&#40;&#41;;
            &#125;
            return instance;
        &#125;
        
        private Singleton&#40;&#41; &#123;&#125;
    
        public static void main&#40; String &#91;&#93; args &#41; &#123;
            Singleton instance = Singleton.getInstance&#40;&#41;;
            // ...
        &#125;
    &#125;
    Thanks
    Rodean

  • #2
    The singleton is created by Spring's application context, and so there is no contention in it's creation. After that it's up to you (as is the case with Servlets) to make it thread safe.

    Comment

    Working...
    X