Java EE 5 中的两阶段提交事务
我想知道如何使用 Java EE5 进行两阶段提交事务...我正在将 EJB 与 JPA 一起使用,JPA 已使用 MySql 配置了 hibernate。我只想使用 JAVA EE 规范进行事务处理,而不使用 hibernate 或 JDBC 特定对象......
I want to know that how can I do two phase commit transaction by using Java EE5...I am using EJB with JPA which has hibernate configured with MySql. I just want to use JAVA EE specification for transaction not using hibernate or JDBC specific object....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要确保使用 JTA 事务来执行 JPA 中的所有事务性工作,您所需要做的就是指定持久性单元类型为 JTA,并指定 JPA 提供程序使用的 JTA 数据源。您的 persistence.xml 文件将包含类似于以下内容的内容:
此外,您必须确保 jta-data-source 属性中定义的数据源不采用允许本地事务等优化。简单来说,涉及该数据源的所有事务都必须是XA事务,或者数据源必须是不支持本地事务的XA数据源。
请注意,仅指定 JTA 数据源是不够的。您必须将持久性单元定义为需要使用 JTA 实体管理器的持久性单元,因为
transaction-type
属性的未定义值取决于 JPA 提供程序运行的环境。如果提供程序在 Java EE 环境中运行,则将创建JTA
实体管理器,而RESOURCE_LOCAL
实体管理器将在 Java SE 环境中创建。另请注意,如果您将
transaction-type
指定为RESOURCE_LOCAL
,那么在 Java EE 环境中,JPA 提供程序将忽略jta-data-source
值,并将依赖non-jta-data-source
值来创建连接。All you need to do, to ensure that JTA transactions are used to perform all transactional work in JPA, is to specify that the Persistence Unit type is JTA, and designate a JTA datasource for use by the JPA provider. Your persistence.xml file would have contents similar to the following:
Additionally, you must ensure that the datasource defined in the
jta-data-source
attribute, does not employ optimizations like allowing local transactions. In simpler words, all transactions involving the said datasource must be XA transactions, or the datasource must be an XA datasource without any support for local transactions.Note that merely specifiying a JTA data source is not sufficient. You must define the persistence unit as one requiring the use of JTA entity managers, as an undefined value for the
transaction-type
attribute, depends on the environment in which the JPA provider operates. If the provider operates in a Java EE environment,JTA
entity managers will be created, where asRESOURCE_LOCAL
entity managers will be created in a Java SE environment.Also, note that, if you specify the
transaction-type
asRESOURCE_LOCAL
, then in a Java EE environment, the JPA provider will ignore thejta-data-source
value, and will instead rely on thenon-jta-data-source
value for creating connections.