Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how can I stop the submit being processed twice?

 
James Byars
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • 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
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • 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"
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read this article and pay particular attention to the PRG pattern.
 
Charbel Keyrouz
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can simply disable the button submit after the first click.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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
  • 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: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Soniya Ahuja
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • 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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic