aspose file tools*
The moose likes Servlets and the fly likes Singleton And Exceptions Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Singleton And Exceptions Question" Watch "Singleton And Exceptions Question" New topic
Author

Singleton And Exceptions Question

Chris Heady
Greenhorn

Joined: Jun 28, 2001
Posts: 9
Hi,
I don't often use the normal lazy instantiation singleton implementation of:

I don't use it because of their is no way of guaranteeing that there will only be one because of synchronization issues (doubly checked locking). I use:

However, this piece of code comes into trouble if my constructor throws exceptions. It won't compile because you cant throw exceptions in the initializer area. Say the first time it is used I need to make an db call, and I want the user of the singleton to handle the exception so I can't handle it in the constructor. Any ideas?
[ June 21, 2002: Message edited by: Chris Heady ]
[ June 21, 2002: Message edited by: Chris Heady ]
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
well, first i think you did a typo and you should have the Singelton self instances as static also.
secondly, why not just make the static getInstance() method synchronized...? wont that solve your problems?
Chris Heady
Greenhorn

Joined: Jun 28, 2001
Posts: 9
Originally posted by Roy Ben Ami:
well, first i think you did a typo and you should have the Singelton self instances as static also.
secondly, why not just make the static getInstance() method synchronized...? wont that solve your problems?

You are right that the selves should be static. If you make the getInstance synchronized, then you would slow down performance heavily since many classes will most likely be using the getInstance method. The way that gets you lazy instantiation and quick access if the servlet has already been created is Double Checked Locking(DCL):

But that has problems as demonstrated in this article: DCL javaworld article
[ June 21, 2002: Message edited by: Chris Heady ]
Fritz Passow
Greenhorn

Joined: Jun 16, 2002
Posts: 3
However, this piece of code comes into trouble if my constructor throws exceptions. It won't compile because you cant throw exceptions in the initializer area. Say the first time it is used I need to make an db call, and I want the user of the singleton to handle the exception so I can't handle it in the constructor. Any ideas?

You could catch and save the exception, then re-throw it from getInstance().
Chris Heady
Greenhorn

Joined: Jun 28, 2001
Posts: 9
ahh, your right, I can save it as a member variable, and then check to see if it is not null and throw the exception in the getInstance method. Thanks very much.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Singleton And Exceptions Question
 
Similar Threads
Thread-safe lazy Singleton creation
Singleton Pattern
private constructor
Exceptions and Singleton constructors ?
private constructor