-->
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.  [ 6 posts ] 
Author Message
 Post subject: Transaction rollback doesn't work, why?
PostPosted: Thu Aug 26, 2004 11:51 am 
Newbie

Joined: Tue Jul 20, 2004 7:03 am
Posts: 19
Location: Spain
Hi all.

I'm doing a simple transaction test case, the objective is

1. Save two objects, one simple, one complex
2. Before committing the transaction, hit rollback
3. Test these objects are not really saved into the DB

Well, this is simple but doesn't work in my environment. Here is the trace and code, i'm sure i'm doing something wrong :(((

Test method
Code:
   public void testTransaction() throws HibernateException, InterruptedException {

      HibernateUtil.beginTransaction();
      Session s = HibernateUtil.getSession();

      VersionedContent vc = new VersionedContent();
      Content published = new Content();
      Content draft = new Content();

      vc.setDraft(draft);
      vc.setPublished(published);
      String author = "transactionTest";
      vc.setPublishedBy(author);
      draft.setAuthor(author);
      published.setAuthor(author);
      
      Thread.sleep(1000);
      Role role = new Role();
      role.setName(""+new java.util.Date().getTime());
      

      // salvamos y rollback
      try {
         s.save(vc);
         s.save(role);
         ExceptionThrower.throwHibernateExteption();
      } catch (HibernateException e) {
         HibernateUtil.rollbackTransaction();
         com.oneplanettravel.util.log.LogUtil.getDebugLogger().debug("", e);
      } finally {
         HibernateUtil.closeSession();
      }

      try {
         // ahora esos objetos no deberian existir !!
         HibernateUtil.beginTransaction();
         s = HibernateUtil.getSession();
         boolean isEmptyList =             s
         .createQuery("from VersionedContent as c where c.publishedBy=:author")
         .setParameter("author", author)
         .list()
         .isEmpty();

         HibernateUtil.commitTransaction();


         Assert.assertEquals(true,isEmptyList);
      } catch (HibernateException e) {
         HibernateUtil.rollbackTransaction();
         com.oneplanettravel.util.log.LogUtil.getDebugLogger().debug("", e);
      } finally {
         HibernateUtil.closeSession();
      }

   }


HibernateUtil Class relevant code
Code:
// init --------------------------------
   static {
      try {
         configuration = new Configuration();
         sessionFactory = configuration.configure().buildSessionFactory();
         // We could also let Hibernate bind it to JNDI:
         // configuration.configure().buildSessionFactory()
      } catch (Throwable ex) {
         // We have to catch Throwable, otherwise we will miss
         // NoClassDefFoundError and other subclasses of Error
          log.debug("Building SessionFactory failed.", ex);
         throw new ExceptionInInitializerError(ex);
      }
   }


// methods --------------------------

   /**
    * Retrieves the current Session local to the thread.
    * <p/>
    * If no Session is open, opens a new Session for the running thread.
    *
    * @return Session
    */
   public static Session getSession()
      throws InfrastructureException {
      Session s = (Session) threadSession.get();
      try {
         if (s == null) {
            log.debug("Opening new Session for this thread.");
            if (getInterceptor() != null) {
               log.debug("Using interceptor: " + getInterceptor().getClass());
               s = getSessionFactory().openSession(getInterceptor());
            } else {
               s = getSessionFactory().openSession();
            }
            try {
               log.debug("Connection Autocommit status = " + s.connection().getAutoCommit());
            } catch (Throwable e) {
               // TODO Auto-generated catch block
               log.debug("", e);
            }
            threadSession.set(s);
            log.debug("Session opened with hash "+s.hashCode());
            log.debug("\t\t\tvvvvvv Session vvvvvv");
            sessionsOpened++;
            log.debug("Total opened sessions = "+sessionsOpened);
         }
      } catch (HibernateException ex) {
         throw new InfrastructureException(ex);
      }
      return s;
   }

   /**
    * Closes the Session local to the thread.
    */
   public static void closeSession()
      throws InfrastructureException {
      try {
         Session s = (Session) threadSession.get();
         threadSession.set(null);
         if (s != null && s.isOpen()) {
            log.debug("Closing Session of this thread. Session hash "+s.hashCode());
            s.close();
            sessionsOpened--;
            log.debug("Total opened sessions = "+sessionsOpened);
            log.debug("\t\t\t^^^^^^ Session ^^^^^^");
         }
      } catch (HibernateException ex) {
         throw new InfrastructureException(ex);
      }
   }

   /**
    * Start a new database transaction.
    */
   public static void beginTransaction()
      throws InfrastructureException {
      Transaction tx = (Transaction) threadTransaction.get();
      try {
         if (tx == null) {
            log.debug("Starting new database transaction in this thread. Session hash "+getSession().hashCode());
            tx = getSession().beginTransaction();
            threadTransaction.set(tx);
            log.debug("Transaction hash "+tx.hashCode());
            log.debug("\t\t\t\t\tvvvvvv Transaction vvvvvv");
         }
      } catch (HibernateException ex) {
         throw new InfrastructureException(ex);
      }
   }

   /**
    * Commit the database transaction.
    */
   public static void commitTransaction()
      throws InfrastructureException {
      Transaction tx = (Transaction) threadTransaction.get();
      try {
         if ( tx != null && !tx.wasCommitted()
                     && !tx.wasRolledBack() ) {
            tx.commit();
            log.debug("Committing database transaction of this thread. Transaction hash "+tx.hashCode());
         }
         threadTransaction.set(null);
          log.debug("\t\t\t\t\t^^^^^^ Transaction ^^^^^^");
      } catch (HibernateException ex) {
         rollbackTransaction();
         throw new InfrastructureException(ex);
      }
   }

   /**
    * Commit the database transaction.
    */
   public static void rollbackTransaction()
      throws InfrastructureException {
      Transaction tx = (Transaction) threadTransaction.get();
      log.debug("\t^^^^^^ **** Transaction rollback **** ^^^^^^");
      try {
         threadTransaction.set(null);
         if ( tx != null && !tx.wasCommitted() && !tx.wasRolledBack() ) {
            log.debug("Trying to rollback transaction of this thread. Transaction hash "+tx.hashCode()+". Session hash "+((Session) threadSession.get()).hashCode());
            tx.rollback();
         }
      } catch (HibernateException ex) {
         throw new InfrastructureException(ex);
      } finally {
         closeSession();
      }
   }




Output trace
Code:
2004-08-26 15:37:29,765 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:135)     - Opening new Session for this thread.
2004-08-26 15:37:29,828 DEBUG [main] zzz_dev (HibernateUtil.java:144)     - Connection Autocommit status = false
2004-08-26 15:37:29,843 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:150)     - Session opened with hash 29774358
2004-08-26 15:37:29,843 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:151)     -          vvvvvv Session vvvvvv
2004-08-26 15:37:29,843 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:153)     - Total opened sessions = 1
2004-08-26 15:37:29,843 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:189)     - Starting new database transaction in this thread. Session hash 29774358
2004-08-26 15:37:29,843 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:192)     - Transaction hash 28708894
2004-08-26 15:37:29,843 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:193)     -                vvvvvv Transaction vvvvvv
2004-08-26 15:37:30,968 DEBUG [main] zzz_dev (HibernateUtil.java:226)     -    ^^^^^^ **** Transaction rollback **** ^^^^^^
2004-08-26 15:37:30,968 DEBUG [main] zzz_dev (HibernateUtil.java:230)     - Trying to rollback transaction of this thread. Transaction hash 28708894. Session hash 29774358
2004-08-26 15:37:30,984 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:170)     - Closing Session of this thread. Session hash 29774358
2004-08-26 15:37:30,984 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:173)     - Total opened sessions = 0
2004-08-26 15:37:30,984 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:174)     -          ^^^^^^ Session ^^^^^^
2004-08-26 15:37:31,000 DEBUG [main] zzz_dev (HibernateTest.java:140)     -
net.sf.hibernate.HibernateException: Test exception. Ignore it
   at com.zzz.util.test.ExceptionThrower.throwHibernateExteption(ExceptionThrower.java:35)
   ...
2004-08-26 15:37:31,031 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:135)     - Opening new Session for this thread.
2004-08-26 15:37:31,031 DEBUG [main] zzz_dev (HibernateUtil.java:144)     - Connection Autocommit status = false
2004-08-26 15:37:31,031 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:150)     - Session opened with hash 6151022
2004-08-26 15:37:31,031 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:151)     -          vvvvvv Session vvvvvv
2004-08-26 15:37:31,031 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:153)     - Total opened sessions = 1
2004-08-26 15:37:31,109 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:189)     - Starting new database transaction in this thread. Session hash 6151022
2004-08-26 15:37:31,109 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:192)     - Transaction hash 32580443
2004-08-26 15:37:31,109 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:193)     -                vvvvvv Transaction vvvvvv
2004-08-26 15:37:31,390 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:210)     - Committing database transaction of this thread. Transaction hash 32580443
2004-08-26 15:37:31,390 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:213)     -                ^^^^^^ Transaction ^^^^^^
2004-08-26 15:37:31,390 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:170)     - Closing Session of this thread. Session hash 6151022
2004-08-26 15:37:31,390 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:173)     - Total opened sessions = 0
2004-08-26 15:37:31,390 DEBUG [main] persistence.HibernateUtil (HibernateUtil.java:174)     -          ^^^^^^ Session ^^^^^^


Hibernate version: 2.1.6

Mapping documents: Not neccesary

Name and version of the database you are using:MySQL 4.0.18 max-debug



Thanks !!!!!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 27, 2004 4:45 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Are you using innodb tables? You should.


Top
 Profile  
 
 Post subject: Solution
PostPosted: Fri Aug 27, 2004 6:03 am 
Newbie

Joined: Tue Jul 20, 2004 7:03 am
Posts: 19
Location: Spain
That is !

The innodb tables support transaction, so the rollback worked.

As allways, was my failure :)

So, if you want transactions running ok and don't want to read mysql docs, here is the solution:

Code:
CREATE TABLE customers (a INT, b CHAR (20), INDEX (a))  ENGINE=[color=red]InnoDB[/color];


Or

Code:
CREATE TABLE customers (a INT, b CHAR (20), INDEX (a)) TYPE=[color=red]InnoDB[/color];



Thanks all.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 27, 2004 6:04 am 
Newbie

Joined: Tue Jul 20, 2004 7:03 am
Posts: 19
Location: Spain
Of course, forgive the [colour] tag, the html formatting doesn't stack :(


Code:
CREATE TABLE customers (a INT, b CHAR (20), INDEX (a)) TYPE=InnoDB;


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 27, 2004 6:07 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
http://www.hibernate.org/119.html#A8

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: Actually i'm moving to postgreSQL
PostPosted: Mon Aug 30, 2004 5:47 am 
Newbie

Joined: Tue Jul 20, 2004 7:03 am
Posts: 19
Location: Spain
Thanks man

I'm moving to postgreSQL now. I've read the pros and cons and i think my project will need the added features in postgre.
And it's really free, so...


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