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

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

问题描述

是否可以将 Hibernate 和 RDBMS(Mysql、Postgres 等)与 ReactiveCrudRepository 而不是 CrudRepository 一起使用?我已经用 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(Reactive Relational D>atabase Connectivity)是对这样一个标准的提议.希望它要么有助于说服 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 个里程碑.请参阅发布文章了解详情.>

另见这个答案:为什么 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 在 spring 中使用 Hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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