-->
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: Are Transactions required?
PostPosted: Sat Feb 28, 2004 9:47 am 
Newbie

Joined: Sat Feb 28, 2004 9:02 am
Posts: 6
I created some code following Nick Heudecker's Introduction to Hibernate tutorial, and nothing happened with my database. The basic code idea was:

Session s = sf.openSession();
s.saveOrUpdate(team);
s.close();

However, if I wrap a Transaction around the saveOrUpdate() and commit it before the session close(), it works.

I got rid of the Transaction and put s.flush() before the s.close() and the database was updated, but in much of the introductory material scattered around the Hibernate website, there are neither Transactions nor Session flushes to be found. Is this a change from previous versions?

So, I guess my questions are:

1) Should I be using Transactions as a matter of course, even for atomic statements?

2) If the answer to (1) is no, do I have to call flush() separately from close(), and, if so, isn't this different from the way Streams and Writers work in the Java classes, and so confusing?

Thanks!

P.S. I followed the Getting Started guide at http://www.hibernate.org/152.html and almost nothing worked in the way it was advertised. I think the implementation and the documentation have parted ways. I suppose that's fine, but it does make life for newbies quite a bit more difficult.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 28, 2004 12:56 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Yes, you should ALLWAYS use transactions!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 28, 2004 12:56 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
There is no SQL execution without transactions, thats a myth usually attributed to "auto commit mode". Read the reference documentation, "Ending a Session".

Can you describe _what_ didn't work?

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 28, 2004 7:31 pm 
Newbie

Joined: Sat Feb 28, 2004 9:02 am
Posts: 6
My entry did not appear in the database is what didn't work.

So, I'm to understand that Transactions are *not* optional.

OK.

Todd


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 29, 2004 9:05 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you can never avoid using *transactions* at the database level - but you can avoid using the Transaction class - just go read the docs about "ending a session".'

But if you need good control over transactions then use the Transaction class.

...and the reason for you not getting the enitty to the db is because you miss the call to flush(); which acutally is perfectly aligned with java io streams since you also can lose data on a stream if you do not close it first ;) ...

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 29, 2004 7:10 pm 
Newbie

Joined: Sat Feb 28, 2004 9:02 am
Posts: 6
Right, but I did close the session.

With Java streams, close() automatically calls flush(). Apparently, it doesn't with Sessions. That's what had me confused.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 29, 2004 7:23 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you got it ! ;)

(...but still java streams does not call flush() on close() - only some of them do! If you are writing to a file then always remember to call flush() before closing!)

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