-->
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.  [ 9 posts ] 
Author Message
 Post subject: Transactions and Saving Transient Objects
PostPosted: Mon Sep 04, 2006 3:28 pm 
Newbie

Joined: Mon Sep 04, 2006 3:09 pm
Posts: 5
Hi, here is my scenario. Points will be given fairly!

Simply, the question is, why in my configuration, does the session.save method call not obey transactions for transient objects?


here are the details:

I created a new transitive object (Parent), and this transient object has a one to many mapping to another object (Child).

Here are my mappings:
Parent:

<set name="children" inverse="true" cascade="all-delete-orphan">
<key column="parent_uid"/>
<one-to-many class="Child"/>
</set>

Child:

<many-to-one name="parent" class="Parent">
<column name="parent_uid" not-null="true"/>
</many-to-one>

Note the *not-null* attribute for child.

This is what I did in my java code.
try{
s = sessions.openSession();
t = s.beginTransaction();
Parent parent = new Parent();
Child child = new Child();
parent.addChild(child);
s.save(parent); <--- throws an exception.
t.commit(); <--- never executed
}catch(Exception e){
if(t!=null)
t.rollback(); <--- this gets executed.
}

Notice that the s.save(parent) line of code throws an exception because I did not call child.setParent(parent). The exception thrown looks like this:

org.hibernate.PropertyValueException: not-null property references a null or transient value: Child.parent

Again, my question is, why is the parent object saved into the DB even though the transaction was never committed, rather rolled back? It seems like session.save() does not obey transactions when saving transient objects!

I have verified that transactions do work correctly in other cases, such as when updating detached objects. It only doesn't work for new transient objects.

I am using a nonmanaged environment with JDBC, the hibernate properties file is as follows. Thanks in advance!



hibernate.properties:

hibernate.dialect = org.hibernate.dialect.MySQLMyISAMDialect
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://localhost/SOMETABLE
hibernate.connection.username = SOMEUSER
hibernate.connection.password = SOMEPASSWORD
hibernate.c3p0.min_size=2
hibernate.c3p0.max_size=5
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=20
hibernate.show_sql=true
hibernate.transaction.factory_class=org.hibernate.transaction.JDBCTransactionFactory
hibernate.current_session_context_class=thread

_________________
SNOP!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 05, 2006 3:16 am 
Newbie

Joined: Mon Sep 04, 2006 3:09 pm
Posts: 5
I was able to answer my own question.

the solution was to set FlushMode.commit on my session.

Now, I am trying to figure out why transient-entity-saving does not obey transaction rules. This seems silly imo.

_________________
SNOP!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 05, 2006 3:38 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
it's silly to rely on transactions on a db that doesn't support them ;)

(innodb tables is the key to do that in mysql)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 05, 2006 4:09 am 
Newbie

Joined: Mon Sep 04, 2006 3:09 pm
Posts: 5
ok, so if I had used a different db like innodb, then leaving FlushMode=AUTO would not have persisted the transient object to the database?

GAH!

I guess that means with InnoDB, my Session.save() would have made an 'insert' within an InnoDB transaction, and rolling back would have correctly rolled back the transaction.

Also, since I'm not using a transactional database, I might as well not use hibernate transactions, and just set FlushMode=COMMIT or NEVER and manually call Session.flush() whenever I am done with an "atomic unit" of work.

Is this correct?

Thanks in advance!

_________________
SNOP!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 05, 2006 4:31 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
of course it would have persisted it to the database (otherwise queries in the same transaction wouldn't be able to see the data).

The issue is that rollback on non-innodb on mysql does not actually result in any rollback...why don't you just use transactions?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 05, 2006 11:56 am 
Newbie

Joined: Mon Sep 04, 2006 3:09 pm
Posts: 5
Quote:
of course it would have persisted it to the database (otherwise queries in the same transaction wouldn't be able to see the data).[/quotee]

Well... it's *possible* that hibernate have the data not persist to the database and still let the application see the data. So, I disagree with the "of course" part of this statement. =)

Quote:
The issue is that rollback on non-innodb on mysql does not actually result in any rollback...why don't you just use transactions?


I've switched over to InnoDB.

_________________
SNOP!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 05, 2006 4:16 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Jae wrote:
Quote:
of course it would have persisted it to the database (otherwise queries in the same transaction wouldn't be able to see the data).


Well... it's *possible* that hibernate have the data not persist to the database and still let the application see the data. So, I disagree with the "of course" part of this statement. =)


tell me how queries will be able to return the valid results if your changes is not put in the database ? :)

Quote:
Quote:
The issue is that rollback on non-innodb on mysql does not actually result in any rollback...why don't you just use transactions?


I've switched over to InnoDB.


congratulations ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 05, 2006 8:01 pm 
Newbie

Joined: Mon Sep 04, 2006 3:09 pm
Posts: 5
Quote:
tell me how queries will be able to return the valid results if your changes is not put in the database ? :)


well... I was *thought* that hibernate would do something intelligent with the queries, like for select queries, query both the backend DB (in my previous case, the MyIsamDB) and also run the query with the objects in session/transaction scope, and do a merge on both results... something *crazy* and *magical* like that. I guess Hibernate just can't handle that huh?

:)

_________________
SNOP!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 05, 2006 8:14 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
nope, hibernate is not a database.

_________________
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.  [ 9 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.