users@glassfish.java.net

Glassfish / Hibernate / ejb3 dao issue

From: Josh Donkersloot <josh_at_geomaxmobile.com>
Date: Tue, 18 Sep 2007 10:29:22 -0500

Good morning all,

I have been trying to use glassfish / hibernate / ejb3 to write a
webservice but I ran into a problem. My EJBs are reading correctly from
the database, but I am having issues writing to the database.

When I call updateRecord() (in the code below) for the 1st time the
changes to theRecord are not persisted in the database. When I call
updateRecord() for the second time the changes from the 1st call are
persisted. I believe that this is due to a new transaction being
created for each call, forcing the previous transaction to be committed?

What I need to be able to do is that for each time I call updateRecord()
a new transaction is created and it is committed (the changes are
written) to the db when it reaches the end of the updateRecord() function.

Any ideas how I can get this functionality without having to manually
lookup the session and commit it?

Thanks,
Josh

@WebService
public class WebService{
   @EJB private MyDAO myDAO;

   @WebMethod
   @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
   public void updateRecord( ) throws Exception{
        Record theRecord = myDAO.getRecord();
        theRecord.setStuff( "test" );
      }

}