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 ]
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.
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!
>..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 ]