[英]Concurrent Read - Reading different rows from same table by different java application instance in using jpa hibernate
I am writing a scheduler in spring boot, which will do the following action. 我在Spring Boot中编写调度程序,它将执行以下操作。
When I will run my application as two separate instances, 2 schedulers will run. 当我将应用程序作为两个单独的实例运行时,将运行2个调度程序。
Which will make all the above steps repeated in 2 times. 这将使以上所有步骤重复执行2次。
My goal is if the schedule of the 1st instance is reading 2 rows at that time schedule of the 2nd instance to read next 2 rows, instead of the same row. 我的目标是,如果第一个实例的时间表正在读取第二行的时间表,那么第二个实例的时间表将读取接下来的两行,而不是同一行。
Please suggest any way to do this using JPA or in Java. 请提出使用JPA或Java的任何方法。
So let's have a look at your requirements: 因此,让我们看一下您的要求:
Now how to solve them? 现在如何解决它们?
So you need some kind of "reservation" on the two rows the process is going to handle. 因此,您需要在流程要处理的两行中进行某种“保留”。
I'd handle it this way: 我会这样处理:
FOR UPDATE
, preventing other processes from doing the same. 修改您的选择查询FOR UPDATE
,以防止其他进程执行相同的操作。 (Yes, that IS blocking others, but only while you're at it) (是的,这是在阻止其他人,但只有在您处于这种状态时) You might want to take extra measures in terms of one process dying after setting lock-marker but before releasing them which could eventually lead to rows never processed - maybe us a timestamp value in lock column and select for lock based on reasonable time delta (process runs every 10 minutes - eveeything older than 30min is probably crashed) 您可能想要在设置锁定标记之后但在释放它们之前可能导致一个进程死亡的过程中采取额外措施,这最终可能导致未处理的行-也许我们在lock列中添加了一个timestamp值,并根据合理的时间增量选择了lock(进程每10分钟运行一次-超过30分钟的所有东西都可能崩溃了)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.