• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How are static variables treated with clustering?

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

Since each node of a cluster has it's own JVM, how are static variables handled? I'm assuming that the ServletContext of an application is kept synchronized throughout the cluster. Does the same hold true for static variables?

The reason I'd prefer not to use the ServletContext is because only Servlets and JSPs have access to it. When my application loads, I'd like to initialize some static variables based on web.xml context parameters as well as data read from a database, and allow multiple classes to have read-only access to these.

Thank you,
Yuriy
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static variables are obviously scoped to the JVM they're living in. So, in a clustered environment, if you change one, the other JVMs are not going to know about the change. However, if all you're doing is initializing some stuff from the DD then you're fine.
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nathaniel Stoddard:
Static variables are obviously scoped to the JVM they're living in. So, in a clustered environment, if you change one, the other JVMs are not going to know about the change. However, if all you're doing is initializing some stuff from the DD then you're fine.



Actually, that's not entirely true. Static variables are scoped to the class in which they are defined. So, if a class is loaded by more than one classloader within the same JVM (classes are not equal unless they are loaded by the same classloader), then there will be multiple copies of the static variable within the same JVM.
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by James Carman:


Actually, that's not entirely true. Static variables are scoped to the class in which they are defined. So, if a class is loaded by more than one classloader within the same JVM (classes are not equal unless they are loaded by the same classloader), then there will be multiple copies of the static variable within the same JVM.



Ha, you make me laugh, Mr. Carman. But yeah, you're absolutely correct. I didn't want to confuse Yuriy though. Okay, it didn't really occur to me to mention it. (Yuriy: everything Mr. Carman said is true.)
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Either way, initializations are usually safe.
For runtime updates, JMS or someother strategy is required to insure that they are all updated.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, since the values are coming from the web.xml file, you should be okay. However, if you change the web.xml file and start up another instance within your cluster, you might get mismatched values between instances. Unlikely, but possible.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Souther:
Either way, initializations are usually safe.


Unless, of course, the init data is subject to change between the time that the first instance loads and any others load.
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the replies, everyone!

All right, so then every instance inside the cluster does it's own initialization when it starts up? And by default a web application will have a single class loader per instance?

I'm also assuming all of the above applies to classes implementing the Singleton pattern as well. But out of curiosity, what if a Singleton is stored in the ServletContext. Would marking it "serializable" be enough for changes to it to propogate to all instances?

Thank you,
Yuriy
 
Ranch Hand
Posts: 472
Objective C Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Absolutely.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Would marking it "serializable" be enough for changes to it to propogate to all instances?



No.
You would need to look at the documentation for your server to find out what capablities it has.
Tomcat, for instance, has session replication but nothing for context scoped objects or for singleton objects.
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ben!

That's something to think about I guess. While right now we're working on what amounts to a demo of the application we'd make if the client likes it, scalability could be an issue in the future so it might not make sense to populate the ServletContext with tons of stuff.

-Yuriy
 
D Rog
Ranch Hand
Posts: 472
Objective C Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Zdorovo.
Why don't you store on backend whatever you want to avoid extra machinery? Like CMP against BMP.
reply
    Bookmark Topic Watch Topic
  • New Topic