• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

two web applications referring to a common jar

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,
Not sure if this is the right forum for this question

I have two web applications ( Ap A & B ) deployed on a server as war files.
These wars have a "Manifest" file in which is specified a common library jar file containing utility classes. ( Util.jar )

So essentially both applications make use of this jar file .

This jar file is kept at a common location on server.

Questions ::
1 >Will different classloaders load these applications separately ?
2 >Will there be any issues related to "thread" safety ?
I dont know what the right word is - by thread I mean if both applications make a call to a Utility class in which there are static instance level variables .

So will a change in the value of these variables made by one app be reflected in the request simultaneously coming in from another app ?

Or does separate classloaders also mean an additional compartment within the jvm for each app ? i.e the classloader demarcates the boundary for each app ?

Thanks ,
~satish
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have two web applications ( Ap A & B ) deployed on a server as war files. These wars have a "Manifest" file in which is specified a common library jar file containing utility classes. ( Util.jar )


I don't think that .war files use manifests.

Will different classloaders load these applications separately ?


Yes, at least three classloaders will be involved: one for the common libraries, and one for each of the web apps.

Will there be any issues related to "thread" safety ? I dont know what the right word is - by thread I mean if both applications make a call to a Utility class in which there are static instance level variables.


If there's a chance that the code will be called simultaneously by more than one thread, then, yes, you'll need to make it thread-safe.

So will a change in the value of these variables made by one app be reflected in the request simultaneously coming in from another app ?


Yes. If you want to avoid this then you need to keep a copy of the library in each web app's WEB-INF/lib directory. You still need to worry about thread safety, of course, since the servlet that uses the library could be hit simultaneously by several requests.
[ August 26, 2008: Message edited by: Ulf Dittmer ]
 
satish bodas
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf for the response .

In a couple of examples I am seing war files with manifest that contains jar references .

Regarding " thread safety " - I totally understand that the servlet container uses a muti thread approach

What I was worried was if two apps simultaneously use the utility jar - will there be any issues - and from your answer - there will be issues .

Thanks for these details .

My only query is - so what really does the class loader do ?
what would have happened if only one classloader did the loading of all three ( two apps and one jar )

I am not knowledgeable regarding class loaders so pardon if these questions are basic .

Thanks ,
~satish
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by satish bodas:
Hello ,
Not sure if this is the right forum for this question

I have two web applications ( Ap A & B ) deployed on a server as war files.
These wars have a "Manifest" file in which is specified a common library jar file containing utility classes. ( Util.jar )

So essentially both applications make use of this jar file .

~satish




Don't do this at home!

If you do not NEED to share objects between web-apps (which you SHOULD NOT anyway ), do NOT USE a common library, you will get into a hell of class loaders.

Then, if you do not NEED static variable, do not use them, and if you think you need them then use singletons instead.

Somethig like this:

You can use synchronized getters and setters to access/modify private fields of a singleton, which is more secure then accessing static fields.


Classloaders errors, and errors realted to static variables are quite nasty to debug, always try to find a solution which will not possibly trigger this kind of errors.
 
Mario Minutolo
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by satish bodas:

My only query is - so what really does the class loader do ?
what would have happened if only one classloader did the loading of all three ( two apps and one jar )



Classloaders in a webapp are arranged into a tree structure.
There is a root classloader which belongs to the 'server' and then there is the common classloader, which is shared between apps, and then there is a classloader for each webapp.

Since webapp are distinct entities, must keep their resource 'private', using a classloader per webapp enforce this policiy.

You should not access resouces of a webapp from another, using differnt instance of a class loader you cannot do this, so you can't violate this fundamental policy of a web-application.
 
Catch Ernie! Catch the egg! And catch this tiny ad too:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic