File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes application managed EM can be either JTA or resource-local? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "application managed EM can be either JTA or resource-local?" Watch "application managed EM can be either JTA or resource-local?" New topic
Author

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

Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
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
    Raf Szczypiorski
    Ranch Hand

    Joined: Aug 21, 2008
    Posts: 383
    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
    Treimin Clark
    Ranch Hand

    Joined: Nov 12, 2008
    Posts: 757
    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.
    Raf Szczypiorski
    Ranch Hand

    Joined: Aug 21, 2008
    Posts: 383
    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.
    Treimin Clark
    Ranch Hand

    Joined: Nov 12, 2008
    Posts: 757
    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?
    Raf Szczypiorski
    Ranch Hand

    Joined: Aug 21, 2008
    Posts: 383
    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
    Treimin Clark
    Ranch Hand

    Joined: Nov 12, 2008
    Posts: 757
    Thanks again, Raf
    Fei Wong
    Greenhorn

    Joined: Jun 15, 2009
    Posts: 11
    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?
    Mihai Radulescu
    Ranch Hand

    Joined: Sep 18, 2003
    Posts: 916

    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


    SCJP, SCJD, SCWCD, OCPJBCD
    Fei Wong
    Greenhorn

    Joined: Jun 15, 2009
    Posts: 11
    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
    Mihai Radulescu
    Ranch Hand

    Joined: Sep 18, 2003
    Posts: 916

    Yes.
    Both, JTA and RESOURCE_LOCAL describes transactions - for different purposes (see the upper post from Raf)

     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: application managed EM can be either JTA or resource-local?