2021年3月2日星期二

How to perform reactive database operations inside a spring transaction when using Oracle Database?

Currently my spring transaction without asynchronous DB calls looks something like this:

@Transactional(value = "TxnManager_xyz", rollbackFor = Exception.class)  public Mono<Void> doSomething() {      db.query1(); // Blocking operation      db.query2(); // Blocking operation      return Mono.empty();  }    

Here db is a class that uses jdbc to perform the query. query1 and query2 are independent of each other and so can be run in parallel. So I tried to convert the above to this:

@Transactional(value = "TxnManager_xyz", rollbackFor = Exception.class)  public Mono<Void> doSomething() {      Mono<Integer> mono1 = Mono.fromCallable(() -> db.query1());      Mono<Integer> mono2 = Mono.fromCallable(() -> db.query2());      return Mono.zip(mono1, mono2).then();  }    

The problem with the above code is that once the DB query executes, for some reason spring rollsback the operation.

So how do I perform these two queries in parallel and in a transactional context?

I read that @Transactional stores some transaction related state in ThreadLocal. I guess Mono.zip causes the DB operation to be performed in a different thread hence causing some problem with the transaction.

I read that there was support for reactive transactions with reactive database driver but as far as I know there does not exist a reactive database driver for Oracle database (please correct me if I'm wrong).

The other alternative I can think of is to write my own transaction manager. But I don't know how to do that. Any resources would be helpful.

https://stackoverflow.com/questions/66450979/how-to-perform-reactive-database-operations-inside-a-spring-transaction-when-usi March 03, 2021 at 12:50PM

没有评论:

发表评论