- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在标准同步 Spring (WebMVC) 世界中,有 OpenEntityManagerInViewFilter
和 OpenEntityManagerInViewInterceptor
在整个处理过程中保持 JPA EntityManager
打开。请求并允许避免烦人的 LazyInitializationException
(“无 session ”)。
OpenEntityManagerInViewInterceptor
对于基于异步 servlet API 的应用程序也以类似的方式工作。
但是在 Webflux 应用程序中如何处理这个问题呢?假设我有一个 Webflux Controller ,它可以执行类似的操作
service.getOneReactively(...).flatMapMany(one -> obtainAFlux(one))
其中 service.getOneReactively()
在数据库中查找域对象,并且 obtainAFlux()
访问该域对象上的惰性集合,从而导致其加载。这两个调用都将在某个线程池上执行,可能在不同的线程上执行,因此 Hibernate 的 Session 绑定(bind)到第一个线程(其中具体化了域对象)将无法在第二个线程(其中集合已加载)。
这会导致LazyInitializationException
。
遇到这样的问题怎么解决呢?到目前为止,我唯一能发明的就是将这两个调用打包到一个方法中,并在一个 react 式调用的事务中调用它。
最佳答案
Hibernate 不是响应式的,它使用 JDBC,这是一个阻塞数据库驱动程序,并且它又使用 threadlocal 来存储在 Webflux 中无法使用的 session 信息。
如果您希望执行阻塞数据库调用,则必须使用 Mono.fromCallable
并将调用分配给它自己的调度程序,以便它获得自己的专用线程。您可以在文档 https://projectreactor.io/docs/core/release/reference/#faq.wrap-blocking 中阅读更多相关信息。 hibernate 本身无法返回 Flux。
你的代码应该是这样的
Flux<Objects> myObjects = Mono.fromCallable(() -> {
// make all your calls to the database here
// build then your flux or whatever you want
return Flux.fromIterable(listOfObjects);
}).subscribeOn(Schedulers.elastic()); // Makes sure this is run in its own thread
关于java - Spring webflux : keeping an EntityManager open during the whole process of a reactive request handling,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57597313/
有一些使用多个数据源的示例: @Inject @DataSource("users") AgroalDataSource dataSource1; @Inject @DataSource("inven
我试图使用持久性和 servlet guice 扩展,让简单的 webapp 在 Jetty 上与 Guice 和 JPA 一起工作。 我写了这个服务实现类: public class PersonS
我使用 Hibernate 5.1.0.Final、Guice、Jersey。我有创建 EntityManagerFactory 的 HibernateModule并管理EntityManager实例
我正在使用 ThreadLocal 和请求/实体模式来获取实体。这种情况发生时,我关闭了一个实体管理器,并且在后台有一些实体可以编辑、复制和修改,然后我想将其保留或与新的实体管理器合并。我不知道这是一
好的,我正在使用 EJB 3.0 和 hibernate,我们将 .ear 文件放入嵌入 Apache Tomcat 6.0.18 的 Easy-Beans 1.0.1(带有 Hibernate)部署
我正在使用 Spring + JPA + Hibernate + EntityManager 与数据库对话。我收到“A JTA EntityManager cannot use getTransact
更新数据库时我应该更喜欢什么?这两种方法的优缺点是什么?我什么时候应该使用其中一种? public void disemployEmployee(Integer employeeId, Date en
我正在尝试在存储库中注入(inject) EntityManager。 编译成功,但是当我运行应用程序并发送一个发布请求时,我收到了这个错误: Unexpected error occurred: F
我正在尝试在 Spring Tools Suite 和 Pivotal tc Server Developer Edition 上开发 Spring+Hibernate+EntityManager+S
我有一个使用 spring boot + spring data JPA 的示例项目。在日志中,我观察到 EntityManagers 在我进行第一次 rest 调用之前被创建了几次。请澄清为什么会发
给定网络应用程序中的以下情况: // EntityManager em, one per Request with Spring's OpenEntityManagerInViewFilter //
当我们在 JAVA EE 环境中的 EntityManager 上使用 @PersistenceContext 注释时,容器将创建 entityManagerFactory(我猜是整个 session
我遇到了一种情况(我认为这很奇怪,但可能很正常),我使用 EntityManager.getReference(LObj.getClass(), LObj.getId()) 来获取数据库实体,然后通过
我有以下服务... @Stateless @LocalBean public class RandomService { @EJB RandomString stringTokenizer;
我在实体类中有这个函数,但 getDoctrine 不喜欢...... public function getObject() { $em = $this->getDoctrine()->ge
我正在尝试以级联方式保存某个对象并检索它。 我有 3 个对象超过 3 个实体。 实体: class Order { /** * @var integer $id *
我正在开发一个 JPA 应用程序(使用 hibernate ),我正在与 作斗争。自动冲洗 特征。 默认情况下,每当我们处理对任何实体的查询时,完整的 实体管理器 被冲洗。在大多数情况下这是可以的:我
我刚刚建立了一个到目前为止仍然非常小的项目 maven/jpa/hibernate 项目,我试图在其中持久化一个对象。 我的类(class)是一个非常简单的类(class): @Entity publ
我当前的项目使用 HSQLDB2.0 和 JPA2.0 。 场景是:我查询数据库以获取 contactDetails 的列表的 person .我删单contactInfo在 UI 中,但不保存该数据
我是 jpa 和 spring 世界的新手,我目前正在对一个简单的方法进行一些单元测试,但只有当我在单元测试模式下运行我的测试类时才会继续收到此错误消息: java.lang.IllegalState
我是一名优秀的程序员,十分优秀!