-->
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: Issue with Transactions (New User)
PostPosted: Sat Mar 19, 2005 10:08 pm 
Newbie

Joined: Sat Mar 19, 2005 9:56 pm
Posts: 2
I am new user and having an issue with rolling back transactions in Hibernate. I started with the simple example for storing cat objects (with fields name, sex, weight). I am using the source code below for testing transactions. I start a transaction, create a cat named fred, do a query for cats, and print the results and it shows Fred. Then, I create a second cat named Wally, rollback the transaction, do a query for cats, and it lists 1 cat, Wally, even though I have rolled back the transaction. After the program completes, the transaction does appear to be rolled back as the database table has no entries in it.

Can someone explain why my second query gets the one cat back even though I rolled back the transaction? Is the transaction not rolled back until I close the session or is there something about the objects being cached in the session?

Thanks in advance!

Robert

==============SOURCE=====================
SessionFactory sessionFactory =
new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();

System.out.println("Beginning transaction");
Transaction tx = session.beginTransaction();

System.out.println("Inserting Fred");
Cat fred = new Cat();
fred.setName("Fred");
fred.setSex('M');
fred.setWeight(14.4f);
session.save(fred);

System.out.println();
System.out.println("Listing Cats");
Query query = session.createQuery("from Cat");
List cats = query.list();
for (int i = 0; i < cats.size(); i++) {
System.out.println(cats.get(i));
}
System.out.println();

System.out.println("Inserting Wally");
Cat wally = new Cat();
wally.setName("Wally");
wally.setSex('F');
wally.setWeight(7.4f);
session.save(wally);

System.out.println("Rolling back transaction");
tx.rollback();

System.out.println();
System.out.println("Listing Cats");
query = session.createQuery("from Cat");
cats = query.list();
for (int i = 0; i < cats.size(); i++) {
System.out.println(cats.get(i));
}

session.close();

==============OUTPUT=====================
Beginning transaction
Inserting Fred

Listing Cats
(Fred, M, 14.4)

Inserting Wally
Rolling back transaction

Listing Cats
(Wally, F, 7.4)
=========================================


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 20, 2005 7:11 am 
Newbie

Joined: Sun Mar 20, 2005 6:35 am
Posts: 3
Dear Robert,

When you use rollback, hibernate roll back the changes made to the persistent object(here it is cat) in this transaction at the database level.Hibernate does not rollback the changes made to the persistance objects in the memory.

That is why when you are performing the following
Cat wally = new Cat();
wally.setName("Wally");
wally.setSex('F');
wally.setWeight(7.4f);
session.save(wally);

you are making the changes to objects which is cached by the session(i,e in-memory).As mentioned when you roll back the transaction hibernate rollback the changes in the database level, thats why for the following
query = session.createQuery("from Cat");
cats = query.list();
for (int i = 0; i < cats.size(); i++) {
System.out.println(cats.get(i));
}

you are able to see the object cached by the session.

I hope the above shall address your query.Should there any further queries pls dont hesitate to revert.

Thanks,
Shayanika


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 20, 2005 7:47 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
well it shouldnt happen since we dont perform queries against the session. we only use the session to look up by id...

do you have query caching enabled ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 20, 2005 7:49 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
oooh sorry - got it.

you have not flused the session so the Wally is not stored before after the rollback when you do the query - and thus wally is in the db at query time.

He will then go away again since you dont call commit.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Thanks for reply, how to handle session caching?
PostPosted: Sun Mar 20, 2005 10:57 am 
Newbie

Joined: Sat Mar 19, 2005 9:56 pm
Posts: 2
Thanks for the replies. I think it makes sense why this happens, but what is required such that I don't get the Cat object cached in the session? I tried flushing the session right after the rollback call and before doing the query to get the Cat objects, but I still get the 1 cat Wally. Do I need to use a new session object?

Below is my hibernate.cfg.xml, in case the configuration matters.

Thanks!

Robert

===========================================
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>
<session-factory>

<!-- Settings for a remote MySQL database. -->
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/aerospace_krisp</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>

<!-- Use the C3P0 connection pool. -->
<property name="c3p0.min_size">3</property>
<property name="c3p0.max_size">30</property>
<property name="c3p0.timeout">1800</property>

<!-- Print SQL to stdout. -->
<property name="show_sql">false</property>

<!-- Mapping files. -->
<mapping resource="org/aero/krisp/hibernate/Cat.hbm.xml"/>
<mapping resource="org/aero/krisp/hibernate/User.hbm.xml"/>
<mapping resource="org/aero/krisp/hibernate/UserGroup.hbm.xml"/>
</session-factory>

</hibernate-configuration>
===========================================


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 20, 2005 10:59 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
do the flush BEFORE the rollback

_________________
Max
Don't forget to rate


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.