aspose file tools*
The moose likes Spring and the fly likes Spring singleton objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring singleton objects" Watch "Spring singleton objects" New topic
Author

Spring singleton objects

Ragupathirajan Venkatesan
Ranch Hand

Joined: Aug 18, 2010
Posts: 62
Dear All,

As per my idea, Spring framework creates the singleton objects for all the bean that is configured. What happens when two or more concurrent clients expecting the same pojo or services. Lets say when the clients trying to update user profile database. In this case each user need the same pojo or service to update the data base. If that is the case, whether application server takes care of serving the required objects to client or spring has separate thread logic for this.

Thanks in Advance.
Ragupathirajan V
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2402
    
  28

The threads are managed by the application server, not by spring. When a new request comes in, the app server starts a thread to service the request ( actually uses a thread from a pool, but lets not get into that right now) and calls the spring controller on that request. The controller internally calls the services. If the controller and services are configured to be singletons, then there will be one instance being called from multiple threads.
Ragupathirajan Venkatesan
Ranch Hand

Joined: Aug 18, 2010
Posts: 62
Thanks a lot for your information. 'Thread takes care of invoking the controller' you mean dispatcher Servlet? or anything else. Please clarify bit extra. More over still am not so clear. Let me post in this way. Thread x is comes with request and object 1 will be served by the application server to populate the user 1 details in that pojo for updation. Next Thread y comes with another request, then new copy object 1 will be served or same object 1 will be served which already given to thread x. If second case is true then what happens to values populated by thread x? If first case is true then we can assume that for application server is giving new object for each thread. Then what happens to spring singleton concept.

I agree my question is somewhat complex, please help me to get better idea on spring object management.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2402
    
  28

The same object will be called by both threads
Ragupathirajan Venkatesan
Ranch Hand

Joined: Aug 18, 2010
Posts: 62
Thanks for your reply. If you are interested could you please explain about what happens to the values assigned to that object by previous thread. I know that, this is a simple/repeated question but this answer will clear my doubts fully. Whether application server will do anything like activation/passivation like ejb does.


Thanks & Regards,
Ragu
Ragupathirajan Venkatesan
Ranch Hand

Joined: Aug 18, 2010
Posts: 62
Hi,

I studied somewhere like, ejb will create certain number of objects initially and maintain them in a pool. I will serve those objects to the request one by one, when all the objects got assigned from pool, then it will check the object which is idle currently apart from the assignment. If still all the objects are free then it will create a new object and assign.

If the above statement is true about EJB then how the same happens in spring in the absence of EJB. Hope now you can understand my actual idea to post this question.

Please provide your valuable suggesion or redirection to any topic.

Thanks,
Ragu
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2402
    
  28

Ragu,

Before you understand what happens to the variables, you have to understand thread safety and state fullness. A bean is said to be state full if it keeps data pertaining to the current context in data members

So, for example



The above code will work, but is not a good way to write a service. The current user that you are trying to retrieve is stored in a data member, which means it is shared between threads that call the getUser function. This means that it is not thread safe because it is stateful

A better way to write this code is



The current user is not stored in a shared variable. It's in a local variable, and each thread gets its own variable. It's not stateful. It's thread safe

Stateless beans can be singletons. In spring nothing stops you from declaring a stateful bean as a singleton, but if you do, you are asking for lot of trouble. Your code will be thread unsafe. Singleton beans will never be in a pool. As the name singleton suggests, there will always be one instance of a singleton bean.

If you do have stateful beans, you can use a different scope. This page talks about the various bean scopes. I don't think spring does pooling on non singleton beans. Although I might be wrong there
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring singleton objects