File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can you syncronize two requests from the same user in JSF?

 
Grae Cullen
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Basically, my problem is that if the user click the one button, and does wait for the page to respond, and then clicks another button, it seems like there is a race condition.

How can I avoid the code for the first button press using the same variables as the code for the second button press?

I suspect that both requests are going to call the the same getters and setters in some random order, because each button click will be in a different phase of the JSF model.


Ok, here is an example, suppose the user clicks a button, the action function changes some variable. That variable is linked with a getter and a setter to an actuall jsf tag. Now suppose the action function is 50% complete. Now the user hits the same button. JSf will call the getters and the setters linked to that same variable, restoring the state or whatever. Basically, that variable is reset back to what it was before the action changed it.

Now the action function for the first click is continuing to run, and it changes that same variable again. Even with out the second action call, it is a race condition.
 
B Nirvan
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Grae Cullen wrote:Hi all,

Basically, my problem is that if the user click the one button, and does wait for the page to respond, and then clicks another button, it seems like there is a race condition.

How can I avoid the code for the first button press using the same variables as the code for the second button press?

I suspect that both requests are going to call the the same getters and setters in some random order, because each button click will be in a different phase of the JSF model.


Ok, here is an example, suppose the user clicks a button, the action function changes some variable. That variable is linked with a getter and a setter to an actuall jsf tag. Now suppose the action function is 50% complete. Now the user hits the same button. JSf will call the getters and the setters linked to that same variable, restoring the state or whatever. Basically, that variable is reset back to what it was before the action changed it.

Now the action function for the first click is continuing to run, and it changes that same variable again. Even with out the second action call, it is a race condition.


Disable the button after click using javascript

regards,
Nirvan
 
Grae Cullen
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any sort of server-side solution?

Grae
 
Thiago Alves de Moraes
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. You can use the synchronizer token (JEE Pattern).
Using this pattern you address the double submit problem in your application.

I don't think JSF has this feature out of the box.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 17616
39
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a quick explanation of Synchronizer Token:

http://www.javajunkies.org/index.pl?lastnode_id=3361&node_id=3355

And a more formal one:

http://www.corej2eepatterns.com/Design/PresoDesign.htm

The root of the problem is that browsers are multi-threaded and TCP/IP networking can be sequential within a request, but not between requests. Thus, you can send down 2 separate but identical requests, and the second request can end up being processed before - or at the same time as - the first request, thanks to the server's use of separate threads for each request.

At the core of the synchonizer token pattern, then, is the idea that a form gets one and only one shot at being processed, because it has a unique ID (token) and that token is only honored once.

This can lead to a moral dilemma (so to speak) if the form being submitted changed between the first and second requests, but for most cases, such as double-click on a button, the form will probably be identical anyway. You can further hedge your bets by attaching javascript code to the submit button that disables it on submit, although this can be a problem as well, if the submit fails and the button has been locked out from retrying.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic