views:

49

answers:

3

Hello!

I have a Spring web application with singleton services. There're also some singleton variables such as map with session data for all authenticated users and so on. That all works nice on one server, but how could this system be distributed between a few servers? The system runs under Tomcat.

Thanks in advance!

A: 

That's one of the reasons that singletons are considered stupid or harmful. Have a look at Google Singleton Detector. This is a tool that detects singletons in java byte code. Consult the FAQ to see what are the problems with singleton.

If you want to cluster your application avoid the use of singletons. Consider persisting state into a database.

kgiannakakis
Persisting everything into database looks inefficient. Are there any other methods?
Andrey
A: 

Terracotta has Spring support and makes sure it's only one singleton bean among all the instances in the Terracotta cluster.

All you need to do is to enable Terracotta and declaratively add the singleton beans to the cluster.

If you bother to register a community account, you can read more in their online documentation.

Espen