-->
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.  [ 1 post ] 
Author Message
 Post subject: Transaction question
PostPosted: Wed Jun 01, 2005 3:23 pm 
Beginner
Beginner

Joined: Tue Dec 21, 2004 11:53 am
Posts: 42
Hibernate version:
2.1.7

Hello there! I always thought that I was doing the things right using transactions. My HibernateDAO has this pattern:
Code:
public void update(Entity obj) throws PersistenceException {
      Session session = getCurrentSession();
      beginTransaction();
      try {
         session.update(obj);
      } catch (HibernateException e) {
         logger.error(e);
         rollbackTransaction();
         throw new PersistenceException(e);
      } finally {
         commitTransaction();
         closeSession();
      }
   }

public static Session getCurrentSession(){
      Session s = (Session)threadSession.get();
      if(s == null){
         try{
            s = sessionFactory.openSession();
            threadSession.set(s);
         }catch(HibernateException e){
            e.printStackTrace();
            throw new RuntimeException(e);
         }
      }
      return s;
   }
   
   public static void closeSession(){
      Session s = (Session)threadSession.get();
      threadSession.set(null);
      if(s!= null && s.isOpen()){
         try {
            s.close();
         } catch (HibernateException e) {
            throw new RuntimeException(e);
         }
      }
   }
   
   public static void beginTransaction() {
      Transaction tx = (Transaction) threadTransaction.get();
      try {
         if (tx == null) {
            tx = getCurrentSession().beginTransaction();
            threadTransaction.set(tx);
         }
      } catch (HibernateException ex) {
      }
   }
   
   public static void commitTransaction() {
      Transaction tx = (Transaction) threadTransaction.get();
      try {
         if ( tx != null && !tx.wasCommitted()
               && !tx.wasRolledBack() )
            tx.commit();
         threadTransaction.set(null);
      } catch (HibernateException ex) {
         rollbackTransaction();
         throw new RuntimeException(ex);
      }
      
   }
   public static void rollbackTransaction() {
      Transaction tx = (Transaction) threadTransaction.get();
      try {
         threadTransaction.set(null);
         if ( tx != null && !tx.wasCommitted()
               && !tx.wasRolledBack() ) {
            
            tx.rollback();
         }
      } catch (HibernateException ex) {
         throw new RuntimeException(ex);
      } finally {
         closeSession();
      }
   }



All methods looks like these. Well I was happy. was...

Untill I deal with this:
(while it.hasNext()){
Pojo pojo = (Pojo)it.next();
//do some fancy code here
dao.update(pojo);
}

So, at a given point, an error happens. Well, with this type of code, all the previous iterations changed the database. Something totally undesirable. Even setRollBackOnly() (I'm using SLSBs) can't do anything (pretty obvious, since I've commited every unit)

Anyway, which is the right way to perform a single block of transaction? To be a real ACID application?

Best regards

Vinicius


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.