• 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

how can I stop the submit being processed twice?

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

I have an application
user clicks submit
data sent to database
user redirected to another page.

The problem is, it takes about 5 seconds from the time the user clicks submit to the time they're redirected to the other page. And in this 5 seconds, users sometimes double click on Submit, so the data is submitted twice! I want my servlet to only accept one submit within say 5 seconds in a Session. How can I do this?

thank you
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to design your transactions to be idempotent.
http://en.wikipedia.org/wiki/Idempotence

The short answer is that you can not stop it, so you have to handle it in your design.
 
James Byars
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks, I was hoping more along the lines of someone telling me that there is some design pattern or method already available that I can make use of
 
Pat Farrell
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Idempotency is the design pattern. For some actions, its trivial. You simply return the same result as you would have returned the first time it was executed. So if the command is "delete record for James Byars" you return "all records for James Byars deleted"

If it was "create James Byars record" then you have problems. So change the definition to "make sure that there is a James Byars record" and then its easy.

The hard one is "add one to James Byars's balance"
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read this article and pay particular attention to the PRG pattern.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can simply disable the button submit after the first click.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doing something in the client is not a guarantee. Have a read of the article Bear linked; it’s a far more thorough solution.
 
James Byars
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot guys, I will definitely look into that article later on when I develop larger web applications, for the time being I will just use some client side disabling I guess
 
Pat Farrell
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Byars wrote: I will just use some client side disabling I guess


Seriously, don't do that. It breaks the user's expectations and does not solve your problem.

 
James Byars
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pat Farrell wrote:

James Byars wrote: I will just use some client side disabling I guess


Seriously, don't do that. It breaks the user's expectations and does not solve your problem.



ok, I won't! I'll find the time to read up on the earlier link and work through it.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pat Farrell wrote:

James Byars wrote: I will just use some client side disabling I guess


Seriously, don't do that. It breaks the user's expectations and does not solve your problem.


Quoted for truth. Solutions that don't solve the problem aren't solutions at all.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was wondering if you could simply display a message saying that the page is being loaded please wait... or something of this sort.

When I go internet shopping, I see such messages being displayed at many payment gateways. They ask the user to submit only once as clicking the button twice would result in duplicate transactions.

One more suggestion - not sure if it would be a good idea, but I think that instead of disabling the submit button, you can add a Load Mask on the entire page while the redirect takes place. Thus the user won't be confused and would wait patiently for the redirect. Just an idea...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic