How to avoid race condition

java, jsp

Solution

A one-size-fits-all solution (e.g. `threadSafe=false`) causes requests to be executed one at a time. And that inevitably slows down request processing.

To that avoid that scenario, you need to understand why you are getting race conditions and (re-)design your architecture to avoid the problem. For example:

if the race condition is in updates to some shared in-memory data structures, you need to synchronize access and updates to the data structure at the appropriate level of granularity

if the race condition is in updates to the your database, you need to restructure your SQL to use transactions at the appropriate level of granularity.

These are just (possible) schemas for how to solve your race condition problems. In reality, you have to understand the root causes yourself.

Problem

What are the best ways to avoid race conditions in jsp at the same time not to slow down the process.I have tried isThreadSafe=false synchronized(session) however is there any other alternate solution is available?

Original source

Related problems