• 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 ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • 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
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.

 
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.

Marco
 
this is supposed to be a surprise, but it smells like a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic