English 中文(简体)
How to LOCK TABLE ... dopinuff ... UNLOCK TABLE with Spring Boot?
原标题:How to LOCK TABLE ... do stuff ... UNLOCK TABLE with Spring Boot?

基本设想是延长 ~>>>。 具有习惯功能的储存库。 因此,我看着这个机构的工作!

@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)
    }
}

www.un.org/Depts/DGACM/index_spanish.htm 我怎样做这一更多的跳跃?

首先,我想到@transactional将会使LOCK和UNLOCK陷入困境。 我尝试了另外两个参数和<代码>@。 洛克希德/编码。 我的确通过了一些理论,但抽象的技术英语往往不易理解。 最后,我没有找到一个可行的解决办法,因此,我人工添加了一张桌子,该桌子的开工是罚款的。 不过,更喜欢采取类似春季的办法。

问题回答

<>1> 您目前的设计也可能存在问题。 <代码>persist并不即刻在INSERT a row in the database。 当方法回报时,交易就会出现这种情况。

因此,在实际插入之前,你将桌上锁在:

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

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

<>2> 回到如何只与没有原住民的《人民权利法》做事(这仍然需要工作范围,因为它没有得到违约的支持):

    // 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)




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...