-->
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: Rollback not working with createQuery
PostPosted: Wed Mar 10, 2010 12:44 am 
Newbie

Joined: Wed Mar 10, 2010 12:30 am
Posts: 2
I'm having trouble with rollbacks not getting rolled back. I'm using hibernate3 with mysql. I have a simple table, call Person, it only has one column, which is the person's name. This is the stripped down version of the code Im using:

Code:
SessionFactory factory = HibernateConfiguration.getSessionFactory();

Session session = factory.openSession();
session.beginTransaction();
Person p1 = new Person();
Person p2 = new Person();
Person p3 = new Person();
p1.setName("Bill");
p2.setName("Hank");
p3.setName("Dale");
session.save(p1);
session.save(p2);
session.save(p3);
session.getTransaction().commit();
session.close();

System.out.println("BREAKPOINT1");

session = factory.openSession();
session.beginTransaction();
session.createQuery("delete from Person").executeUpdate();
session.getTransaction().rollback();
session.close();

System.out.println("BREAKPOINT2");


I run this code through the java debugger, so at breakpoint1, it saves 3 persons to the database, Hank, Bill and Dale. At this time, I manually run a select query on mysql server, and I see the 3 rows, everything looks ok.

The second part of the code deletes all the rows, then changes its mind and does a rollback. But this rollback doesn't work, as the rows in the database gets deleted when this code finishes. Can anyone help me debug this, what's wrong with my code?


Top
 Profile  
 
 Post subject: Re: Rollback not working with createQuery
PostPosted: Wed Mar 10, 2010 2:04 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
Just make sure that you use INNODB in mysql instead of MYISAM


Top
 Profile  
 
 Post subject: Re: Rollback not working with createQuery
PostPosted: Wed Mar 10, 2010 3:55 pm 
Newbie

Joined: Wed Mar 10, 2010 12:30 am
Posts: 2
Thanks for the hint! I was using Innodb on the mysql backend. I did find the fix when googling around, so I'll share for other people. Seems like there are more than one mysql dialect. I was using MySQLDialect dialect, which works for the most part except for this rollback issue.

Code:
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>



The fix was to use the MySQLInnoDBDialect dialect, which seems to fix this rollback issue.
Code:
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>


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.