java - JPA 中的事务同步与事务关联

标签 java hibernate jpa transactions persistence

我不明白这些概念之间的区别。 Pro JPA 2 的说明如下:

Transaction synchronization is the process by which a persistence context is registered with a transaction so that the persistence context can be notified when a transaction commits. The provider uses this notification to ensure that a given persistence context is correctly flushed to the database.

Transaction association is the act of binding a persistence context to a transaction. You can also think of this as the active persistence context within the scope of that transaction.

您能否提供更多解释,也许是实际例子?我应该在不使用任何 JTA 事务的 Java SE 环境中关心它吗?感谢您的任何注释!

最佳答案

事务同步:您可以将其视为 Spring 的 TransactionSynchronization 接口(interface),该接口(interface)接收事务同步的回调。它具有各种方法,例如 afterCommit()afterCompletion()beforeCommit() 根据事务状态调用。考虑一个实际示例,您希望在用户注册完成后向用户发送电子邮件,根据事务状态通知任何外部服务或记录任何特定的信息事件..

事务关联:我们基本上在 Activity 持久化上下文下提交事务..让它成为 JPA 的实体管理器或 Hibernate 的 session ..

我应该在不使用任何 JTA 事务的 Java SE 环境中关心它吗?是的..您将不得不退回到 JDBC 事务划分..

关于java - JPA 中的事务同步与事务关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45266595/

相关文章:

java - 如何让一个越来越透明最后消失的JPanel呢?

Java异常-第n次尝试返回到 "try"语句 block

spring - 为什么 Spring JPA 将有效日期转换为无法识别的格式化字符串?

hibernate - 登录应用程序异常

java - JPA 应用程序中的连接何时返回到连接池?

java - 领域驱动设计中的 JPA 实体责任

java - 框架和 Canvas 变得比指定的大

java - 如何为 GridBagLayout 中的所有组件设置间隙?

java - 在数据库中使用 Java 8 LocalDate 和 LocalDateTime 进行 hibernate

hibernate - 使用Postgresql文本类型在Grails域类中映射Clob类型