aspose file tools*
The moose likes Servlets and the fly likes How are static variables treated with clustering? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How are static variables treated with clustering?" Watch "How are static variables treated with clustering?" New topic
Author

How are static variables treated with clustering?

Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
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
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
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.


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
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.


James Carman, President<br />Carman Consulting, Inc.
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
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.)
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Either way, initializations are usually safe.
For runtime updates, JMS or someother strategy is required to insure that they are all updated.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Dec 13, 2004
Posts: 429
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
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

Absolutely.


Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Dec 13, 2004
Posts: 429
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

Joined: Feb 07, 2004
Posts: 472

Zdorovo.
Why don't you store on backend whatever you want to avoid extra machinery? Like CMP against BMP.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How are static variables treated with clustering?