![]() This saves an extra query on insert or updates, because Spring Data is then using the version field to determine if it is a new entity to be saved (version is null), hence an insert is necessary or if it is an existing one to update. TIP: If you have an application generated key, e.g., like the UUID or any other business key from the example above, I recommend using a nullable type for the version field (Wrapper type Long instead of primitive long in java). To make an entity ready for optimistic locking, you need to add a version attribute to your database table and entity mapping like this: class = "uuid-char") var id: UUID, The code is written in kotlin and available on GitHub. To understand why, let's first take a look at an example using the attribute as proposed. But typically this is not working for the concurrent long conversations problem. Normally the proposed solution is to just add an attribute to your entity. Without optimistic locking, the user who saves last, overwrites changes from the previous users without knowing that the data was changed by others in the meantime. Usually you find articles only handling the problem of concurrent database transactions: Two or more transactions load the same state of an entity, then modify and save it to the database again, leading to the fact that the last transaction which is committed overwrites the changes from the previous ones.īut there is a second problem, very well described in article : concurrent long conversations: Typically two or more users are loading the same state of an entity to their local UI, then modify some fields and send their state back to the server. Some examples include: Optimistic Locking in JPA, Optimistic locking with JPA and Hibernate, Testing Optimistic Locking Handling with Spring Boot and JPA. There are several articles out dealing with this topic. To be more precise: It's not about preventing concurrent changes (this would be pessimistic locking), but to allow and detect those changes happening at the same time and avoid concurrent modification. Optimistic locking is a concept to avoid concurrent changes on the same data.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |