How to enable LockModeType.PESSIMISTIC_WRITE when looking up entities with Spring Data JPA?

后端 未结 3 1867
难免孤独
难免孤独 2020-12-02 18:27

How can I achieve the equivalent of this code:

tx.begin();
Widget w = em.find(Widget.class, 1L, LockModeType.PESSIMISTIC_WRITE);
w.decrementBy(4);
em.flush()         


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-02 18:36

    If you are able to use Spring Data 1.6 or greater than ignore this answer and refer to Oliver's answer.

    The Spring Data pessimistic @Lock annotations only apply (as you pointed out) to queries. There are not annotations I know of which can affect an entire transaction. You can either create a findByOnePessimistic method which calls findByOne with a pessimistic lock or you can change findByOne to always obtain a pessimistic lock.

    If you wanted to implement your own solution you probably could. Under the hood the @Lock annotation is processed by LockModePopulatingMethodIntercceptor which does the following:

    TransactionSynchronizationManager.bindResource(method, lockMode == null ? NULL : lockMode);
    

    You could create some static lock manager which had a ThreadLocal member variable and then have an aspect wrapped around every method in every repository which called bindResource with the lock mode set in the ThreadLocal. This would allow you to set the lock mode on a per-thread basis. You could then create your own @MethodLockMode annotation which would wrap the method in an aspect which sets the thread-specific lock mode before running the method and clears it after running the method.

提交回复
热议问题