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

Is it Okay if a DAO class is made as Singleton

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Assume that we have methods such as insert , update , select and delete inside my DAO class . So please tell me if i make this DAO class as Singleton .

In my opinion this saves a lot of memory .please guide .

But as per my knowledge as DAO methods contains reference to connection object , so there would be a race around condition .

Please share your views on this .


Save India From Corruption - Anna Hazare.
Dieter Quickfend
Ranch Hand

Joined: Aug 06, 2010
Posts: 359
If it's a singleton, you have to worry about concurrency. Once concurrency is taken care of, you can load-test your application with singleton dao's and prototype dao's, and compare the results to know which is the right option for you. I highly doubt that a singleton will be the better approach. It will be difficult to take care of all aspects of concurrency where it would be no challenge with prototypes, and a singleton blocking at long-running queries will cause your users severe hair loss.


Oracle Certified Professional: Java SE 6 Programmer
Oracle Certified Expert: Java EE 6 Web Component Developer
Oracle Certified Expert: Java EE 6 Enterprise JavaBeans Developer
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Hi , Thanks for th reply

In what case we need to worry about the Concurency in which case

1.If the same user is doing multiple operarations with rspect to Database ??

2. Or from a Different User request the concurrency issues might arise ??

William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
Processing more than one request at a time would obviously cause trouble if you could only have one instance of a DAO class. All other requests would just have to wait - completely contrary to the idea of DAO objects.

The whole idea of DAO objects is to provide an instance which is independent of all others to conveniently hold a particular set of data.

See this rather complete wikipedia article.

Bill

Java Resources at www.wbrogden.com
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Processing more than one request at a time would obviously cause trouble if you could only have one instance of a DAO class.


I think i am close to my question now .
This would impact if there are multiple requests from a Single User ??

Or

Even a request from a different user ??

Please reply .
bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
Ravi Kiran Va wrote:
I think i am close to my question now .
This would impact if there are multiple requests from a Single User ??

Or

Even a request from a different user ??
Please reply .


It's the same whether request is from the same user or from a different user as long as your DAO is a singleton.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it Okay if a DAO class is made as Singleton
 
Similar Threads
Design question for creating a DAO using Hibernate
how to maintain state
Is it ok to make a DAO class as Singleton
Singleton Class static vs non-static methods