简体   繁体   English

并发读取-使用JPA Hibernate通过不同的Java应用程序实例从同一表读取不同的行

[英]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中编写调度程序,它将执行以下操作。

  1. read the 2 rows from a table (database - oracle) 从表中读取2行(数据库-oracle)

  2. do some operation. 做一些手术。

  3. Then update one field value of the rows read in step 1. 然后更新在步骤1中读取的行的一个字段值。

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: 因此,让我们看一下您的要求:

  1. Each process instance should read two rows from some table 每个流程实例应从某个表读取两行
  2. A two-row-set worked on by one process should be off limits for any other process 由一个过程处理的两行设置应该超出任何其他过程的限制
  3. You don't want to wait for one process to finish the first two-row-set before the seconds process picks up its two rows 您不想等待一个过程完成第一个两行的设置,而后等待秒数的过程获取其两行

Now how to solve them? 现在如何解决它们?

  1. That's basic - you already have that, right? 这很基本-您已经拥有了,对吗?
  2. Could be done different ways, so let's first look at 可以用不同的方法完成,所以让我们先来看一下
  3. No blocking. 无阻塞。 That implies: Any of your processes being started has to be aware of the rows the other processes are working on 这意味着:您正在启动的任何进程都必须知道其他进程正在处理的行

So you need some kind of "reservation" on the two rows the process is going to handle. 因此,您需要在流程要处理的两行中进行某种“保留”。

I'd handle it this way: 我会这样处理:

  • add a lock column into your table as this marker of "I'm currently worked at" 在表中添加一个锁定列,作为“我目前在”工作的标记
  • modify your selection query to ignore already locked columns AND 修改您的选择查询以忽略已经锁定的列,并且
  • modify your selection query FOR UPDATE , preventing other processes from doing the same. 修改您的选择查询FOR UPDATE ,以防止其他进程执行相同的操作。 (Yes, that IS blocking others, but only while you're at it) (是的,这是在阻止其他人,但只有在您处于这种状态时)
  • Now update both rows to set the lock marker or your process 现在更新两行以设置锁定标记或您的过程
  • Do what you have to do 做你该做的事
  • when you're ready for your step 3 (update field), reset lock marker. 当您准备好执行第3步(更新字段)时,请重置锁定标记。

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.

相关问题 使用Hibernate将多个Java对象映射到同一表中的不同行 - Mapping multiple Java objects to different rows in same table with Hibernate 使用 hibernate 在不同的应用程序上更新同一个表 - Updating the same table on the different applications using hibernate 使用带有Hibernate的Spring数据jpa的具有相同标识符的不同对象 - A different object with the same identifier using Spring data jpa with Hibernate 如何避免两个不同的线程从 DB 读取相同的行(Hibernate 和 Oracle 10g) - How to avoid two different threads read the same rows from DB (Hibernate and Oracle 10g) Java ObjectInputStream从不同的应用程序读取 - Java ObjectInputStream reading from different Application Spring with JPA:将来自不同请求的对JPA实体的并发访问排队 - Spring with JPA: Queue concurrent access to JPA entity from different requests 休眠中同一表的不同表示 - Different representation of same table in hibernate 使用JPA2 / Hibernate进行并发写作和阅读 - Concurrent Writing & Reading with JPA2/Hibernate Java Hibernate JPA 创建一个实体并加入同一列引用的两个不同的表,具有相同的列名 - Java Hibernate JPA create an entity and Join two different tables referenced by the same column, with same column name Hibernate:在同一个应用程序中使用两个不同的DataBase模式 - Hibernate: Using two different DataBase schemas in the same application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM