-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Right combination of annotations for CMT JTA transaction
PostPosted: Fri Oct 31, 2008 2:19 pm 
Newbie

Joined: Fri Oct 31, 2008 1:35 pm
Posts: 3
Hibernate version: annotations 3.3.0.GA, entity manager 3.3.1.GA, core 3.2.5.GA

Hi everyone,

I'm developing a web application deployed in GlassFish v2 using the Hibernate components/versions listed above.

My persistence.xml file looks like the following:
Code:
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="JomarAPIServerPU" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>jdbc/JomarDatabase</jta-data-source>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect"/>
    </properties>
  </persistence-unit>
</persistence>


I'm having problems finding the right combination of @Resource, @PersistenceUnit and @PersistenceContext.

If I use @PersistenceUnit(name="JomarAPIServerPU") I can load entities correctly, but since I've configured it to use JTA I can't use em.getTransaction(). So I added @Resource UserTransaction and somehow the transaction instance is getting injected because the object is not null when I debug it. However, it appears the Entity Manager is not "bound" to the UserTransaction injected because even when I don't get any exception, the changes are not committed to the database.

The relevant parts of the code in this case are:

Code:
    @Resource
    private UserTransaction tx;
    @PersistenceUnit(unitName = "JomarAPIServerPU")
    private EntityManagerFactory emf;

    private void executeUpdateLogisticCenters(HttpServletRequest request, HttpServletResponse response, Document requestData) throws Exception {
        EntityManager em = emf.createEntityManager();
...
        try {
            tx.begin();
            customer = em.find(customerId);
            plant = em.find(plantId);
          warehouse = em.find(warehouseId);
          logisticCenter = new MrpLogisticCenter();
          logisticCenter.setCusid(customer);
          logisticCenter.setPltid(plant);
          logisticCenter.setWhsid(warehouse);
          logisticCenter.setProjectCode(projectCode);
           em.persist(logisticCenter);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
            throw new RuntimeException(e.getMessage());
        }
    }


I also tried using ...

Code:
    @PersistenceContext
    private EntityManager em;


... and no transaction at all as suggested in Hibernate EntityManager documentation for CMT but I received an exception because a transaction was required and no transaction was available when calling the persist method.

Finally, I also tried using @Resource for both the UserTransaction and the EntityManagerFactory as suggested in Hibernate EntityManager documentation for BMT, like this:

Code:
    @Resource
    private UserTransaction tx;
    @Resource
    private EntityManagerFactory emf;


... but I got an Injection Exception like this one:

Code:
com.sun.enterprise.InjectionException: Exception attempting to inject Env-Prop: tag.jomarAPI.servlet.UpdateServlet/emf@Field-Injectable Resource. Class name = tag.jomarAPI.servlet.UpdateServlet Field name=emf@java.lang.String@tag.jomarAPI.servlet.UpdateServlet/emf@@ into class tag.jomarAPI.servlet.UpdateServlet


Bottom line, I'm obviously mixing different methods of setting up my persistence environment and probably even mixing methods used in different versions of Hibernate or in different combinations of Hibernate components.

If I'm making an obvious mistake or missing a part of the process here, can someone point me out to it? I have been unable to find a complete, working example of how to use EJB3 CMT-JTA persistence in a web application in GlassFish V2 using Hibernate Core, EntityManager and Annotations.

Thank you very much,

_________________
Mauricio Peccorini


Top
 Profile  
 
 Post subject: RE: Right combination of annotations for CMT JTA transaction
PostPosted: Fri Oct 31, 2008 4:58 pm 
Newbie

Joined: Fri Oct 31, 2008 1:35 pm
Posts: 3
Just an update of my thoughts. If I use

Code:
@PersistenceUnit(unitName = "JomarAPIServerPU")
    private EntityManagerFactory emf;

    private void execute() throws Exception {
        EntityManager em = emf.createEntityManager();
        MrpLogisticCenter logisticCenter = null;
        ...
        try {
          ...
          logisticCenter = new MrpLogisticCenter();
          ...
          logisticCenter.setProjectCode(projectCode);
          em.persist(logisticCenter);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }


I get a javax.persistence.TransactionRequiredException. However, if I add a Transaction like this:

Code:
@PersistenceUnit(unitName = "JomarAPIServerPU")
    private EntityManagerFactory emf;

    @Resource
    private UserTransaction tx;

    private void execute() throws Exception {
        EntityManager em = emf.createEntityManager();
        MrpLogisticCenter logisticCenter = null;
        ...
        try {
          tx.begin();
          logisticCenter = new MrpLogisticCenter();
          ...
          logisticCenter.setProjectCode(projectCode);
          em.persist(logisticCenter);
          tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
            throw new RuntimeException(e.getMessage());
        }
    }


... then I don't get the exception. So it is working up to some point. I still don't understand why it doesn't actually commit the information to the database. I have tried to UPDATE and DELETE records instead of INSERTing them but I get the same result, no exception but no changes in the database.

_________________
Mauricio Peccorini


Top
 Profile  
 
 Post subject: SOLUTION: Right combination of annotations for CMT JTA
PostPosted: Fri Oct 31, 2008 5:49 pm 
Newbie

Joined: Fri Oct 31, 2008 1:35 pm
Posts: 3
Just in case this ever helps anyone, I found the solution. I moved the call to emf.createEntityManager() after the call to tx.begin() and added em.close() in a finally block. The final (working) code is like this:

Code:
    @PersistenceUnit(unitName = "JomarAPIServerPU")
    private EntityManagerFactory emf;

    @Resource
    private UserTransaction tx;

    private void execute() throws Exception {
        EntityManager em = null;
        MrpLogisticCenter logisticCenter = null;
        ...
        try {
          tx.begin();
          em = emf.createEntityManager()
          logisticCenter = new MrpLogisticCenter();
          ...
          logisticCenter.setProjectCode(projectCode);
          em.persist(logisticCenter);
          tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
            throw new RuntimeException(e.getMessage());
        } finally {
          if (em != null) {
              em.close();
          }
        }
    }

_________________
Mauricio Peccorini


Top
 Profile  
 
 Post subject: Transaction Timeout when modifying large resultsets
PostPosted: Thu Dec 11, 2008 5:35 pm 
Newbie

Joined: Wed Apr 04, 2007 1:56 pm
Posts: 5
Location: Boston Area
Mauricio,

Many thanks for providing the right combination of annotations - I was tearing my hair out trying to scale my transactioning for a large dataset. I went through pretty much the same set of trials as you. Glad to finally get it right!

Ilane


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.