简体   繁体   中英

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

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 using the same variables as the code for the second button?

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.

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 the variable again. Even with out the second action call, it is a race condition.

Well on the client side you may want to look at JavaScript to disable multiple submissions eg disable submit input type after it's clicked. This isn't a foolproof mechanism by any means though.

In JSF itself it depends upon the scope of the Bean. If it's session scoped you could synchronize access to its action method (ie invoke application method) so only one thread at a time can perform the logic. Likewise you could synchronize access to the shared data. This will only work if you have session affinity though. If you don't then you're going to struggle!

However, the component tree itself (update values, process validations, etc.) will all still be invoked for the second request. Maybe you could look at a PhaseListener or something to prevent this, but this seems tricky and maybe an overkill.

Generally the way your application is coded and built means that you should be able to handle multiple requests by the same user. Although personally I go for the JavaScript approach to prevent multiple form submissions. Do you want to explain in some more detail, maybe with code snippets, what problem you perceive?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM