使用JDBC和R2DBC连接在Jooq中的区别

huangapple go评论116阅读模式
英文:

Difference between using JDBC and R2DBC connection for Jooq

问题

以下是您要的翻译内容:

"The documentation for Reactive Fetching in Jooq mentions:

Out of the box, all jOOQ provided publishers will block on the underlying JDBC connection, but if you provide jOOQ with a io.r2dbc.spi.Connection or io.r2dbc.spi.ConnectionFactory, then the publishers will execute queries in a non-blocking fashion on an R2DBC driver.

I am trying to understand what is the difference between using the io.r2dbc.spi.Connection and java.sql.Connection when we are already wrapping the publisher in fromPublisher methods available in the rx-java.

For example (the code is identical for both R2DBC and JDBC, only the context ctx is constructed differently):

Flowable.fromPublisher(ctx.select(...).where(...)).subscribe(...);

If this code is being run on Thread 1, it shouldn't immediately block that thread in either case (because it's a Flowable created from the publisher provided by jooq).

But then, in the JDBC case, will it later block the same Thread 1, or some other thread?

My current understanding, based on https://github.com/r2dbc/r2dbc-spi/issues/162#issuecomment-598908146 is that R2DBC reduces the number of threads required as compared to JDBC for an equivalent number of simultaneous DB connections.

Is this understanding correct and complete?

For more context, we're using Vertx + jooq + R2DBC and are trying to evaluate whether it's possible to write a scalable system using JDBC instead of R2DBC."

请注意,中文翻译中不包括代码部分。

英文:

The documentation for Reactive Fetching in Jooq mentions:

> Out of the box, all jOOQ provided publishers will block on the underlying JDBC connection, but if you provide jOOQ with a io.r2dbc.spi.Connection or io.r2dbc.spi.ConnectionFactory, then the publishers will execute queries in a non-blocking fashion on an R2DBC driver.

I am trying to understand what is the difference between using the io.r2dbc.spi.Connection and java.sql.Connection when we are already wrapping the publisher in fromPublisher methods available in the rx-java.

For example (the code is identical for both R2DBC and JDBC, only the context ctx is constructed differently):

Flowable.fromPublisher(ctx.select(...).where(...)).subscribe(...);

If this code is being run on Thread 1, it shouldn't immediately block that thread in either case (because it's a Flowable created from the publisher provided by jooq).

But then, in the JDBC case, will it later block the same Thread 1, or some other thread?

My current understanding, based on https://github.com/r2dbc/r2dbc-spi/issues/162#issuecomment-598908146 is that R2DBC reduces the number of threads required as compared to JDBC for an equivalent number of simultaneous DB connections.

Is this understanding correct and complete?

For more context, we're using Vertx + jooq + R2DBC and are trying to evaluate whether it's possible to write a scalable system using JDBC instead of R2DBC.

答案1

得分: 1

这不是jOOQ特定的,尽管jOOQ可以帮助您轻松切换JDBC和R2DBC驱动程序,而无需更改客户端代码。从逻辑上讲,切换确实不应该产生太多影响,除非总会出现边缘情况和错误,特别是在R2DBC生态系统中,它尚未像JDBC那样成熟,还有jOOQ本身,因为编写正确的响应式(或任何并发)代码很困难。

然而,R2DBC相对于JDBC的主要价值在于它允许可能具有响应式能力的传输协议直接从数据库内部向客户端推送数据,而使用JDBC时,客户端将始终在某个地方阻塞。

这是否实际对您的应用程序产生任何明显影响,最好由您在实际场景中自行测量,之后了解响应式API本身的感觉(它们本身会带来一些认知障碍)。 jOOQ设计的另一个好处是,一旦使用jOOQ的Publisher API,您可以在生产环境(或集成测试、基准测试等)中轻松切换驱动程序。我认为在这个领域没有什么是经过证明的或甚至是百分之百可靠的。

从长远来看,可扩展性问题应该由Loom以更透明的方式解决,包括JDBC在内的所有实现。我在这里的期望是,随着Loom的出现,响应式编程模型将主要关注编程模型(推送而不是拉取),而不是关于扩展。

英文:

This isn't really jOOQ specific, although jOOQ helps you easily switch between JDBC and R2DBC drivers, without changing your client code. Logically, the switch should indeed not have too many effects, apart from the fact that there are always edge cases and bugs, especially in the R2DBC ecosystem, which isn't as mature as JDBC yet, and also jOOQ itself, given how hard it is to write correct reactive (or any concurrent) code.

The main value of R2DBC over JDBC, however, is that it allows for possibly reactive-capable wire protocols to push data to clients directly from within the database, whereas with JDBC, the client will always block somewhere.

Whether this actually has any tangible effects on your application is best measured by yourself in a real world scenario, after getting a feel of the reactive APIs themselves (which impose their own cognitive hurdles). Another benefit of jOOQ's design is that you can simply do this on a production (or integration test, benchmark, etc) workload, switching drivers, once you use jOOQ's Publisher API. I don't think there's anything proven or even fool proof in this area.

In the long run, scalability problems should be addressed by Loom in a more transparent way, for all implementations, including JDBC. My expectation here is that with the advent of Loom, the reactive programming model will go back to being mainly about the programming model (push instead of pull), rather than about scaling.

huangapple
  • 本文由 发表于 2023年2月27日 18:20:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定