Spring JPA 存储库锁定混乱

Spring JPA repository locking confusion

我对 Spring JPA 存储库的锁定方式有些困惑,需要对此进行一些说明。

所以,我有一个 Spring 引导服务,我在其中使用休眠与数据库交互。 在其中一项服务中,我有一个这样的实体:-

    @Entity
    @Table(name="JUNIPER_SCH_CURRENT_JOB_DETAIL", schema="juniperx")
    public class JuniperSchCurrentJobDetail {

    private int currentJobSequence;

        ......

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    @Column(name="CURRENT_JOB_SEQUENCE")
    public int getCurrentJobSequence() {
    return currentJobSequence;
    }
    public void setCurrentJobSequence(int currentJobSequence) {
    this.currentJobSequence = currentJobSequence;
    }
    .......

现在我在此之上有一个存储库,标记为@Repository 并扩展了 JPARepository,如下所示:

   @Repository
   public interface JuniperSchCurrentJobDetailRepository extends 
   JpaRepository<JuniperSchCurrentJobDetail, String> {

   @Query
   public List<Object[]> findLastRunJobDetail(@Param("projectId") 
    String projectId);

所以令人困惑的是,我的一位同事告诉我不要为此 table 使用 @Repository 和 @Entity,因为所引用的 table 具有非常高的插入和更新率其他应用程序。使用 JPA 存储库将锁定此 table 并导致问题。

所以我的查询是:-

1) 在此微服务中,我仅在 table 上使用 select 查询,那么 select 查询是否也会锁定此 table?

2) 根据我的阅读,如果我使用乐观锁,它只会在提交事务时启用锁,但由于此微服务仅读取数据,它仍会影响其他服务,这些服务正在此 table?

3) 如果锁定部分为真,是否有任何方法可以停止锁定?

4) 我可以读取此微服务中的脏数据,因为它用于填充非实时的仪表板,所以可以继续使用 @repository 还是应该将其更改为 JDBC 模板避免锁定问题?

5) 使用@NoRepositoryBean 注释 class 并创建只读存储库会有帮助吗?

will the select query also lock this table?

this microservice is only reading the data will it still effect other services which are making inserts and updates in this table?

一些数据库在读取表时会在表上创建锁。

然而,这完全独立于 JPA 并且高度特定于数据库。 您需要查看您正在使用的特定数据库以了解它的行为方式。

写入通常会创建锁,至少对于您正在写入的行,而且对于通过外键引用的行也是如此。 某些数据库锁定的行数可能多于它们实际写入的行数。

同样,如果您使用 JPA、JDBC 或您选择的任何 SQL 工具,也是如此。

From what i read if i use optimistic lock it will enable a lock only when committing a transaction

不,乐观锁定不会创建除上述之外的任何其他(阻塞)锁。 但是,当在读取和写入行之间发生另一个更新时,它可能会使更新失败。 实际上,乐观锁定的目的是在不阻塞写入(和可能的读取)的情况下防止丢失更新。

总而言之,我看不出 JPA 在其默认配置中会如何生成比使用 JdbcTemplate 等更低级别的东西更多的锁。