Re: Singletons in Tomcat(6.0)

2009-12-01 Thread Leon Rosenberg
everything below is just my knowledge :-)
comments inlined

On Tue, Dec 1, 2009 at 2:01 PM, jkv j.kumara...@gmail.com wrote:

 Hello,

 Is using singleton patterns in Tomcat (in servlets programming and deploying
 them in tomcat) a really bad idea?? I came accross many forum posts and
 wikis that warn about of OOM errors. One really useful post is
 http://wiki.apache.org/tomcat/OutOfMemory
 http://wiki.apache.org/tomcat/OutOfMemory  and this is really worth taking a
 look!!
 We are having a web application and have four or five singleton patterns,
 they are really necessary as singletons patterns, such as Xml parsers and
 Applications properties reader. I have some questions when they say. server
 being unable to reclaim the space for the entire webapp class loader.

 1) Will I be getting OOM errors only when I redeploy a war file (with
 singleton patterns) several times without re-starting my Tomcat? Or I will
 be getting the OOM errors even if I didnot redeploy the war file and the
 server crashes after some days??


Yes. The OOME are only redeploy related, they don't affect normal
production sites.

 2) I dont think we can GC a conventional singleton pattern by using a
 ServletContextListener, as the private instance variable is hard coded and
 we cannot access it from outside(and set it to null) as its never visible to
 the outside world unless we try using reflection??

Use the 'new' singleton pattern with enums. This should be free of OOMEs:

public enum MySingletonClass{
   INSTANCE;
   //functionality
}

access it simply by:
MySingletonClass.INSTANCE.method()...

regards
Leon

-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



RE: Singletons in Tomcat(6.0)

2009-12-01 Thread Caldarale, Charles R
 From: jkv [mailto:j.kumara...@gmail.com]
 Subject: Singletons in Tomcat(6.0)
 
 Is using singleton patterns in Tomcat (in servlets programming and
 deploying them in tomcat) a really bad idea??

No, it's fine.

 One really useful post is
 http://wiki.apache.org/tomcat/OutOfMemory

Unfortunately, the section on singletons in that page is completely bogus.  
Keeping an instance of a class in a static variable of that class does not 
inhibit garbage collection in any way in a current JVM.  I suspect the author 
was using a GC method in an older JVM where class unloading was disabled, or he 
simply didn't wait long enough for the class to be discarded (calling 
System.gc() won't do it).

The comment about starting extra threads is also erroneous.  As long as your 
webapp manages the threads by using a ServletContextListener to shut them down 
when the webapp is stopped, there's no problem.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY 
MATERIAL and is thus for use only by the intended recipient. If you received 
this in error, please contact the sender and delete the e-mail and its 
attachments from all computers.


-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



Re: Singletons in Tomcat(6.0)

2009-12-01 Thread Mark Thomas
Caldarale, Charles R wrote:
 From: jkv [mailto:j.kumara...@gmail.com]
 Subject: Singletons in Tomcat(6.0)

 Is using singleton patterns in Tomcat (in servlets programming and
 deploying them in tomcat) a really bad idea??
 
 No, it's fine.
 
 One really useful post is
 http://wiki.apache.org/tomcat/OutOfMemory
 
 Unfortunately, the section on singletons in that page is completely bogus.

Deleting it now...

I'll add some brief notes on some real problems.

Mark




-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org