With all structures of this type, we can safely assume that the controller methods will be processed simultaneously (i.e., in several parallel threads). In addition, this is a serious performance hit to do it in any other way (yes, there are frameworks developed in a completely different way than at the initial stage, but we are not talking about them now).
Spring itself does not know that part of the account id of the URL somehow synchronizes it. If it synchronizes at the URL level, this will result in poor performance. You can always implement this logic yourself, for example, by synchronizing on the object with the scope of the session on which this controller method works (or making the entire controller control session as Lev answers ). This will then serialize calls only to this user (this HTTP session). You may have a global shared lock if you want to synchronize between all users, but this is a really bad idea for the reasons mentioned above.
Instead, create an application so that all controllers are completely stateless and make the methods either idempotent, if possible, or have the correct logic for checking preconditions. If the method is written to the database, you can implement an optimization strategy with optimization. Hibernate already has this feature, and some DB / drivers also have this, so see what applies to your case.
UPDATE
In the presented example, both streams save an instance (I assume that save means insert / update as needed), and the returned object is the result of the current save operation, so there is no strange behavior between the streams. This suggests that persistence is transactional with a reasonable level of transaction isolation (any other assumption would be quite unusual). Be that as it may, the second stream will simply overwrite the values ββrecorded by the first, but this is probably what you expect. However, you only read the identifier on the next line, and that apparently never changes, so in any case, the creation of the URL seems unaffected by concurrency.
source share