开发者

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

开发者 https://www.devze.com 2023-01-07 02:45 出处:网络
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

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.

  • 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 / BMT if 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

Thanks!


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

I guess you actually refer to the jta-datasource and non-jta-datasource elements. In short:

  • 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.
  • The same database can be referred to as a jta-datasource or as a resource local datasource

This is correct. And I didn't mention that just above but some providers even allow to declare both a jta-datasource and a 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).

  • If mentioned as jta-datasource, then the beans / other classes can use JTA. Hence, UserTransaction interface.

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:

The enterprise bean’s business methods [...] must not attempt to obtain or use the javax.transaction.UserTransaction interface.

And section 16.12 UserTransaction Interface:

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

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

  • Cannot use CMT / BMT if the datasource is resource local

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:

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.

And section 6.2.1.2 transaction-type

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 is JTA or RESOURCE_LOCAL. A transaction-type of JTA assumes that a JTA data source will be provided — either as specified by the jta-data-source element or provided by the container. In general, in Java EE environments, a transaction-type of RESOURCE_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.

So you CAN use an application managed entity manager which 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.

  • If mentioned as resource local datasource, transactions are not JTA aware. Code can use EntityTransaction interface but not UserTransaction interface

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号