• 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

Why does this happen?

 
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a class which I put in the servlet directory of the web server (Netscape IPlanet).

Now I have two identical servlets (named differently of course) which access the TestClass class. Both are as follows:

Running the two identical servlets shows that each are in the same context, but each has a different timestamp. I was hoping to get the same timstamp for each, which would show that I am using the same instance of the object in both servlets. Instead it appears that two separate instances of the object are being instantiated.
I know that I could effectively share this object by placing it in the ServletContext and accessing it that way, but that's not the point. I am more curious as to WHY there are different instances of the object being created. Anyone have any ideas?
Thanks,
Jay
[This message has been edited by Jason Menard (edited February 20, 2001).]
[This message has been edited by Jason Menard (edited February 20, 2001).]
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know if it's your problem, but you really need to synchronize the creation of "tc". If two threads try to call getInstance(), they can both end up creating a new timestamp.
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Frank, you're right. That's not the problem in this case though, but I did go ahead and synchronize things.
Anyone have any other ideas? I know this almost sounds like a test question but it's actually work related.
Thanks,
Jay
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible that your servlets are loaded by different classloaders or web-apps? "static" members are only shared within the same classloader.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Frank Carver:
Is it possible that your servlets are loaded by different classloaders or web-apps? "static" members are only shared within the same classloader.


Agreed -- sounds like you may be caught out by the classloaders. Especially older servlet engines like to instantiate new classloaders. For example, when a class needs to be re-loaded because you have updated it, the engine may use a fresh classloader to do it.
This can also lead to unexpected ClassCastExceptions if you're trying to cast between different loads of classes.
The servlet spec has this to say about it: Many servlet containers support servlet reloading for ease of development. Reloading of servlet classes has been accomplished by previous generations of servlet containers by creating a new class loader to load the servlet which is distinct from class loaders used to load other servlets or the classes that they use in the servlet context. This can have the undesirable side effect of causing object references within a servlet context to point at a different class or object than expected which can cause unexpected behavior. Therefore, when a Container Provider implements a class reloading scheme for ease of development, they must ensure that all servlets, and classes that they may use, are loaded in the scope of a single class loader guaranteeing that the application will behave as expected by the Developer. (section 4.6 in version 2.2 of the spec).
- Peter

[This message has been edited by Peter den Haan (edited February 22, 2001).]
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the info. I guess it could be a classloader thing. There really is no way to know for sure is there? The web server btw is the latest version of Netscape IPlanet, if tha tmeans anything.
What I'm going to end up doing is make a servlet that instantiates the class and puts it into the ServletContext. This servlet will be run at server startup. Then all servers sharing that context will be able to access that same instance of the class.
Thanks for the replies.
Jay
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not a classloader thing. The simplest explanation is that you are creating a new TestClass tc on every request and since you are not holding any reference to it, it's being garbage collected after the request but before you make your second request to other servlet. Try to hold a reference in the servlet for the servlet's life time and then try your test again. If they are still different THEN it's a classloader problem.
 
Ruth Stout was famous for gardening naked. Just like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic