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.  [ 3 posts ] 
Author Message
 Post subject: DAO and transactions
PostPosted: Tue Oct 26, 2004 9:55 am 
Newbie

Joined: Tue Oct 26, 2004 3:25 am
Posts: 2
Hi,

I have a design issue implementing my DAO's. I have seen in examples and on the net that a DAO method could typically looks like :

Code:
public void saveEntity(myDTO daily) throws DAOException {
   try {   
       ... do something ...
   } catch (HibernateException e) {
     throw new DAOException(e);
   } finally {
     ... close session ...
   }
}


using JBOSS with JTA transaction, it works great.

But when I write my JUnits tests, I have to enclose my DAO's method call within a 'session.beginTransaction()' and a 'transaction.commit()' . The problem is that the 'commit' fails since the session has been closed within DAO method.

other way, if I do not close the session in DAO, a warning appears when using it in JBOSS (session not properly closed).


Thanks for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 26, 2004 10:01 am 
Senior
Senior

Joined: Fri Jun 18, 2004 10:17 am
Posts: 140
i thought the point of a dao is to conceal your persistence mechanism, so the session begin and commit are part of what the DAO does, so you put them in the method, e.g

Code:
public void saveEntity(myDTO daily) throws DAOException {
   try {   
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
       ... do something ...
tx.commit();
HibernateUtil.closeSession(session);   } catch (HibernateException e) {
     throw new DAOException(e);
   } finally {
     ... close session ...
   }
}


have i got the wrong end of the stick with what you are doing?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 26, 2004 10:16 am 
Newbie

Joined: Tue Oct 26, 2004 3:25 am
Posts: 2
adcworks wrote:
i thought the point of a dao is to conceal your persistence mechanism, so the session begin and commit are part of what the DAO does, so you put them in the method, e.g


You probably right :-) i haven't seen the problem this way, since i already define transaction in EJB descriptors.



Thank you.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.