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
Difference between a "jta-datasource" and a " resource-local " datasource?
提问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-datasource
and non-jta-datasource
elements. In short:
我猜你实际上是指jta-datasource
和non-jta-datasource
元素。简而言之:
- if the transaction type of the persistence unit is JTA, the
jta-datasource
element 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-source
should 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-datasource
anda non-jta-datasource
and use the later for optimized reading through non-JTA connections (i.e. that won't be associated to an ongoing JTA transaction).
这是对的。我没有在上面提到这一点,但一些提供者甚至允许同时声明 ajta-datasource
和anon-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.UserTransaction
interface.
企业 bean 的业务方法 [...] 不得尝试获取或使用该
javax.transaction.UserTransaction
接口。
And section 16.12 UserTransaction Interface:
和第16.12节UserTransaction 接口:
The container must not make the
UserTransaction
interface available to the enterprise beans that are not allowed to use this interface.
容器不得让
UserTransaction
不允许使用该接口的企业 bean 使用该接口。
In other words, the UserTransaction
interface 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-type
attribute 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 isJTA
orRESOURCE_LOCAL
. A transaction-type of JTA assumes that a JTA data source will be provided — either as specified by thejta-data-source
element or provided by the container. In general, in Java EE environments, atransaction-type
ofRESOURCE_LOCAL
assumes 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 实体管理器还是资源本地实体管理器。该元素的值为JTA
orRESOURCE_LOCAL
。JTA 的事务类型假定将提供 JTA 数据源 - 由jta-data-source
元素指定或由容器提供。通常,在 Java EE 环境中,atransaction-type
ofRESOURCE_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 EntityManagerFactory
to 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.
同样,措辞有点令人困惑,但我会说这是正确的。