• Post Reply Bookmark Topic Watch Topic
  • New Topic

using synchronize keyword when using multiple instances of server.  RSS feed

 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am not sure if it belongs here, but it is a Java question, so i am asking here...

If we want only one thread to have access to a method, we normally use the synchronized keyword (or enclose it in a synchronized block)...

However, if i have multiple servers running(to perform load balancing or anything else), and each has its own copy of the class.....that synchronized keyword (or the block) will cease to perform its own function...

Is it correct?

In such a case, if we are performing some DB operations in that function, our only resort is to have DB level locks.....

or is there some other solution...

PS. I have never really worked on a case of multiple servers for load balancing etc...but i think the above scenario makes sense.

 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rahul,

basically you're right! Different servers usually means different JVMs so locks are only valid within one server/JVM.

Using a database as a central locking mechanism is one possible solution. Other possibilities I can imagine would be to use lock files on a central network filesystem. Or the Terracotta framework which allows your application to (more or less) transparently use multiple JVMs on different servers like it would be only one JVM with one big heap, i.e. Java locks in the code remain valid even though your application is clustered on several servers.

Marco
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!