hibernate中的事务边界是什么

发布于 2025-01-18 03:18:39 字数 280 浏览 0 评论 0原文

我有2个彼此相关的问题

Q1,冬眠/春季数据JPA中的交易边界到底是什么。 我是JPA的新手,所以请给出一个非常基本的示例,这样我就可以理解,因为我尝试阅读多个博客,但仍然不太清楚。

Q2以及最重要的是,这是什么意思 -

在Hibernate中,Persist()方法可以保证,如果在交易边界之外调用插入语句,则不会执行插入语句,Save()方法不能保证相同。

什么是 在交易边界的外部和内部以及如何在范围内执行执行?

I have 2 questions related to each other

Q1 What exactly is a transaction boundary in hibernate/Spring Data JPA.
I am new to JPA , so please give a very basic example so I can understand as I tried to read multiple blogs but still not very clear.

Q2 And on top of it, what does this mean-

In hibernate, persist() method guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries, save() method does not guarantee the same.

What is outside and inside of a transaction boundary and how executions are performed outside boundaries?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰火雁神 2025-01-25 03:18:39

交易是完全执行或根本执行的工作单位。

交易在典型的关系数据库中很容易使用。
您可以通过修改一些数据开始交易。每次修改都会开始交易,通常无法避免。您可以通过执行提交回滚来结束交易。

在交易完成之前,您的更改在其他交易中无法看到(有例外,变化和详细信息)。如果您回滚您的交易数据库中的所有更改都将撤消。
如果您提交您的更改将对其他交易可见,即连接到同一数据库的其他用户。如果更改仅在新交易中或已经运行的交易中可见,则实施方式在许多其他方面有所不同。

JPA中的交易是数据库事务以及其他内容。
您可以通过获取事务对象和调用方法来开始和结束事务。但是没有人会这样做,因为它容易出错。取而代之的是,您使用@transaction注释方法并输入该方法将启动事务并退出方法将结束事务。
详细信息在春季得到照顾。

JPA交易的棘手部分是,在JPA交易中,JPA可能(并且将)选择延迟甚至避免尽可能长时间地读取和写入操作。例如,当您加载实体并将其再次加载到同一JPA事务中时,JPA不会从数据库加载它,而是在第一个加载操作期间返回其返回的实例。如果您想了解更多有关此信息的信息,我建议您研究JPAS第一级缓存。

A transaction is a unit of work that is either executed completely or not at all.

Transactions are fairly simple to use in a typical relational database.
You start a transaction by modifying some data. Every modification starts a transaction, you typically can't avoid it. You end the transaction by executing a commit or rollback.

Before your transaction is finished your changes can't be seen in other transactions (there are exceptions, variations and details). If you rollback your transaction all your changes in the database are undone.
If you commit your changes your changes become visible to other transactions, i.e. for other users connected to the same database. Implementations vary among many other things if changes become visible only for new transactions or also for already running transactions.

A transaction in JPA is a database transaction plus additional stuff.
You can start and end a transaction by getting a Transaction object and calling methods on it. But nobody does that anymore since it is error prone. Instead you annotate methods with @Transaction and entering the method will start a transaction and exiting the method will end the transaction.
The details are taken care of by Spring.

The tricky part with JPAs transactions is that within a JPA transaction JPA might (and will) choose to delay or even avoid read and write operations as long as possible. For example when you load an entity, and load it again in the same JPA transaction, JPA won't load it from the database but return the same instance it returned during the first load operation. If you want to learn more about this I recommend looking into JPAs first level cache.

就像说晚安 2025-01-25 03:18:39

事务边界是事务开始或提交/回滚的地方。

A transaction boundary it's where the transaction starts or is committed/rollbacked.

去了角落 2025-01-25 03:18:39

当事务启动时,事务上下文就绑定到当前线程。因此,无论消息流中有多少个端点和通道,只要您确保消息流在同一线程上继续,您的事务上下文都会被保留。一旦您通过引入可轮询通道或执行器通道或在某些服务中手动启动新线程来打破它,事务边界也将被打破。

  • 其他一些人问过这个问题 - 查一下。

如果您有不明白的地方,请再次更准确地给我写信,我会解释。
我真的希望我有所帮助!

When a transaction is started, the transaction context is bound to the current thread. So regardless of how many endpoints and channels you have in your Message flow your transaction context will be preserved as long as you are ensuring that the flow continues on the same thread. As soon as you break it by introducing a Pollable Channel or Executor Channel or initiate a new thread manually in some service, the Transactional boundary will be broken as well.

  • some other people ask about it - look it up.

If you do not understand something write to me again more accurately and I will explain.
I really hope I helped!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文