Java “jta-datasource”和“resource-local”数据源之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3217586/
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-13 17:30:24  来源:igfitidea点击:

Difference between a "jta-datasource" and a " resource-local " datasource?

javajpajakarta-eeejb-3.0jta

提问by stratwine

The terms "jta-datasource" and "resource-local datasource" are a little vague to me. I'm putting down what I am understanding ( or assuming ) and I'd like you to say where I'm right / wrong.

术语“jta-datasource”和“resource-local datasource”对我来说有点含糊。我正在写下我的理解(或假设),我希望你说出我对/错的地方。

  • The same database can be referred to as a jta-datasource or as a resource local datasource
  • If mentioned as jta-datasource, then the beans / other classes can use JTA. Hence, UserTransaction interface
  • Cannot use CMT/ BMTif the datasource is resource local
  • If mentioned as resource local datasource, transactions are not JTA aware. Code can use EntityTransaction interface but not UserTransaction interface
  • 同一个数据库可以称为 jta-datasource 或资源本地数据源
  • 如果提到 jta-datasource,那么 bean/其他类可以使用 JTA。因此,UserTransaction 接口
  • 如果数据源是本地资源,则不能使用CMT/ BMT
  • 如果作为资源本地数据源提及,事务不是 JTA 感知的。代码可以使用 EntityTransaction 接口,但不能使用 UserTransaction 接口

Thanks!

谢谢!

采纳答案by Pascal Thivent

The terms "jta-datasource" and "resouce-local datasource" are a little vague to me.

术语“jta-datasource”和“resource-local datasource”对我来说有点含糊。

I guess you actually refer to the jta-datasourceand non-jta-datasourceelements. In short:

我猜你实际上是指jta-datasourcenon-jta-datasource元素。简而言之:

  • if the transaction type of the persistence unit is JTA, the jta-datasourceelement is used to declare the JNDI name of the JTA data source that will be used to obtain connections. This is the common case.
  • if the transaction type of the persistence unit is resource-local, the non-jta-data-sourceshould be used to declare the JNDI name of a non-JTA data source.
  • 如果持久化单元的事务类型是JTA,则该jta-datasource元素用于声明将用于获取连接的 JTA 数据源的 JNDI 名称。这是常见的情况。
  • 如果持久化单元的事务类型是resource-local,则non-jta-data-source应该使用 来声明非 JTA 数据源的 JNDI 名称。
  • The same database can be referred to as a jta-datasource or as a resource local datasource
  • 同一个数据库可以称为 jta-datasource 或资源本地数据源

This is correct. And I didn't mention that just above but some providers even allow to declare both a jta-datasourceanda non-jta-datasourceand use the later for optimized reading through non-JTA connections (i.e. that won't be associated to an ongoing JTA transaction).

这是对的。我没有在上面提到这一点,但一些提供者甚至允许同时声明 ajta-datasourceanon-jta-datasource并使用后者通过非 JTA 连接进行优化读取(即不会与正在进行的 JTA 事务相关联)。

  • If mentioned as jta-datasource, then the beans / other classes can use JTA. Hence, UserTransaction interface.
  • 如果提到 jta-datasource,那么 bean/其他类可以使用 JTA。因此,UserTransaction 接口。

The first part is correct, the last part not entirely. From the EJB 3.0 spec, section 13.3.4 Enterprise Beans Using Container-Managed Transaction Demarcation:

第一部分是正确的,最后一部分不完全正确。来自 EJB 3.0 规范的13.3.4 Enterprise Beans Using Container-Managed Transaction Demarcation 部分

The enterprise bean's business methods [...] must not attempt to obtain or use the javax.transaction.UserTransactioninterface.

企业 bean 的业务方法 [...] 不得尝试获取或使用该javax.transaction.UserTransaction接口。

And section 16.12 UserTransaction Interface:

和第16.12UserTransaction 接口

The container must not make the UserTransactioninterface available to the enterprise beans that are not allowed to use this interface.

容器不得让UserTransaction不允许使用该接口的企业 bean 使用该接口。

In other words, the UserTransactioninterface is not available to CMT enterprise beans.

换句话说,该UserTransaction接口对 CMT 企业 bean 不可用。

  • Cannot use CMT / BMT if the datasource is resource local
  • 如果数据源是本地资源,则不能使用 CMT / BMT

The wording is a bit confusing here but I'd say that this not strictly correct. From the JPA 1.0 specification, section § 5.5 Controlling Transactions:

这里的措辞有点令人困惑,但我会说这并不完全正确。从 JPA 1.0 规范,第 5.5控制事务

An application-managed entity manager may be either a JTA entity manager or a resource-local entity manager.

...

Both JTA entity managers and resource-local entity managers are required to be supported in Java EE web containers and EJB containers. Within an EJB environment, a JTA entity manager is typically used.

应用程序管理的实体管理器可以是 JTA 实体管理器或资源本地实体管理器。

...

JTA 实体管理器和资源本地实体管理器都需要在 Java EE Web 容器和 EJB 容器中得到支持。在 EJB 环境中,通常使用 JTA 实体管理器。

And section 6.2.1.2 transaction-type

第 6.2.1.2 节交易类型

The transaction-typeattribute is used to specify whether the entity managers provided by the entity manager factory for the persistence unit must be JTA entity managers or resource-local entity managers. The value of this element is JTAor RESOURCE_LOCAL. A transaction-type of JTA assumes that a JTA data source will be provided — either as specified by the jta-data-sourceelement or provided by the container. In general, in Java EE environments, a transaction-typeof RESOURCE_LOCALassumes that a non-JTA datasource will be provided. In a Java EE environment, if this element is not specified, the default is JTA.

transaction-type属性用于指定实体管理器工厂为持久化单元提供的实体管理器必须是 JTA 实体管理器还是资源本地实体管理器。该元素的值为JTAor RESOURCE_LOCAL。JTA 的事务类型假定将提供 JTA 数据源 - 由jta-data-source元素指定或由容器提供。通常,在 Java EE 环境中,a transaction-typeofRESOURCE_LOCAL假定将提供非 JTA 数据源。在 Java EE 环境中,如果未指定此元素,则默认为 JTA。

So you CAN use an application managed entity managerwhich can be a resource-local entity manager(you must inject an EntityManagerFactoryto get the EM from it in that case) and it won't be part of a JTA transaction. See this (very interesting) discussion.

因此,您可以使用应用程序管理的实体管理器,该管理器可以是资源本地实体管理器EntityManagerFactory在这种情况下,您必须注入 an才能从中获取 EM),并且它不会成为 JTA 事务的一部分。看到这个(非常有趣的)讨论

  • If mentioned as resource local datasource, transactions are not JTA aware. Code can use EntityTransaction interface but not UserTransaction interface
  • 如果作为资源本地数据源提及,事务不是 JTA 感知的。代码可以使用 EntityTransaction 接口,但不能使用 UserTransaction 接口

Again, the wording is a bit confusing but I'd say that this is correct.

同样,措辞有点令人困惑,但我会说这是正确的。