Java 如何使任务等待另一个并发
我目前正在使用Java(spring boot)开发预约系统,我面临一个问题 当两(2)个用户几乎同时要求相同的时间时,我的验证不起作用 说明:Java 如何使任务等待另一个并发,java,spring-boot,Java,Spring Boot,我目前正在使用Java(spring boot)开发预约系统,我面临一个问题 当两(2)个用户几乎同时要求相同的时间时,我的验证不起作用 说明: New request recieved // Fetch all the previous request from the database and checks if there is any kind of conflicts Verification(); // If no conflict found saveAppointment(
New request recieved
// Fetch all the previous request from the database and checks if there is any kind of conflicts
Verification();
// If no conflict found
saveAppointment();
例如:
用户1在11:03请求预约:50
日期:2020年12月12日
开始时间:10:00
完:12时
用户1在11:03请求预约:51
日期:2020年12月12日
开始时间:10:00
完:12时
问题:
系统保存这两个文件而不发现任何问题,因为这两个文件同时发生,并且系统无法获取以前的任何约会
有没有一种方法让系统在有一个请求的时候等待,而在第一次完成时只需要另一个呢?
< P>我认为你应该考虑在你的应用程序或数据存储中使用某种锁。 SQL数据存储的经典方法(但也可以与NoSQL一起使用)是乐观锁定或悲观锁定。您可以查看这些方法的简要说明您可以找到使用Spring Boot和JPA实现乐观锁定的示例,以及-悲观锁定的示例。您可以使用synchronized关键字。只需阅读有关互斥锁的内容