• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Caching of Database Access Objects's possible?

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Palak Shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>..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
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 don't always make ads but when I do they're tiny
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic