fetchOne(), store(), and transactionResult() are all blocking calls. The first obvious step is to use the new transactionResultAsync() method, which looks like this:Mutator<User> mutator = u -> { ... };User updatedUser = DSL.using(configuration).transactionResult(transactionConfiguration -> {UserRecord userRecord = DSL.using(transactionConfiguration).selectFrom(USER).where(USER.USER_ID.equal(userId)).forUpdate().fetchOne();if (userRecord == null) {throw new ObjectNotFoundException("User(userId = %d) not found", userId);}else {User user = new User(userRecord);mutator.mutate(user);userRecord.store();return user;}});
CompletionStage<User> updatedUserStage= DSL.using(configuration).transactionResultAsync(transactionConfiguration -> {UserRecord userRecord = DSL.using(transactionConfiguration).selectFrom(USER).where(USER.USER_ID.equal(userId)).forUpdate().fetchOne();if (userRecord == null) {throw new ObjectNotFoundException("User(userId = %d) not found", userId);}else {User user = new User(userRecord);mutator.mutate(user);userRecord.store();return user;}});
CompletionStage<CompletionStage<User>> userCompletionStageCompletionStage = DSL.using(configuration).transactionResultAsync(transactionConfiguration ->DSL.using(transactionConfiguration).selectFrom(USER).where(USER.USER_ID.equal(userId)).forUpdate().fetchAsync().thenApply(result -> {if (result.isEmpty()) {throw new ObjectNotFoundException("User(userId = %d) not found", userId);}else {UserRecord userRecord = result.get(0);User user = new User(userRecord);mutator.mutate(user);userRecord.store();return user;}}));
CompletionStage<CompletionStage<User>> userCompletionStageCompletionStage = DSL.using(configuration).transactionResultAsync(transactionConfiguration -> {CompletionStage<Result<UserRecord>> selectCompletionStage = DSL.using(transactionConfiguration).selectFrom(USER).where(USER.USER_ID.equal(userId)).forUpdate().fetchAsync();Function<Result<UserRecord>, CompletionStage<User>> mutateAndPersistAsync = result -> {if (result.isEmpty()) {throw new ObjectNotFoundException("User(userId = %d) not found", userId);}else {return CompletableFuture.supplyAsync(() -> {UserRecord userRecord = result.get(0);User user = new User(userRecord);mutator.mutate(user);userRecord.store();return user;}, configuration.executorProvider().provide());}};return selectCompletionStage.thenCompose(mutateAndPersistAsync);});
Neither in the jOOQ 3.8 manual, nor anywhere online, can I find examples of how to use the new transactionAsync() and transactionResultAsync() methods.
This is where I'm uncertain on how to proceed. Was it the designers' intent that we would stop here? I'd like to continue with fetchAsync() but it starts to get hairy:CompletionStage<CompletionStage<User>> userCompletionStageCompletionStage = DSL.using(configuration).transactionResultAsync(transactionConfiguration ->DSL.using(transactionConfiguration).selectFrom(USER).where(USER.USER_ID.equal(userId)).forUpdate().fetchAsync().thenApply(result -> {if (result.isEmpty()) {throw new ObjectNotFoundException("User(userId = %d) not found", userId);}else {UserRecord userRecord = result.get(0);User user = new User(userRecord);mutator.mutate(user);userRecord.store();return user;}}));That just doesn't seem right.
If it wasn't for the fact that the asynchronous transactional scope "wraps" the we could compose the stages together and flatten the stages. This is, in fact, the approach we could take if we wanted to squash that last blocking call, store():CompletionStage<CompletionStage<User>> userCompletionStageCompletionStage = DSL.using(configuration).transactionResultAsync(transactionConfiguration -> {CompletionStage<Result<UserRecord>> selectCompletionStage = DSL.using(transactionConfiguration).selectFrom(USER).where(USER.USER_ID.equal(userId)).forUpdate().fetchAsync();Function<Result<UserRecord>, CompletionStage<User>> mutateAndPersistAsync = result -> {if (result.isEmpty()) {throw new ObjectNotFoundException("User(userId = %d) not found", userId);}else {return CompletableFuture.supplyAsync(() -> {UserRecord userRecord = result.get(0);User user = new User(userRecord);mutator.mutate(user);userRecord.store();return user;}, configuration.executorProvider().provide());}};return selectCompletionStage.thenCompose(mutateAndPersistAsync);});But that still leaves us with CompletionStage<CompletionStage<User>>. Am I missing something? Any and all feedback appreciated.
ctx.beginTransactionAsync().thenApply(... -> ...).thenApply(... -> ...).thenApply(... -> commit());
--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I also have a use case where I need to inject some additional SQL to be executed within the same transaction.
the code snippet you describe would sound ideal for us.ctx.beginTransactionAsync().thenApply(... -> ...).thenApply(... -> ...).thenApply(... -> commit());
Transaction transaction = ctx.beginTransaction();ctx.insert()...ctx.update()...
Savepoint savepoint = transaction.savepoint();
ctx.delete()...transaction.commit();
ctx.beginTransactionAsync().thenApply(transaction -> transaction.ctx().insert()).thenApply(transaction -> transaction.ctx().update()).thenApply(transaction -> transaction.savepoint())
.thenApply(transaction -> transaction.ctx().delete()).thenApply(transaction -> commit());
--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Thanks Lukas. That helps. Curious though, if the JDBC driver doesn't provide an async I/O API, what is the gain from adding async methods to the jOOQ API?
Clients can always wrap jOOQ usage in CompletableFuture.supplyAsync(() -> ...) themselves
and that would at least not perpetuate the lie.
Lukas
--
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--Thank you
Samir Faci
--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+unsubscribe@googlegroups.com.
Lukas
Lukas
--
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--Thank you
Samir Faci
--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
So far, this was just a very high level sketch. Let's assume we'd be going this way. There would be two new alternative transaction APIs: A blocking one and a non-blocking one. The blocking one might look just like JDBC or JTA:Transaction transaction = ctx.beginTransaction();ctx.insert()...ctx.update()...Savepoint savepoint = transaction.savepoint();ctx.delete()...transaction.commit();
--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+unsubscribe@googlegroups.com.
Are you looking for something particular?
ctx.beginTransactionAsync().thenApply(transaction -> transaction.ctx().insert()).thenApply(transaction -> transaction.ctx().update()).thenApply(transaction -> transaction.savepoint())
.thenApply(transaction -> transaction.ctx().delete()).thenApply(transaction -> commit());
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+unsubscribe@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+unsubscribe@googlegroups.com.