Please give advice to my very general question. I have the following usecase..
1. User does a search on customers and gets a list of search results 2. User clicks a hyperlink in search results which takes them to a View Customer details screen. 3. Now if user has edit privelege.. they click on a button in view screen (say view.jsp) and go to the edit screen (edit.jsp)
My ques. is this:
I'm using scope="request" everywhere so far.. and to get data for a customer on the view screen I'm going to the database and getting results back. Now when the user wants to go to the edit screen.. I don't want to hit the database again and get back the same details... so I'm thinking I will have to save the formbean in "session" from view action and use it in edit action.. Is that a correct approach ?
But then I have this doubt.. if I use the formbean in "session" for view and edit actions but I'm using the same formbean in "request" scope for my other screens like add and search.. will it work this way or I have to have it in scope="session" for all the screens ?
It is not a good idea to have an ActionForm bean defined in request scope for some actions and session scope for others. This will lead to a great deal of head scratching and strange bugs. If you put it in session scope for the edit, you should keep it in session scope consistently.
While I believe that putting the ActionForm in session scope is probably a good option, leaving it in request scope and retrieving the data again from the database isn't a bad option either. Remember that almost all databases have some sort of caching mechanism, so getting the same data from the database the second time is always going to be faster than getting it the first time. [ November 21, 2006: Message edited by: Merrill Higginson ]
My general philosophy is that simple database reads are extremely cheap. If you are reading a lot of data, executing queries in a loop or if you have a DBA that likes to implement every query with a view that calls stored procedures which call functions then maybe you should think about optimizing your database usage, but I don't worry about reading simple data from an indexed table.
Plus, you can look at your implementation as a feature. Reading the data fresh from the database cuts down on the window where multiple users might be editing the same piece of data. Anyways it seems likely that your editable page will have to hit the database to retrieve data for lists or other fields not needed on the read-only page.
Joined: Oct 19, 2005
Tks a lot for the reply.
Merrill, your answer confirmed my doubt of having a mix and match of scopes for the same formbean could cause problems.. so now I'd be using either only and most preferably "request" scope.
Brent, I agree with you..I'll go with the request scope. my edit pages as per now have exactly the same fields/data as on the view page.. regarding the list boxes/drop downs I'm caching them, so thats not a problem.