简体   繁体   中英

Spring Singleton in Clustered Environment

As discussed in this post, it is not suitable to use singleton in clustered environment (because of multiple singleton objects in different JVMs), this must be true for singletons created by Spring framework.

If that's correct, then we have to be a lot careful using Spring framework to use singleton classes. Can you please tell if this is correct understanding?

This is not necessarily the case.

It is a problem to use singletons across separate JVMs if they share meaningful state . For instance, a singleton that stored and issued incrementing IDs would be very dangerous if two separate instances existed on two separate application servers that were both part of the same application.

There is nothing inherent in Spring that makes this more or less tricky to deal with. Your Spring beans (likely services) should aim to maintain as little state as is practical as a matter of good practice. If they need to share state, then you will have to solve this problem just as you would with any other shared state.

A great many people use Spring in clustered environments and do not run into any problems relating to the above. I'm one of them!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM