Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton DAO in J2EE container

 
Dave Boden
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1902
Hibernate Netbeans IDE PHP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic