users@glassfish.java.net

Glassfish / Hibernate Manual transaction demarcation

From: Josh Donkersloot <josh_at_geomaxmobile.com>
Date: Tue, 06 Nov 2007 12:40:46 -0600

Glassfish / Hibernate Manual transaction demarcation

Hi All,
I am trying to use the following code in a thread that is not managed by
glassfish (it is a callback method that is registered during a
webservice method). The global jndi lookup of both "MyDAORemote" and
the UserTransaction work correctly, and the code runs without any
exceptions, even "myObj" is retrieved correctly using the DAO. The
problem is that the status update to "myObj" is not persisted to the
database, the change is never saved even though I do commit the
transaction. Any help or suggestions would be appreciated. Do I need
to attach the transaction to the entity manager some how?

Thanks,
Josh

/***************************************************************/
 public void onResult(int resultId) {
            try {
             
                //jndi lookup of DAO as this is not a managed method
                Context jndiContext = new javax.naming.InitialContext();
                Object remoteDAOObj=
jndiContext.lookup("com.geomaxmobile.dao.MyDAORemote");
                MyDAORemote remoteDAO = (MyDAORemote) remoteDAOObj;
               
                //jndi lookup of usertransaction
                Object utObject =
jndiContext.lookup("java:comp/UserTransaction");
                UserTransaction tx = (UserTransaction) utObject;
                tx.begin();
               
               
                MyObject myObj= remoteDAO .findById(resultId, false);
                myObj.setStatus("Done");

               
                //commit the transaction
              tx.commit();
               
            } catch (Exception e) {
                e.printStackTrace();
            }
        }