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

Using a Singleton for a DAO class

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234



Hi Good day to all ,


while declaring DAO on to the application-hibernate.xml , is it good to declare the DAO class as a singleton or not ?

Please tell me the best approach .

Thanks in advance .




Save India From Corruption - Anna Hazare.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Personally I would always have all my DAOs as Singletons in Spring. I would never hold state in a DAO, so no reason to have more than one, one can handle a lot of clients simultaneously, and since it doesn't hold state you don't have to worry about thread safety and synchronization.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
All stateless objects should be singleton.


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Thank you very much Mark , Kengkaj .

That helped .


One more question on this .

As far my knowledge , By Making an Object singleton will result in bottleneck know , so how can simultaneous requests can be handled ? (AS DAO are made singleton )

Please let me know the answer for this .
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Ravi Pavan wrote:
As far my knowledge , By Making an Object singleton will result in bottleneck know , so how can simultaneous requests can be handled ? (AS DAO are made singleton )

It depends on if your code use synchronize, if not it's fine. There is only one instance but it can be accessed by many threads.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Thank you very much Kengkaj Sathianpantarit . You really made the difference .
Myself very thankful to you .


Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
You're welcome. Glad to know my answer is useful.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using a Singleton for a DAO class