File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes lifespan of Spring singleton bean Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Application Frameworks » Spring
Reply Bookmark "lifespan of Spring singleton bean" Watch "lifespan of Spring singleton bean" New topic
Author

lifespan of Spring singleton bean

John Sears
Greenhorn

Joined: Sep 15, 2009
Posts: 14
Hi,

I'm having some trouble with a singleton spring bean in a web application context. It's being used as an ID manager, handing out unique ids to prototype-scoped beans. Anyway, I had been under the impression that once the application was deployed, Spring would create this bean once and keep it alive indefinitely as long as the deployment was active. This application has been active for several months, and the bean has been displaying behavior that is leading me to wonder if perhaps there is some inactivity trigger that is killing the bean and then re-initializing it on new request. If this is the case, I need to do something to persist the data that the bean is holding in its private instance variables... Any information about the death-cycle of singleton beans would be much appreciated, I haven't been able to find a definite answer in the Spring documentation.

Thanks,
John
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12612
As long as the application isn't restarted and the Spring context isn't reinitialized you should always get the original instance.

But if it's important to system function this kind of information should be persistent somewhere anyway, because what if you lose power? Or redeploy the application? Or anything?
John Sears
Greenhorn

Joined: Sep 15, 2009
Posts: 14
Gotcha, thanks. Definitely valid concerns about if the app goes down, although for me if the entire deployment is down then the info becomes irrelevant. Just a problem if the bean was dying and the rest of the app continued.

I guess that alleviates my concern about the lifecycle, so here goes another theory to explain my problem: in a clustered environment, does Spring 2.5 automatically distribute singleton beans across the machines in the cluster? For instance, if in a single clustered app you declare a singleton bean with private instance variables, are those variables synchronized across the cluster? If one machine modifies those variables, will the modification reflect when read from another machine? I saw something about having to use Terracotta with Spring, but that seemed like more for web service calls.

Thanks!
John
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12612
I would have assumed that each jvm would get its own instance; this should be trivial to test. I'm not aware that spring has any concept of clusters out of the box.
John Sears
Greenhorn

Joined: Sep 15, 2009
Posts: 14
Thanks. I think I will try storing/retrieving the bean in JNDI to ensure that all clustered jvms get the same reference.
 
 
subject: lifespan of Spring singleton bean
 
Threads others viewed
Backing Bean Constructors
Singleton Creation
Spring - Multiple IoC Containers with Singleton Bean in a JVM
Who are logged in? - without spring security
Newbie Singleton Enforcement Question
IntelliJ Java IDE