• 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

Singleton DAO in J2EE container

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1902
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic