• 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

Servlet instance is started twice

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I got a problem with my servlet on OC4J, because it instance is being created twice by the container. My web.xml:



and my servlet code:



1. The init() method is called twice
2. The destroy() method isn't called
3. The servlet is mapped only once
4. Each time servlet starts, the instances attribute is 0 and the previously created singleton JournallingOrdersGenerator is null

The problem is, that when servlet starts twice, the singleton object JournallingOrdersGenerator is again null and is created (and started) twice.

I'm desperated and I have no more ideas how to fix it. Do you know any solution? I've read that on Tomcat is the same problem.

Regards
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure. Just don't use an instance variable in a servlet for an object which needs to be a singleton in your application's scope.

Instead create a class which implements ServletContextListener. When it is initialized:

Then anywhere you have access to the servlet context (e.g. in any servlet), you can get that attribute from it and use it.

You'll need to mention that class in your web.xml like this:
 
Mateusz Moroz
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
GREAT! Even the servlet is now being created once, what I don't understand (but its way it should!)
Thanks Paul
[ November 06, 2008: Message edited by: Mateusz Moroz ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic