Java EE 5 中的两阶段提交事务

发布于 2024-12-01 09:42:55 字数 140 浏览 3 评论 0原文

我想知道如何使用 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 技术交流群。

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

发布评论

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

评论(1

萌无敌 2024-12-08 09:42:55

要确保使用 JTA 事务来执行 JPA 中的所有事务性工作,您所需要做的就是指定持久性单元类型为 JTA,并指定 JPA 提供程序使用的 JTA 数据源。您的 persistence.xml 文件将包含类似于以下内容的内容:

<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <!-- Specifies the type of the entity managers used by the persistence unit,
         as a JTA entity manager -->
    <persistence-unit name="example-pu" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <!-- Specifies a JTA datasource for use by the JPA provider.
             All connections obtained by the JPA provider for this persistence unit
             will be from this datasource -->
        <jta-data-source>jdbc/myDS</jta-data-source>
            ...
    </persistence-unit>
 </persistence>

此外,您必须确保 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:

<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <!-- Specifies the type of the entity managers used by the persistence unit,
         as a JTA entity manager -->
    <persistence-unit name="example-pu" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <!-- Specifies a JTA datasource for use by the JPA provider.
             All connections obtained by the JPA provider for this persistence unit
             will be from this datasource -->
        <jta-data-source>jdbc/myDS</jta-data-source>
            ...
    </persistence-unit>
 </persistence>

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 as RESOURCE_LOCAL entity managers will be created in a Java SE environment.

Also, note that, if you specify the transaction-type as RESOURCE_LOCAL, then in a Java EE environment, the JPA provider will ignore the jta-data-source value, and will instead rely on the non-jta-data-source value for creating connections.

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