Forums Register Login

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

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
Is there any sort of server-side solution?

Grae
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1845 times.
Similar Threads
Simple list table
JSP + struts html:select
Resetting UIViewRoot
How do I achieve this solution?
JSP + struts html:select
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:54:26.