R2DBC – 它在现实中是如何工作的?

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

R2DBC - how it works in reality?

问题

我开始学习响应式编程范式,所以对这个主题还很陌生。
我创建了一个示例应用,其中使用Spring R2DBC来连接PostgreSQL数据库。

我创建了一个Rest Controller,用于从数据库中获取和保存数据,以更好地理解这个概念。

我预期会遇到以下场景:

  1. 我从响应式Repository类中运行findById查询,查找尚不存在于数据库中的id。我对返回的Mono对象进行订阅。
  2. 我使用在步骤(1)中查询的id保存实体。
  3. 来自步骤(1)的查询在数据库中不断监听变化,并立即找到新增的实体。

不幸的是,事情并没有按照这样的方式进行。基本上,步骤(3)从未发生过,我需要手动从(1)中触发查询,以再次命中数据库。

我的问题是 - 我是否做错了什么,或者对响应式数据库的概念理解是否有误?

我感觉在这里需要一些解释。非常感谢您对这个问题的帮助。

英文:

I've started to learn Reactive paradigm, so I'm pretty new in this topic.
I created a sample app which uses Spring R2DBC to connect to PostgreSQL DB.

I created a Rest Controller for fetching and saving data in DB to better understand a concept.

I expected to meet the following scenario:

  1. I'm running findById query from reactive repository class looking for id, which does not exist in DB yet. I'm subscribing on the returned Mono object
  2. I'm saving the entity with id queried in (1)
  3. Query from (1) is constantly listening for changes in DB and finds added entity immediately

Unfortunately it does not work in such way. Basically point no (3) is never happening and I need to fire query from (1) manually to hit the DB again.

My question is - am I doing something wrong or understanding reactive database concept incorrectly?

I feel, needing some explanation here. I really appreciate your help at this issue.

答案1

得分: 1

一般来说,我认为HTTP连接和数据库连接会在执行完成后释放连接。

但是对于你在这里列出的情况,在Spring Reactive堆栈中实现并不难。

我已经创建了一些使用可追踪的Mongo文档的示例。

对于R2dbc,你可以使用SinksProcessors来回放有效负载(在保存时)并将其发送到客户端,查看此分支

你还可以利用Postgres通知来实现类似的功能,参考这个问题

英文:

Genrally,I think http connection and database connection will release the connection after execution is done.

But for your cases list here, it is not diffcult to implement in Spring Reactive stack.

I have created some samples using a tailable Mongo document.

For R2dbc, you can use Sinks or Processors to replay the payload (when it is saved) and sent to the client, check this branch.

And you can also ultilze Postgres Notificaiton to implement similar featuers, check this question.

huangapple
  • 本文由 发表于 2020年9月28日 05:07:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64093335.html
匿名

发表评论

匿名网友

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

确定