This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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 ]
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
Joined: Mar 28, 2004
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.
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 )
Joined: Jan 21, 2005
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!
Joined: Apr 16, 2008
>..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
Joined: Mar 28, 2004
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.