Java 使用什么事务管理器?(JPA, 春天)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3880563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 06:18:20  来源:igfitidea点击:

What transaction manager to use? (JPA, Spring)

javaspringjpatransactions

提问by John Manak

I'm developing a web application based on JPA + Hibernate, Spring and Wicket. I was wondering what's the best way of implementing transactions in my code? What transaction manager should I use? Should it be org.springframework.orm.jpa.JpaTransactionManager, or org.springframework.jdbc.datasource.DataSourceTransactionManageror something else? I'd like to use Spring for managing my transactions.

我正在开发基于 JPA + Hibernate、Spring 和 Wicket 的 Web 应用程序。我想知道在我的代码中实现事务的最佳方式是什么?我应该使用什么事务管理器?应该是org.springframework.orm.jpa.JpaTransactionManagerorg.springframework.jdbc.datasource.DataSourceTransactionManager还是其他什么?我想使用 Spring 来管理我的交易。

采纳答案by Sean Patrick Floyd

nanda is right, you canonly use JpaTransactionManager. The Transaction Manager abstraction we are talking about here is Spring's PlatformTransactionManagerinterface, and JPATransactionManageris the only implementation of that interface that understands JPA.

南大是正确的,你可以只使用JpaTransactionManager接口。我们这里所说的事务管理器抽象是 Spring 的PlatformTransactionManager接口,而JPATransactionManager是该接口的唯一理解 JPA 的实现。

You should read the chapter Transaction Managementfrom the Spring reference to better understand this topic.

您应该阅读Spring 参考中的事务管理一章以更好地理解该主题。

回答by nanda

org.springframework.orm.jpa.JpaTransactionManager

My preference is to use this with annotation:

我的偏好是将其与注释一起使用:

<tx:annotation-driven transaction-manager="myTxManager" />

回答by Neeraj Gahlawat

The org.springframework.transaction.PlatformTransactionManager interface is the key abstraction in the Spring API providing essential methods for controlling transaction operations at run-time: begin, commit and rollback.

org.springframework.transaction.PlatformTransactionManager 接口是 Spring API 中的关键抽象,它提供了在运行时控制事务操作的基本方法:开始、提交和回滚。

PlatformTransactionManagerinterface, its implementations

PlatformTransactionManager接口,它的实现

  1. JtaTransactionManager-----> JTA
  2. DataSourceTransactionManager-----> JDBC
  3. JpaTransactionManager------> JPA
  4. HibernateTransactionManager------> Hibernate
  1. JtaTransactionManager-----> JTA
  2. DataSourceTransactionManager-----> JDBC
  3. JpaTransactionManager------> JPA
  4. HibernateTransactionManager------>休眠

it depand on your requirment which moudle of spring your are using

这取决于您的要求,您使用的是哪种弹簧