ReactiveCrudRepository 在春季使用 Hibernate [英] ReactiveCrudRepository to use Hibernate in spring

查看:26
本文介绍了ReactiveCrudRepository 在春季使用 Hibernate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过 ReactiveCrudRepository 而不是 CrudRepository 使用 Hibernate 和 RDBMS(Mysql、Postgres 等)?我用 Spring Data Jpa 和 Hibernate 尝试了一些示例,但无法完成.我只能在 ReactiveCrudRepository 上为 MongoDB 和 cassandra 找到一些示例.

解决方案

是否可以将 Hibernate 和 Mysql 与 ReactiveCrudRepository 一起使用而不是 CrudRepository?

TL;DR:

不是使用 Hibernate 和 MySQL,而是使用 R2DBC 和 Postgres、Microsoft SQL Server 或 H2.看看 Spring Data R2DBC.

加长版

为什么不用 JPA?

在包含 Hibernate/JPA 的情况下,这在可预见的将来不会发生.JPA 基于您将部分数据模型加载到内存中、操作生成的对象模型并让 JPA 转换这些更改的想法.所有这一切都在一次交易中完成.

这与处理响应式存储的方式相反,您尝试进行原子更改并尝试将加载、处理和存储以及所有这一切解耦而不会阻塞.

为什么不用 JDBC?

所以我们要看看JPA下面的技术层次:JDBC.但是 JDBC 仍然阻塞:您向数据库发送一条 SQL 语句,然后 JDBC 将阻塞,直到您得到结果.这再次违背了反应式的想法:永远不要阻塞.可以将其包装在线程池中以在一定程度上缓解这种情况,但这更像是一种变通方法而不是解决方案.

为什么选择 R2DBC?

对于一些可用于反应式存储库的数据库,有一些合适的驱动程序.但它们是专有的,因此不是真正应该最终在所有(相关)关系数据库中工作的东西的良好基础.

一段时间以来,Spring Data 团队希望 ADBA 将填补这一空白.但是邮件列表上的讨论清楚地表明,ADBA 的目标不是响应式的,而是异步的.同样不是我们需要的响应式存储库抽象.

所以在 2018 年初,生活在十字路口或反应式和关系式的各种人决定我们需要一个反应式数据库访问标准.

R2DBC(R主动R情绪化D数据库C连接性)是对这样一个标准的提议.希望它有助于说服 Oracle 将 ADBA 转移到被动方法,或者如果这种情况没有发生,它本身就会成为标准.

并且已经有了三个可用的实现,第二个选项看起来很有希望.

R2DBC 本身主要是一个 SPI,即由数据库提供者实现的 API.SPI 的设计方式对实施者的要求最低.但这也让 R2DBC 使用起来有些麻烦.我们的想法是,其他库将在 SPI 之上加紧构建为可用性而设计的库,就像 JDBC 发生的那样.

Spring Data R2DBC

Spring Data R2DBC 就是这样一个库,它提供了您所要求的:支持 ReactiveCrudRepository 尽管它独立于 JPA/Hibernate 并且还不支持 MySQL.

项目状态

R2DBC 和 Spring Data R2DBC 都还没有生产版本,至少需要几个月的时间.

Spring Data R2DBC 刚刚发布了第一个里程碑.有关其当前功能,请参阅发布文章.

R2DBC 正处于其第 6 个里程碑.详见发布文章.p>

另请参阅此答案:为什么 Spring 不为关系数据库提供响应式(非阻塞)客户端?

原答案供考古学家参考:

截至目前(2017 年 1 月),这是不可能的.

Spring Data 的响应式部分的当前相关版本是 Spring Data Kay M1 (你可以查看是否有更新版本可用在项目主页上)

还有一篇来自 Spring Data 团队的关于该版本的博客文章,特别是其中的反应部分(重点是我的):

<块引用>

Spring Data Kay M1 是第一个支持响应式数据访问的版本.它最初的支持的存储集——MongoDB、Apache Cassandra 和 Redis——都已经提供了响应式驱动程序,这使得它们成为此类原型的非常自然的候选者.

原因是没有标准的非阻塞方式来访问关系数据库.所以现在只支持那些支持这种 API 的.

可以使用 JPA 或 JDBC 实现 ReactiveCrudRepository 并将工作委托给线程池.这将在外部提供异步 API,但仍会消耗线程资源并在独立数据访问之间阻塞,因此只能实现反应式方法的一小部分好处.

Is it possible to use Hibernate and RDBMS(Mysql, Postgres etc) with ReactiveCrudRepository instead of CrudRepository? I have tried some samples with Spring Data Jpa and Hibernate, but couldn't get it done. I was only able to find a few samples on ReactiveCrudRepository for MongoDB and cassandra.

解决方案

Is it possible to use Hibernate and Mysql with ReactiveCrudRepository instead of CrudRepository?

TL;DR:

Not with Hibernate and MySQL, but with R2DBC and Postgres, Microsoft SQL Server or H2. Take a look at Spring Data R2DBC.

Long Version

Why not JPA?

With Hibernate/JPA included this won't happen in the foreseeable future. JPA is based on the idea that you load part of your data model into memory, manipulate the resulting object model and let JPA transform these changes. All this within a single transaction.

This is kind of the opposite how one deals with a reactive store where you try to make atomic changes and try to decouple the loading, processing and storing and all this without blocking.

Why not JDBC?

So we have to look at the technology level below JPA: JDBC. But JDBC is still blocking: You send a SQL statement to your database and then JDBC will block until you get the result. And again this goes against the idea of reactive: Never block. One could wrap this in a thread pool to mitigate this to some extent, but that is more of a workaround than a solution.

Why R2DBC?

There are some suitable drivers for some databases that could be used for reactive repositories. But they are proprietary and thereby not a good basis for something that really should eventually work across all (relevant) relational databases.

For some time the Spring Data team hoped that ADBA would fill that gap. But discussions on the mailing list made it clear that ADBA was not aiming for reactive but only for asynchronous. Again not what we needed for a reactive repository abstraction.

So early in 2018 various people living at the intersection or reactive and relational decided that we need a standard for reactive database access.

R2DBC (Reactive Relational Database Connectivity) is a proposal for such a standard. The hope is that it either helps convincing Oracle to move ADBA to a reactive approach or if that doesn't happen it becomes the standard itself.

And with already three implementations available chances for the second option look promising.

R2DBC itself is mainly an SPI, i.e. an API that is to be implemented by database providers. The SPI is designed in a way that puts minimal requirements on implementers. But this also makes R2DBC somewhat cumbersome to use. The idea is that other libraries will step up and build libraries designed for usability on top of that SPI, as it happened with JDBC.

Spring Data R2DBC

Spring Data R2DBC is one such library and it offers what you asked for: Support for ReactiveCrudRepository although it is independent of JPA/Hibernate and there is no support for MySQL yet.

State of the projects

Both R2DBC and Spring Data R2DBC didn't have a production release yet and it will take at least several months to get there.

Spring Data R2DBC just released the first milestone. See the release article for its current capabilities.

R2DBC is on its 6th milestone. See the release article for details.

See also this answer: Why does Spring not provide reactive (non-blocking) clients for relational databases?

Original answer as a reference for archeologists:

As of now (Jan 2017) it is not possible.

The currently relevant release for the reactive part of Spring Data is Spring Data Kay M1 (You can check if there is a newer version available on the project home page)

And a blog post from the Spring Data team about that release and specifically the reactive parts in it starts with (emphasis mine):

Spring Data Kay M1 is the first release ever that comes with support for reactive data access. Its initial set of supported stores — MongoDB, Apache Cassandra, and Redis — all ship reactive drivers already, which made them very natural candidates for such a prototype.

The reason is that there is no standard non-blocking way to access a relational database. So only those that support this kind of API are supported right now.

One could implement a ReactiveCrudRepository using JPA or JDBC and delegate the work to a thread pool. This would provide an async API on the outside, but would still consume the resources for the Threads and block between independent data accesses, so only a small part of the benefits of the reactive approach would get realized.

这篇关于ReactiveCrudRepository 在春季使用 Hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆