-->
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.  [ 4 posts ] 
Author Message
 Post subject: Transaction rollback does not work
PostPosted: Sun Jul 10, 2005 9:58 pm 
Newbie

Joined: Fri May 20, 2005 3:05 am
Posts: 3
I use the session.beginTransaction() to start a new transaction.
After I save an object, and call transaction.rollback(), it works fine.
But if I try to run some query before that, the object is saved into database regardless I called rollback() or commit() later.

I'm using JDK1.5.0, Hibernate 2.1.7 and MySQL 4.1

Thank you for your help!

The code:
=======================================

Properties cp = new Properties();
cp.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
cp.setProperty("hibernate.connection.url", "jdbc:mysql://localhost/server1?autoReconnect=true");
cp.setProperty("hibernate.connection.username", "localadmin");
cp.setProperty("hibernate.connection.password", "localadmin");
cp.setProperty("hibernate.connection.pool_size", "10");
cp.setProperty("hibernate.dialect", "net.sf.hibernate.dialect.MySQLDialect");
cp.setProperty("show_sql", "true");
cp.setProperty("hibernate.cache.provider_class", "net.sf.ehcache.hibernate.Provider");
cp.setProperty("jdbc.use_scrollable_resultset", "true");
sessionFactory = new Configuration().addProperties(cp).configure().buildSessionFactory();

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Employee ts = new Employee();
ts.setEmployeeCode("dec");
ts.setEmployeeName("dec");
session.save(ts);

/* The following 2 lines cause the problem */
Query query = session.createQuery("select e from Employee as e");
List result = query.list();



tx.rollback();

session.close();


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 11, 2005 12:24 am 
Beginner
Beginner

Joined: Fri Jul 08, 2005 12:38 pm
Posts: 41
Location: Massachusetts, USA
Refer to http://www.hibernate.org/hib_docs/v3/reference/en/html/objectstate.html#objectstate-flushing.

The session may be flushed before the transaction is explicitly committed. One trigger is some queries. The documentation discusses how to change the flush mode to prevent this from happening.

However, I think you should consider not executing the query during the transaction (and therefore not needing to tinker with the flush mode). Queries can make a transaction take longer, and your thread may use more than its fair share of resources.[/url]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 11, 2005 12:55 am 
Newbie

Joined: Fri May 20, 2005 3:05 am
Posts: 3
Thank you. It helps.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 11, 2005 10:31 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Make sure your using Innodb tables


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