admin管理员组文章数量:1023838
I am using jpa in my spring boot application (postgres) and have some entity called car. In the beginning cars are created without assignedUser field in database.
Then trough certain process (post request) users can assign cars to themselves, where they will get the first car that has assignedUser field null and more recent creationTimestamp and status not DELETED. The method finds the first car in these conditions and assigns it to the user that made the request. At this point field assignedUser is populated.
In order to prevent concurrent updates and make sure that if many users try to request cars at the same time (they should all get different cars) I have implemented a optimistic locking mechanism
@Entity
public class Car{
@Id
private Long id;
private String status;
private String name;
private double price;
private String assignedUser;
private Instant creationTimestamp;
@Version
private int version; // Version field for optimistic locking
// Getters and setters
}
The ideia here is if I get an OptimisticLockException it means the record was updated in between by another process and I will try the assign operation again.
My problem is that at any point in time users can execute a clear operation, where all cars that have assignedUser null are updated to status DELETED. I might have thousands of cars in this scenario so I will update in batches (I cannot use optimistic locking here)
How can I prevent following scenarios when clear operation and assign requests happen at the same time?
- clear operation from modifing a car that was just assigned to a user
- assign operation from assigning a car that was just cleared (status = DELETED) ?
I am using jpa in my spring boot application (postgres) and have some entity called car. In the beginning cars are created without assignedUser field in database.
Then trough certain process (post request) users can assign cars to themselves, where they will get the first car that has assignedUser field null and more recent creationTimestamp and status not DELETED. The method finds the first car in these conditions and assigns it to the user that made the request. At this point field assignedUser is populated.
In order to prevent concurrent updates and make sure that if many users try to request cars at the same time (they should all get different cars) I have implemented a optimistic locking mechanism
@Entity
public class Car{
@Id
private Long id;
private String status;
private String name;
private double price;
private String assignedUser;
private Instant creationTimestamp;
@Version
private int version; // Version field for optimistic locking
// Getters and setters
}
The ideia here is if I get an OptimisticLockException it means the record was updated in between by another process and I will try the assign operation again.
My problem is that at any point in time users can execute a clear operation, where all cars that have assignedUser null are updated to status DELETED. I might have thousands of cars in this scenario so I will update in batches (I cannot use optimistic locking here)
How can I prevent following scenarios when clear operation and assign requests happen at the same time?
- clear operation from modifing a car that was just assigned to a user
- assign operation from assigning a car that was just cleared (status = DELETED) ?
本文标签: postgresqlconcurrency in spring jpa with postgresStack Overflow
版权声明:本文标题:postgresql - concurrency in spring jpa with postgres - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745591500a2157915.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论