Spring Boot &Spring Data:Hibernate Sessions 是如何管理的? [英] Spring Boot & Spring Data: how are Hibernate Sessions managed?

查看:26
本文介绍了Spring Boot &Spring Data:Hibernate Sessions 是如何管理的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个使用 Spring Boot 和 Spring Data(准确地说是它的 JpaRepository 接口)和 Hibernate 的应用程序.

I am currently working on an application that uses Spring Boot and Spring Data (its JpaRepository interfaces to be precise) together with Hibernate.

我喜欢 Hiberante 的一件事是它的缓存功能 - 当您提交与特定对象匹配的多个查询时,您将在每次查询执行时返回该对象的相同实例(相对于 Java 的 == 运算符).但是,当使用 Spring Data 和 JpaRepository 类时,情况似乎并不总是如此.出于这个原因,我假设这里有多个 HibernateSession 实例在工作.

One thing I love about Hiberante is its caching feature - when you submit multiple queries that match a particular object, you will get back the same instance of that object on every query execution (with respect to Java's == operator). However, when using Spring Data and JpaRepository classes, this does not always seem to be the case. For that reason, I assume that there are multiple HibernateSession instances at work here.

因此,我的问题是:Spring Data 如何处理 Hibernate 会话?它什么时候打开或关闭它们?有没有办法将它配置为在我的应用程序的整个运行时使用相同的会话以充分利用 Hibernate 的对象缓存?有理由那样做吗?

My question therefore is: how does Spring Data handle Hibernate Sessions? When does it open or close them? Is there a way to configure it to use the same session for the entire runtime of my application to make full use of Hibernate's object cache? Is there a reason not to do it that way?

谢谢,

艾伦

推荐答案

我想我自己已经找到了答案.如果有人发现这个问题,这是我的答案.

I think I've found the answer myself. If somebody finds this question, here's my answer.

Spring 如何管理 Hibernate Session?

默认情况下,Spring Boot 在存储库级别应用事务管理.在这种情况下,当调用 JpaRepository 方法(或通常任何 Repository 方法)时,Spring 将:

By default, Spring Boot applies transaction management at the repository level. In this case, when calling a JpaRepository method (or in general any Repository method), Spring will:

  • 要求 SessionFactory 创建一个新会话
  • 打开此会话
  • 开启交易
  • 执行调用的Repository方法
  • 关闭交易
  • 关闭会话

但是,如果您将 @Transactional 应用于服务类或方法,Spring 将在进入服务方法时打开会话和事务,并且存储库方法将在现有事务中执行.

However, if you apply @Transactional to the service class or method, Spring will open the session and the transaction on entry to the service method, and the repository method will be performed within the existing transaction.

会有什么后果?

作为程序员...

  • 您根本不需要关心交易或会话.
  • 如果您想依赖 Hibernate 的缓存功能,则必须在比存储库更大的范围内指定 @Transactional.缓存只在同一个 HibernateSession 中起作用.
  • 您必须通过它们的 Hibernate ID 值来确定 @Entity 对象的等效性,而不是使用 Java 的 == 运算符.
  • 您需要注意 @Entity 类中的惰性集合(例如在 @OneToMany 引用中)(请参阅 FetchMode.LAZYFetchMode.EAGER 不同)仅在 @Transactional 注释方法中使用
  • you do not need to concern yourself with transactions or sessions at all.
  • if you want to rely on Hibernate's caching functionality, you must specify @Transactional on a larger scope than the repository. Caching only works within the same HibernateSession.
  • you must decide the equivalence of your @Entity objects by their Hibernate ID value, rather than by using Java's == operator.
  • you need to take care that lazy collections (for example in an @OneToMany reference) in your @Entity classes (see FetchMode.LAZY as opposed to FetchMode.EAGER) are used exclusively within an @Transactional-annotated method

作为参考,以下链接非常有帮助:单个会话中的多个事务

Also for reference, the following link has been quite helpful: Multiple Transactions in single session

与 Spring 的许多其他方面一样,如果您愿意牺牲对应用程序的直接控制,这里有很多收获.

As with many other aspects of Spring, there is a lot to be gained here, if you are willing to sacrifice direct control over your application.

这篇关于Spring Boot &Spring Data:Hibernate Sessions 是如何管理的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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