aspose file tools*
The moose likes JDBC and the fly likes Caching of Database Access Objects's possible? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Caching of Database Access Objects Watch "Caching of Database Access Objects New topic
Author

Caching of Database Access Objects's possible?

Palak Shah
Greenhorn

Joined: Jan 21, 2005
Posts: 18
I am working on a web application. I am using Struts for the presentation layer. Also I am using DAO pattern for Database layer.

My doubt is about creating instance of DAO's. I am created a DAO Factory which can create instances of DAO's. Whether I can go for caching of DAO or Do I have to create a new DAO for everytime? Would caching done as shown in the code below create Concurrency issues? or please suggest the right way of going about it.

My Struts Action code is as follows:



UserDAO code is as follows:



And DAO Factory is as follows:


[ April 19, 2008: Message edited by: Palak Shah ]
[ April 19, 2008: Message edited by: Palak Shah ]

Palak Shah,<br />SCJP (85%), SCWCD (84%), SCBCD (95%).
Amit M Tank
Ranch Hand

Joined: Mar 28, 2004
Posts: 257
Why do you want to cache the DAO Objects? Your implementaion will work fine.

But your implementation of the DAO factory and Caching will not help solve the problem of concurrency. You have to handle concurrency in a different way.


Amit Tank
Linked In
Palak Shah
Greenhorn

Joined: Jan 21, 2005
Posts: 18
I would like to cache so that it does not have to create new DAO for each database call. Any idea how to solve problem of concurrency? or it is better to create new DAO every time?
Amit M Tank
Ranch Hand

Joined: Mar 28, 2004
Posts: 257
Caching DAO will only help you with saving the overhead of creating new Objects everytime and they being garbage collected.

If you want to handle concurrency you could do it in 2 ways.

1. Passimistic Locking - I would not recommend this method as it has lot of performance overhead.

2. Table Row Version Checking(Optimistic Concurrency Control) - Introduce a Version_number or TimeStamp Coulumn in your table. Get the version number before calling update. When updating compare the version number.

Hope this clears your doubt.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
You should not access a DAO from the Controller, i.e. Action class. Action classes should not contain business logic, but should only serve as a mediator between the View and the Model.

Caching of data access objects in the Business tier is possible. Caching of data access objects in Presentation tier/Web server is poor design and should not be implemented.

You can cache DAO instances with a serialized POJO.

Read up on Model-View-Controller design pattern. Struts is a framework for View and Controller parts only. Again for emphasis, no business logic in Action instances )
Palak Shah
Greenhorn

Joined: Jan 21, 2005
Posts: 18
I agree James... I need to add the business layer.. However the doubt is about the multiple thread accessing the same method of DAO. Since I have only one instance of DAO, would multiple simultaneous calls to same method of a DAO (for different users) create a problem?

Please see my code above and let me know if it'll work!
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
>..doubt is about the multiple thread accessing the same method of DAO. Since I have only one instance of DAO.

Each thread should have its own set of data access objects. The issue above will create a potential bottleneck if you attempt to force the threads to use only one instance of DAO. This is not recommended.
[ April 22, 2008: Message edited by: James Clark ]
Amit M Tank
Ranch Hand

Joined: Mar 28, 2004
Posts: 257
Multiple Threads will call the same DAO object, putting syncronized in the method will put a bottleneck.

In an environment where you deploy this code in multiple server(like a clustered env) even syncronization will not guaranty avoiding the concurrency issue.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Caching of Database Access Objects's possible?
 
Similar Threads
java.sql.Connection object, can it be a class var of a DAO?
Display struts output to jsp
how do you display the struts forward to jsp
Display struts output to jsp
Help me see if possible Generic DAO Factory