Forums Register Login

application managed EM can be either JTA or resource-local?

+Pie Number of slices to send: Send
It says that application managed entity managers can be either resource-local or JTA. I didn't understand this.

What I thought is,

  • A transaction with a container-managed entity managers is called "JTA Transaction".
  • A transaction with an application-managed entity managers is called "resource-local Transaction".


  • If application-managed entity manager can be either JTA or resource-local, then what is the difference between JTA and resource-local?

    Confused
    +Pie Number of slices to send: Send
    Hi. All container-managed entity managers are JTA managers. An application-managed entity manager may be either JTA or resource-local, and they are always extended. The difference is that when it is JTA, it simply is used in a distributed transaction with EJB calls, Timer creations, other resource managers and so on. When the UserTransaction (either container-managed or bean-managed, it doesn't really matter) is rolled back, all of the mentioned operations are rolled back. If you used resource-local manager (EntityTransaction interface to demarcate transactions), the rollback on the UserTransaction would not influence the entity manager operations. Hence, you will most likely use JTA in Java EE, whereas resource-local are designed to be usable in Java SE, when there is possibly no JTA implementation available.
    One more note - when you use an application-managed JTA entity manager and create them outside an active JTA transaction (for example, when you inject a manager factory and in @PostConstruct you create the entity manager), you call joinTransaction() before you call refresh(), persist(), merge() or remove(), so that these operations don't throw TransactionRequiredException. The JTA transaction may either be container- or bean-managed.
    Does this make sense?

    Raf
    +Pie Number of slices to send: Send
    Thanks Raf,

    Can you tell me in example that how to create an application-managed entity manager with JTA and resource-local transactions? This is the point what I couldn't understand.
    +Pie Number of slices to send: Send
    Application-managed JTA:



    For resource local, in Java SE, with persistance-unit type set to "RESOURCE_LOCAL":

    Note that the above code does not have to be supported by an EJB container.

    Cheers.
    +Pie Number of slices to send: Send
    Thank you Raf,

    Raf Szczypiorski wrote:
    Note that the above code does not have to be supported by an EJB container.



    Why did you say this?
    +Pie Number of slices to send: Send
    JPA specs, 7.2 Bootstrapping in Java SE Environments, page 154, and footnote #42, page 154:


    Use of these Java SE bootstrapping APIs may be supported in Java EE containers; however, support for such use is not required.



    So, basically, the answer is simple - because the specs say so ;-)

    Raf
    +Pie Number of slices to send: Send
    Thanks again, Raf
    +Pie Number of slices to send: Send
    then may I know what is the difference between container-managed entity managers JTA and application-managed entity managers JTA?
    Are there both same in terms of JTA?
    +Pie Number of slices to send: Send
    Hi Fei,

    In the "container-managed entity manager" the container care about the life cycle for the involved resources (or more precisely the resource manager connection factory) and the application is focus only on the business. In the "application mode" the application (you can read here the developer) must care about the resource life cycle (e.g. it must close the factory), so in this case the application must focus on business and on resource life cycle.

    The JTA is only the transactional model.

    Regards,
    Mihai

    +Pie Number of slices to send: Send
    Hi Mihai Radulescu,

    I roughly get your meaning. from the transaction model point of view, these 2 are no difference.
    while from the entity point of view, there is the difference. Am I correct?

    Thanks
    +Pie Number of slices to send: Send
    Yes.
    Both, JTA and RESOURCE_LOCAL describes transactions - for different purposes (see the upper post from Raf)

    Would you turn that thing down? I'm controlling a mind here! Look ... look at the tiny ad ...
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 3480 times.
    Similar Threads
    getTransaction() on transaction-scoped persistence context.
    Entity Managers - Types
    Contradiction about em.getTranaction() in the spec !!
    Resource Local Transactions
    Problem with understanding Transaction Management Demarcation
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 28, 2024 14:24:20.