users@glassfish.java.net

Re: Glassfish / Hibernate / ejb3 dao issue

From: Sahoo <sahoo_at_sun.com>
Date: Wed, 19 Sep 2007 11:16:43 +0530

OK, the problem is with your WebService. I did not notice earlier that
it was *not* an EJB end point, instead you have it as an servlet end
point. You can't use @javax.ejb.TransactionAttribute in a servlet. You
can change the WebService to be an EJB based web service, simply by
*annotating* your class as @Stateless, or use a UserTransaction to begin
and end a transaction explicitly like this:

@WebService
public class WebService{

   *_at_Resource javax.transaction.UserTransaction utx;*

  @EJB private MyDAO myDAO;

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

Thanks,
Sahoo

Josh Donkersloot wrote:
> Thank you for your suggestions,
> I tried, as you suggested, changing the getRecord method as shown below.
> -------------------------
> @Stateless
> public class MyDAOBean
> extends GenericEJB3DAO<Record, Integer>
> implements MyDAO {
>
>
> @TransactionAttribute(TransactionAttributeType.REQUIRED)
> public Record getRecord(){ Criteria criteria =
> ((org.hibernate.ejb.HibernateEntityManager)em).getSession()
> .createCriteria(getPersistentClass());
> criteria.setMaxResults(1);
> Record toReturn =
> (Record)criteria.uniqueResult(); return toReturn;
> }
> }
> ---------------------------
> But the results were the same as before. The web service seems to be
> "one step" behind, always writing out the value that was set during
> the previous call to the method.
>
> All I would like to be able to do is just a single transaction per
> http request on my web service?
>
> Thanks you kindly for you help,
> Josh
>
> Sahoo wrote:
>> What is the transaction attribute of /MyDAO.getRecord()/? Setting it
>> to REQUIRED should give the desired result.
>>
>> Sahoo
>> Josh Donkersloot wrote:
>>> 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" );
>>> }
>>>
>>> }
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
>>> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
>> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>