You cannot implement optimistic locking this way on the server. Take a look at the following picture:
Two users call the microflow at roughly the same time. The ‘ver’ value in the database is 1, the ‘ver’ value of the objects is 1 as well. The first execution retrieves the object from the database, sees that the versions match and commits The version is now 2. The second execution, however, also retrieves the object from the database, sees that the versions match and commits. However, it is this scenario that should be prevented!
The timing window in which this can happen is very small, but it does exist. If you really need to lock, you either need to do it in the database, or you need to implement locking in the microflows, so that the saving can only be executed sequentially. Furthermore, you need to use the Java action EndTransaction, or even then it’s possible to hit a timing window where the information is not yet in the database before the second execution starts! Older versions of CommunityCommons have code to implement locking, but note that this locking does not work once you start horizontally scaling your application.