File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes Singleton DAO in J2EE container Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Singleton DAO in J2EE container" Watch "Singleton DAO in J2EE container" New topic
Author

Singleton DAO in J2EE container

Dave Boden
Ranch Hand

Joined: Nov 08, 2000
Posts: 37
The DAO pattern forms part of the core J2EE pattern set.

The DAOFactory is supposed to create a new Dao() rather than the Dao implementing the Singleton pattern.

This feels right to me. However, it does result in lots of objects being created during the running of the J2EE application, probably with no instance variables. I doubt that this takes up many resources and that they are garbage collected easily after the business method has finished with the DAO.

However, you could save any instantiation by using the Singleton pattern for the Dao.

Are there good reasons why not to use the singleton pattern in this way?

One reason that occurs to me is that it would cause confusion when subclassing a Dao. I'd be thinking to myself, "What does the author of the superclass want me to do? Should I make my subclass as singleton too...".

Any others?
Theodore Casser
Ranch Hand

Joined: Mar 14, 2001
Posts: 1902

Personally, I think the 'confusion' issue would be a serious problem on its own. But while it might add to the instantiation and objects floating around, I think that you're right in saying it's worth the simplicity it lends in trying to determine what's intended.

Just my $0.02.


Theodore Jonathan Casser
SCJP/SCSNI/SCBCD/SCWCD/SCDJWS/SCMAD/SCEA/MCTS/MCPD... and so many more letters than you can shake a stick at!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Dave Boden:
I doubt that this takes up many resources and that they are garbage collected easily after the business method has finished with the DAO.


With modern JVMs, short-lived objects really are very cheap. Modern garbage collectors are optimized for those kinds of objects, so they shouldn't be a problem at all.

Introducing a Singleton simply would in almost any case increase complexity and reduce flexibility without enough advantages to make it worth the costs.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
 
subject: Singleton DAO in J2EE container