-->
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.  [ 39 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: Transaction.rollback() does not work?
PostPosted: Wed Jan 14, 2004 4:59 pm 
Regular
Regular

Joined: Fri Aug 29, 2003 12:48 pm
Posts: 63
Hiya. I'm having some trouble rolling back a transaction - it fails to work as expected. In my code, I'm reading in a bunch of data and creating a set of persistent objects. Sometimes the data is malformed, and the import utility tosses an IllegalStateException. I catch the exception and roll back the transaction, but look in the database and see that the partial data exists therein! Here's my code sample:

Code:
Session session = objectContext.openSession();
Transaction tx = null;
try {
    tx = session.beginTransaction();
    // delete any studies that already exist that have the same name
    String name = studyElement.attributeValue("name");
    List studies = session.find("select from Study as study where study.name = ?", name, Hibernate.STRING);
    if (!studies.isEmpty()) {
        for (int i = 0; i < studies.size(); i++) {
            Study study = (Study)studies.get(i);
            log.debug("deleting study named: " + study.getName());
            session.delete(study);
        }
    }
    tx.commit();
    tx = null;
    tx = session.beginTransaction();
    Study study = new Study();
    // set study properties
    session.save(study);
    // create study children and save them, associate with study, etc., etc.
    tx.commit();
} catch (Exception e) {
    log.error("Error importing data", e);
    if (tx != null) {
        log.debug("Rolling back...");
        tx.rollback();
        log.debug("done rolling back: "+tx.wasRolledBack());
    }
    throw e;
} finally {
    session.close();
}


Any clues as to what I'm doing wrong? fwiw, I'm running this against a mysql database, let's see, 3.23.53 on a win32 box.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2004 5:00 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
inno-db aware ?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2004 5:07 pm 
Regular
Regular

Joined: Fri Aug 29, 2003 12:48 pm
Posts: 63
apparantly not. are you suggesting that my mysql database, as its currently configured, doesn't support transactions. fair enough, if that's the case, but shouldn't hibernate either use its own transaction objects, or if it doesn't have them, toss an exception when i try to open a transaction?

pointers to educational material would be great. otherwise i'll google away.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2004 5:14 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I don't know much of mysql (just that users complains about tx :) ).
You have to specify inno-db during table creation, and allow inno-db in mysql config file (at least). Google it, I told you all I know

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2004 8:41 pm 
Newbie

Joined: Thu Jan 08, 2004 6:54 pm
Posts: 8
Try starting w/ InnoDB and test it. You could also upgrade MySQL to 4+. Inno-DB is automatically used in this version(s) when you start w/ mysqld.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2004 11:31 pm 
Beginner
Beginner

Joined: Tue Dec 30, 2003 1:51 pm
Posts: 49
Location: Michigan, USA
if your using a linux version, i believe its mysql 3.2.x and you do have to call innodb when making a table. if you need a good gui for it try http://www.webmin.com

upgrading to 4 removes this issue, but there must be a way to have schemaexport generate it for you

jason


Top
 Profile  
 
 Post subject: using MySQL 4+, InnoDB tables are turned on - no rollback
PostPosted: Tue Jan 27, 2004 8:40 am 
Newbie

Joined: Thu Oct 09, 2003 3:44 pm
Posts: 18
I am getting this same problem.

To test I am forcing an error and aborting and the erronious data is still there in MySQL.

I am using MySQL 4+, InnoDB tables are turned on.

have updated the MySQL driver to mysql-connector-java-3.0.10-stable and hibernate to version 2.1.1

all help welcomed

dave

ps: to get the hibetnate tool to automatically make the tables InnoDB do this in ant. (note the --delimiter=type=InnoDB)

Code:
   <target name="initdb"
      description="builds the tables we need in the database">
      <java classname="net.sf.hibernate.tool.hbm2ddl.SchemaExport" fork="true">
         <arg value="--config=/hibernate.cfg.xml"/>
         <arg value="--delimiter=type=InnoDB"/>
         <classpath>
            <pathelement location="${dist.dir}/${jar.file}" />
            <path refid="classpath" />
         </classpath>
      </java>
   </target>



Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 8:41 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I can hardly believe that. Please show your code.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 8:48 am 
Newbie

Joined: Thu Oct 09, 2003 3:44 pm
Posts: 18
gloeglm wrote:
I can hardly believe that. Please show your code.


okay. this is extracted from my JUnit tests. the SESSION is built and destroed in the JUnit test's setup and teardown methods.

Code:
public void testCreateExistsLoginEditAndDeleteSubscriber() {
   logger.debug("testing rollback");
   Transaction tx = null;
   try {
      tx = SESSION.beginTransaction();
      // create
      Subscriber me = new Subscriber("Testing","Test",TEST_USERNAME,TEST_PASSWORD);
      me.setEmail("test@testing.test");
      Long id = SubscriberPeer.save(SESSION,me);

      // force an error to get it to abort.
      assertNull("id was null.",id);

// snip out irrelevent code

      tx.commit();
   } catch (Error er) {
      // abort the transaction.
      logger.error("caught an error: "+er.getMessage());
      try {
         if (tx != null) {
            logger.debug("rolling back transaction after an error");
            tx.rollback();
         }
      } catch (HibernateException he) {
         logger.error("HibernateException while aborting transaction. "+
                                             he.getMessage());
      }
      throw er;
   } catch (Exception ex) {
      // abort the transaction.
      logger.error("caught an exception: "+ex.getMessage());
      try {
         if (tx != null) {
            logger.debug("rolling back transaction after an exception");
            tx.rollback();
         }
      } catch (HibernateException he) {
         logger.error("HibernateException while aborting transaction. "+
                                             he.getMessage());
      }
   }

}


after the abort the test user I have created is still hanging around in the database.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 8:55 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Are you reusing the session after the rollback? You should not do that. (And this time I am pretty sure)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 9:09 am 
Newbie

Joined: Thu Oct 09, 2003 3:44 pm
Posts: 18
gloeglm wrote:
Are you reusing the session after the rollback? You should not do that. (And this time I am pretty sure)


no. the session is closed after the test by the test case's teardown method. unless by use you mean closing the session.

Code:
public void tearDown() throws Exception {
   super.tearDown();
   SESSION.close();
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 9:12 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Please show the log excerpt.


Top
 Profile  
 
 Post subject: autocommit
PostPosted: Tue Jan 27, 2004 9:19 am 
Newbie

Joined: Thu Oct 09, 2003 3:44 pm
Posts: 18
I think we have isolated the problem. the autocommit falg in MySQL was set to true, and this needs to be set to false for th etransaction stuff to work correctly. we just tried manually createing some data using the mysql command line client and then rolling it back with the same results as our java tests. so the problem was not hibernate but mysql. now have to work out how to tell hibernate to tell mysql to make autocommit false.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 9:20 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I hate that f+++++ing mysql.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 9:23 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
by the way, wasn't hibernate supposed to set autocommit=false by itself?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 39 posts ]  Go to page 1, 2, 3  Next

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.