• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Multiple rows inserted coz of page refresh

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
My application is three tiered and is based on the MVC architecture. I am facing a problem regarding insertion of multiple redundant rows in the database due to page refreshes. How do I identify if the request is the same as before or is a new request. Where and how should the check be done?
Regards,
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two options make sense:
1. On each of your forms that should not be repeated, include a hidden field with a unique token identifying that form. Keep, in the session, a Map of hidden fields to responses. If you get back a request with this token field identical to a previous one, just return the previous response, rather than repeating the action itself.
2. Alternatively, instead of forwarding to your response using RequestDispatcher, use a sendRedirect... that way, when a user refreshes, they will reload the URL for the response page, which can be idempotent, rather than the URL that triggers the action itself.
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a primary key on the table where you are inserting?
Thats the first thing you need to do maintain data integrity.
You will get an exception instead of duplicating rows, but atleast you won't be messing up the data.
 
Lakshmi Dasari
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chris,
Thanks for your reply.


Keep, in the session, a Map of hidden fields to responses.


My application does not use session. I did think of this option, but I have to work around the problem without the use of session.


Alternatively, instead of forwarding to your response using RequestDispatcher, use a sendRedirect.


I am forwarding the request using RequestDispatcher, as I have packaged my request object with attributes, which are needed by the next servlet/jsp in the chain. I tried using sendRedirect but it does not achieve the purpose. So any other alternatives would be highly appreciated.
Alok,


Have a primary key on the table where you are inserting.


I already have the primary key constraint, but the primary key field is generated whenever a new request arrives. The problem here is to identify if the request is new or the same as in the case of page refresh.. Any thoughts on this?? Thanks all for your interest and responses.
Regards,
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic