How to use lock in R2DBC correctly?

609 views Asked by At

I want to locking the program for some races of database operations.

I would use a lock in jdbc, for example I want to query something :

 synchronized (mylock) {
   if(mydata != null) return;
   getDataFromDatabase();
}

But now for a reactor program, I think it's not good to use a lock, because it will block the thread.

How to deal such races?

I finally use a lock as following.

if(mydata != null) return Mono.just(mydata);
lock.lock();
if(mydata != null) return Mono.just(mydata);
return myRepository.findAll()
            .collect(xxx)
            .doFinally(signalType->lock.unlock());

0

There are 0 answers