wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Question about static synchronized across multiple servers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Question about static synchronized across multiple servers" Watch "Question about static synchronized across multiple servers" New topic
Author

Question about static synchronized across multiple servers

D Arnott
Greenhorn

Joined: Oct 13, 2011
Posts: 7
We have a simple java program that generates a unique ID it pulls from a table and then increments. The classes are developed using the public static synchronized method, but we are stilling having issues of multiple classes being instatiated and causing conflicts.

My question is, about the best practice to implement public static synchronized methods (or something similiar) across multiple servers. I don't want multiple instances of this class to operate at the same time. The other option I was considering is some sort of mutex lock on the table, but that seems a bit messy.

Any help would be greatly appreciated!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

The simplest way is to get the database to enforce mutual exclusion. Trying to synchronize Java code in different JVMs on different machines could probably be done, but since databases already include suitable mechanisms, I'd say just use one of those. Like a stored procedure? Or reading and updating the table in a transaction?
D Arnott
Greenhorn

Joined: Oct 13, 2011
Posts: 7
Thank you for the great suggestion. I did mis-lead you by saying the program talks to a table. It is not in a database rather there is an object in LDAP with an attribute that stores the number. I am not sure if LDAP has the ability to enforce mutal exlusion, but I will do some research on it.
Nomaan Butt
Ranch Hand

Joined: Oct 19, 2011
Posts: 54
you can have singleton object approach, to keep one instance of the class always you can synchronize the creation of the object on the class variable,

like this,

D Arnott
Greenhorn

Joined: Oct 13, 2011
Posts: 7
Its actually funny you posted this. Just yesterday I found this solution and started to implement it. Thanks for following up!
Nomaan Butt
Ranch Hand

Joined: Oct 19, 2011
Posts: 54
i saw your post today itself, by the way this can help others who may come accross the same scenario...
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1752
    
    7

Multiple instances of this "singleton" will still exist simultaneously across multiple VM's, so it's not a viable solution.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
D Arnott
Greenhorn

Joined: Oct 13, 2011
Posts: 7
Understood. Howver, it does solve my issue because I will only have the class loaded from one JVM at a time. Our servers are setup in a Active/Passive configuration so they are not load balanced where it would hit both at once. Since the singlton doesnt hold any state and only managages the connection to LDAP it will work fine.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1752
    
    7

Yes, in that case it will work, but I thought synchronization across mutiple servers was the very thing you were trying to solve...
Anyway, if you ever need to run multiple load-balanced nodes you'll be facing the same problem you're dealing with now.
Scalability of this solution is non existent, which is something to at least stop and think about.
Bo Lin
Greenhorn

Joined: Nov 17, 2002
Posts: 6
Jelle Klap wrote:Yes, in that case it will work, but I thought synchronization across mutiple servers was the very thing you were trying to solve...
Anyway, if you ever need to run multiple load-balanced nodes you'll be facing the same problem you're dealing with now.
Scalability of this solution is non existent, which is something to at least stop and think about.


so what do you suggest?
 
 
subject: Question about static synchronized across multiple servers
 
Similar Threads
Accesing two synchronized methods
Thread
Class modifiers
Static Synchronized methods
servlet instance