Spring data and lock

Is it possible to have two methods in the spring -data repository - one without locking T findOne(Predicate p); at the same time, but with the lock @Lock(LockModeType.PESSIMISTIC_WRITE) T findOne(Predicate p); ?

I would like something like

 public interface TransactionRepository extends JpaRepository<Transaction, String>, QueryDslPredicateExecutor<Transaction> { @Lock(LockModeType.PESSIMISTIC_WRITE) @AliasFor("findOne") Transaction findOne_withLock(Predicate p); } 
+6
source share
1 answer

No its impossible

Java does not allow you to define two functions with the same prototype. U may instead have a selector that calls functions based on some environment variable

  public interface TransactionRepository extends JpaRepository<Transaction, String>, QueryDslPredicateExecutor<Transaction> { @Lock(LockModeType.PESSIMISTIC_WRITE) Transaction findOne_withLock(Predicate p); default Transaction findOne(Predicate p) { if (EvironmentCheck) { return findOne_withLock(p); } return findOne(p); } } 
0
source

Source: https://habr.com/ru/post/989623/


All Articles