How to use r2dbc for blocking operations?

122 views
Skip to first unread message

abhishek shrivastava

unread,
Jul 15, 2022, 1:23:44 AM7/15/22
to r2dbc
Hi Team,
I have a requirement to build both blocking and non-blocking operations in my project.
I didn't want to use two different libraries to achieve this.

Can I use r2dbc for both blocking and non-blocking operations? How? I have to build all of the DB write operations as blocking, while the read operation should be non-blocking.
Please suggest.

Thanks,
Abhishek S.

Michael McMahon

unread,
Jul 15, 2022, 1:51:26 PM7/15/22
to r2dbc
Project Reactor would let you block for the completion of a reactive stream:

By doing this, you'll have the R2DBC driver executing tasks on a pool of background threads, while another thread is just blocked for completion. Compared to just using a synchronous API, this approach would be wasteful in terms of thread allocation. With a synchronous API, like JDBC, you'd have just one thread blocked, without having the additional background threads that execute the task to completion.

But thinking ahead, Project Loom will make threads very cheap in Java. There should be no concern about blocking a virtual thread, as virtual threads won't be a limited resource. With Loom, I think a model in which both synchronous and reactive styles are used could be reasonable. Those who prefer the reactive style of programming can consume a reactive stream as we do today: Using functional style operators of a reactive streams library (like Reactor). And those who prefer a synchronous style can block for the completion of a reactive stream, consuming the result as if it had been returned from a synchronous method call.
Reply all
Reply to author
Forward
0 new messages