• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

using synchronize keyword when using multiple instances of server.

Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

this is supposed to be a surprise, but it smells like a tiny ad:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic