-->
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.  [ 7 posts ] 
Author Message
 Post subject: Transaction Rollback
PostPosted: Tue Feb 23, 2010 1:15 pm 
Newbie

Joined: Tue Feb 23, 2010 12:53 pm
Posts: 5
Hello, i'm new in Hibernate. I'm trying to figure out how Hibernate suppose to rollback transactions.
Code:
Session session = HibernateUtil.getSession();
session.beginTransaction();
User user = new User();
user.setUserName("userName");
session.save(user);
session.flush();
session.getTransaction().rollback();
  session.close();


As i understand the code above should rollback transaction and user would not be created in database. But after executing this code, i can see real changes in database. Even if i comment session.flush() i can see changes in database.

Toggeling rollback/commit in the code below works fine. Commit - make real changes, rollback does not.
Code:
Session session = HibernateUtil.getSession();
session.beginTransaction();
User uu = (User)session.load(User.class, new Long(99));
uu.setUserName("new name");
session.getTransaction().rollback();
session.close();


It's like calling session.save() or session.flush() method commit my transaction automaticity. Is it normal ?

Any help will be highly appreciated


Top
 Profile  
 
 Post subject: Re: Transaction Rollback
PostPosted: Tue Feb 23, 2010 3:00 pm 
Beginner
Beginner

Joined: Tue Aug 25, 2009 11:42 am
Posts: 49
U may have ur Hibernate Session configured to auto commit (not the hibernate session itself but the underlying jdbc connection):
<property name="connection.autocommit">true</property>
switch it to false.
Also, try to have a handle on the transaction instead of using the getTransaction() method so that u know exactly what transaction u are dealing with (depending upon how its configured u might have received a brand new transaction in that statement of yours - I am not sure if this is possible but looks like that's what happened - debug and let us know if u indeed got a new transaction):

Transaction tx = session.beginTransaction();
...
tx.rollback();

Lastly, I believe u are certain no exceptions were thrown.


Top
 Profile  
 
 Post subject: Re: Transaction Rollback
PostPosted: Wed Feb 24, 2010 3:25 pm 
Newbie

Joined: Tue Feb 23, 2010 12:53 pm
Posts: 5
I'm not using this property<property name="connection.autocommit">true</property>.
Also i'm aboslutely sure with what transaction i deal with.

Any clue ?


Top
 Profile  
 
 Post subject: Re: Transaction Rollback
PostPosted: Wed Feb 24, 2010 5:33 pm 
Beginner
Beginner

Joined: Tue Aug 25, 2009 11:42 am
Posts: 49
please attach all ur code/config if possible


Top
 Profile  
 
 Post subject: Re: Transaction Rollback
PostPosted: Fri Feb 26, 2010 5:56 pm 
Newbie

Joined: Tue Feb 23, 2010 12:53 pm
Posts: 5
hibernate.cfg.xml
Code:
   <session-factory>
      <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="connection.username">admin</property>
      <property name="connection.password">admin</property>
      <property name="connection.url">jdbc:mysql://localhost/my_db</property>
      <property name="connection.current_session_context_class">thread</property>
      <property name="connection.pool_size">5</property>
      <property name="connection.dialect">org.hibernate.dialect.HSQLDialect</property>
      
      <property name="hbm2ddl.auto">update</property>
      <property name="show_sql">true</property>
      
      
      <mapping package="...."/>
                 .. etc
   </session-factory>


Code:
   @Test
   public void hibernateRollback() {
      
      Session session = HibernateUtil.getSession();

      Transaction tr1 = session.beginTransaction();
      User user = new User();
      user.setUserName("aaa");
      System.out.println("save user");
      session.save(user);
      System.out.println("afetr save user");
      tr1.rollback();
      session.close();
   }

And i can see this in console output :
save user
Hibernate: insert into t_user (birthdayDate, password, userName) values (?, ?, ?)
afetr save user

Thank you


Top
 Profile  
 
 Post subject: Re: Transaction Rollback
PostPosted: Fri Feb 26, 2010 6:53 pm 
Newbie

Joined: Tue Feb 23, 2010 12:53 pm
Posts: 5
I can't figure out what's going on. I just created new database with same configuration and transaction rolled back fine. When i switch to previuos database with absolutely same configuration(change just database name in hibernate.config) transactions does not rollback. Tested it 5-6 times it is driving me mad


Top
 Profile  
 
 Post subject: Re: Transaction Rollback
PostPosted: Fri Feb 26, 2010 7:21 pm 
Newbie

Joined: Tue Feb 23, 2010 12:53 pm
Posts: 5
The absense of this property cause this problem -
Code:
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>


The worst thing, even you specify <property name="hbm2ddl.auto">update</property> and add dialect property to your configuration file, transactions would not still rollback. So you have to drop database implictly.


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