Difference between a "jta-datasource" and a " resource-local " datasource?
Asked Answered
B

1

58

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!

Beriosova answered 10/7, 2010 at 2:29 Comment(0)
X
71

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.

Xanthic answered 10/7, 2010 at 6:54 Comment(4)
Hi, Thank you so much for taking time and explaining it all so clearly !!! I see now that we use the term "resource local EntityManager" and not "resource local datasource". Yes I meant non-jta-datasource when I said "resource local datasource". This is how I understand now: JTA / RESOURCE_LOCAL --> Transaction Type of the EntityManager. Determines who controls the underlying transaction. whether JTA / EntityTransaction API JTA EntityManger: Container manages this EntityManager. Involves in JTA transactions. A JTA transaction can be either a CMT or a BMT. Can be used in managed classes.Beriosova
Resource-Local EntityManager: The EnityManager is not managed by the container. Involves in Non-JTA transactions. EntityTransaction API is used. Can use in POJOs For BMT, UserTransaction always uses a jta-datasource and cannot use a non-jta-datasource Likewise for CMT too, the container could only use a jta-datasourceBeriosova
@stratwine: You're welcome, glad you found it helpful (and your understanding looks correct). Regarding the wording, I didn't mean to be picky but since the spec defines a very precise (and subtle) terminology, using it makes the communication easier, which is why I insisted a bit about that (and I'd recommend to read the sections I quoted partially).Xanthic
@PascalThivent The link to "this (very interesting) discussion" doesn't work, do you mean the following? java.net/projects/glassfish/lists/users/archive/2009-07/message/…Giliana

© 2022 - 2024 — McMap. All rights reserved.