aspose file tools*
The moose likes JSF and the fly likes How can you syncronize two requests from the same user in JSF? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "How can you syncronize two requests from the same user in JSF?" Watch "How can you syncronize two requests from the same user in JSF?" New topic
Author

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

Grae Cullen
Greenhorn

Joined: Jan 21, 2010
Posts: 19
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

Joined: Oct 20, 2010
Posts: 82
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

Joined: Jan 21, 2010
Posts: 19
Is there any sort of server-side solution?

Grae
Thiago Alves de Moraes
Greenhorn

Joined: Dec 09, 2010
Posts: 8
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.

--
thiago moraes
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can you syncronize two requests from the same user in JSF?