如何锁定表...做一些事情...使用 Spring Boot 解锁表?

问题描述 投票:0回答:1

这个想法基本上是用自定义功能扩展some存储库。所以我得到了这个设置,它确实有效!

@MappedSuperclass
abstract class MyBaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Int = 0

    var eid: Int = 0

}

interface MyRepository<T : MyBaseEntity> {

    @Transactional
    fun saveInsert(entity: T): Optional<T>
}

open class MyRepositoryImpl<T : MyBaseEntity> : MyRepository<T> {

    @Autowired
    private lateinit var entityManager: EntityManager

    @Transactional
    override fun saveInsert(entity: T): Optional<T> {

        // lock table
        entityManager.createNativeQuery("LOCK TABLE myTable WRITE").executeUpdate()

        // get current max EID
        val result = entityManager.createNativeQuery("SELECT MAX(eid) FROM myTable LIMIT 1").singleResult as? Int ?: 0

        // set entities EID with incremented result
        entity.eid = result + 1

        // test if table is locked. sending manually 2-3 POST requests to REST
        Thread.sleep(5000)

        // save
        entityManager.persist(entity)

        // unlock
        entityManager.createNativeQuery("UNLOCK TABLES").executeUpdate()

        return Optional.of(entity)
    }
}

我该怎么做才能更像春天?

起初,我以为

@Transactional
可以完成锁定和解锁的任务。我尝试了几个额外的参数和
@Lock
。我确实阅读了文档和一些教程,但抽象的技术英语通常不容易理解。最后,我没有得到有效的解决方案,所以我手动添加了表锁定,效果很好。还是更喜欢一种更像弹簧的方式来做到这一点。

java spring spring-boot jpa spring-data-jpa
1个回答
0
投票

1) 您当前的设计也可能存在问题。

persist
不会立即在数据库中插入一行。当方法返回时,这会在事务提交时发生。

因此您在实际插入之前解锁表:

    // save
    entityManager.persist(entity) // -> There is no INSERT at this point.

    // unlock
    entityManager.createNativeQuery("UNLOCK TABLES").executeUpdate()

2) 回到如何仅使用 JPA 而不使用本机执行此操作(它仍然需要一些解决方法,因为默认情况下不支持它):

    // lock table by loading one existing entity and setting the LockModeType
    Entity lockedEntity = entityManager.find(Entity.class, 1, LockModeType.PESSIMISTIC_WRITE);

    // get current max EID, TRY NOT TO USE NATIVE QUERY HERE

    // set entities EID with incremented result

    // save
    entityManager.persist(entity)
    entityManager.flush() // -> Force an actual INSERT

    // unlock by passing the previous entity
    entityManager.lock(lockedEntity, LockModeType.NONE)
© www.soinside.com 2019 - 2024. All rights reserved.