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: Session life cycle and committing to DB
PostPosted: Fri Jun 08, 2007 12:35 pm 
Newbie

Joined: Sat May 19, 2007 12:02 pm
Posts: 19
From my understanding to the life cycle of the session, when I call save(), update(), saveOrUpdate(), .... etc. the object is not really saved to the DB. It's only persisted in the persistence context. Assuming I modified many objects and I need to save these modification after I confirmed I am happy with the results, how do I achieve this?

One way I found is to create a method called synchWithDb() which will create a transaction:
Code:
public void  synchWithDb()
   {
      Transaction tx = currentSession().beginTransaction();
      tx.commit();
   }

Then I call this method. I don't know if this is the proper way. Any one can give me comments ?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 10, 2007 8:49 am 
Regular
Regular

Joined: Fri May 12, 2006 4:05 am
Posts: 106
??? But there should already be some transaction running, so why do you have to create a new one???

If you don't necessarily want to commti your transaction but want to synchronise the DB with the Perstistence-Context just call session.flush().


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 10, 2007 1:44 pm 
Newbie

Joined: Sat May 19, 2007 12:02 pm
Posts: 19
Thank you for replying. I thought flush is only to move the changes to the persistence Context. The reason I am creating new transaction is because the generated code from hbm2java produces this:


Code:

   public void attachDirty(Account instance) {
      log.debug("attaching dirty Account instance");
      try {
         HibernateUtil.currentSession().saveOrUpdate(instance);
         log.debug("attach successful");
      } catch (RuntimeException re) {
         log.error("attach failed", re);
         throw re;
      }
   }

   public void attachClean(Account instance) {
      log.debug("attaching clean Account instance");
      try {
         HibernateUtil.currentSession().lock(instance, LockMode.NONE);
         log.debug("attach successful");
      } catch (RuntimeException re) {
         log.error("attach failed", re);
         throw re;
      }
   }

   public void delete(Account persistentInstance) {
      log.debug("deleting Account instance");
      try {
         HibernateUtil.currentSession().delete(persistentInstance);
         log.debug("delete successful");
      } catch (RuntimeException re) {
         log.error("delete failed", re);
         throw re;
      }
   }

   public Account merge(Account detachedInstance) {
      log.debug("merging Account instance");
      try {
         Account result = (Account) HibernateUtil.currentSession().merge(
               detachedInstance);
         log.debug("merge successful");
         return result;
      } catch (RuntimeException re) {
         log.error("merge failed", re);
         throw re;
      }
   }



This is the code from the DAO. I couldn't find any tutorial about using it from the business layer. So I added synchWithDb() method, so that I can commit the changes to the DB. I tried flush but no changes on the DB side.


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.