Static class - one per JVM or one per app?  
Author Message
junk1





PostPosted: 2004-12-21 3:52:00 Top

java-programmer, Static class - one per JVM or one per app? I have a class called MyClass that contains a static variable (myVar).

I have two J2EE apps running on WebSphere under the same JVM,
MyFirstApp and MySecondApp. From MyFirstApp i set the value of myVar by
doing MyClass.setMyVar(3).

....what will be the value of MyClass.getMyVar() when run from
MySecondApp?

ie - will the static var have scope across the whole JVM or just the
web app?

Thanks

David Bevan

http://www.davidbevan.co.uk

 
cnpeyton





PostPosted: 2004-12-21 4:01:00 Top

java-programmer >> Static class - one per JVM or one per app? How do you have MyClass packaged? Is it in both EAR files? It will be
different in the 2 applications if packaged that way. The key isn't
the applications but the classloaders. If you had it in a utility jar
that was in the was/lib/ext directory, then it would be the same for
all applications. You can have a static resource and it can be
different in every classloader.

Chris

 
junk1





PostPosted: 2004-12-21 4:09:00 Top

java-programmer >> Static class - one per JVM or one per app? MyClass is in a jar file and an identical copy of that jar is in the
lib folder of each EAR file (each web app is in a separate ear file)

...would it make any difference if MyClass was in a single jar file
referenced in the classpath of both apps?

How do I know if both apps are using the same classloader or not?
Thanks

David Bevan

http://www.davidbevan.co.uk

 
 
junk1





PostPosted: 2004-12-21 4:16:00 Top

java-programmer >> Static class - one per JVM or one per app? I have a class called MyClass that contains a static variable (myVar).

I have two J2EE apps running on WebSphere under the same JVM,
MyFirstApp and MySecondApp. From MyFirstApp i set the value of myVar by
doing MyClass.setMyVar(3).

....what will be the value of MyClass.getMyVar() when run from
MySecondApp?

ie - will the static var have scope across the whole JVM or just the
web app?

Thanks

David Bevan

http://www.davidbevan.co.uk

 
 
cnpeyton





PostPosted: 2004-12-21 4:18:00 Top

java-programmer >> Static class - one per JVM or one per app? If you have 2 physical ear files, each with a web module, then you have
different class loaders for the two applications. You will have 2
copies of MyClass, one for each class loader. If you want to get
around this, you would have to move the access of MyClass to a class
loader higher up the chain that would be accessible to both
applications.

A different route would be to have 2 web modeuls in the same EAR. If
you package MyClass into a utility jar and put it in the EAR and then
marked it as available to the 2 different web modules, then i think you
would have only 1 copy of MyClass since there would only be one
application class loader. You do have a class loader for each web
module, but since the class wouldn't be accessible to this class
loader, loading would be delegated to the parent which would be the
application class loader.

There is a utiltiy from IBM called class loader viewer that is a plugin
for WebSphere that will allow you to browse class loaders for
applications and modules. I'll dig up the link and post it here.

Chris

 
 
cnpeyton





PostPosted: 2004-12-21 4:22:00 Top

java-programmer >> Static class - one per JVM or one per app? Here is the link to the WebSphere class loader viewer. I have found
this to be a very handy utility in diagnosing class loading problems.
You can browse class loaders for an applications, see what jars and
classes they have access to, search for classes and see which class
loaders can resolve them, etc...

http://www-128.ibm.com/developerworks/websphere/library/techarticles/0312_cocasse/0312_cocasse.html
Chris

 
 
junk1





PostPosted: 2004-12-21 4:26:00 Top

java-programmer >> Static class - one per JVM or one per app? Thanks, what you say sounds promising since I actually want separate
values of MyVar for each app.

David Bevan

http://www.davidbevan.co.uk