-->
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.  [ 2 posts ] 
Author Message
 Post subject: Need better understanding of Session and Transaction
PostPosted: Sun Jul 16, 2006 9:42 pm 
Newbie

Joined: Tue Jul 11, 2006 10:41 pm
Posts: 11
Hi Guys,

I would like to now better understanding and relationship between
1. session.save();
2.session.flush();
3.session.connection().commit()
4. Transaction.commit();

Lets say i would like to insert data to Person Table

Person a = new Person();
a.setName("test")
session.save(a);
session.flush();
session.connection().commit()

Above will insert into database and commit the value right?

But i can also use
session.save(a);
then transaction.commit();

Which are the differences?
what are the session.save and session.flush do?


Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 16, 2006 11:53 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Session.save() puts a transient object into the session cache, preparing it for flushing to the DB. After Session.save(), the object becomes persistent wrt that session only.

Session.flush() writes all in-session changes to the JDBC connection; if there's no transaction, they flow on to the DB immediately. If there is a transaction, then only the current connection to the database knows about the changes that have just been made; thus other connections are protected from partial changes.

sesion.connection().commit() executes a DB commit. If the connection is in auto-commit mode, it does nothing, otherwise it's roughly equivalent to the SQL "GO" statement, or "commit tran" if there was a corresponding "begin tran".

Transaction.commit() commits the transaction. If you're using basic JDBC transactions, there's nothing much to distinguish this from connection().commit(), other than the fact that it protects you from JDBC code. However, there are more powerful kinds of transactions, and they all provide the Transaction interface, so this method can do a lot more than a simple connection().commit(). For example, if your application deals with several databases on each of several database servers, a sufficiently powerful transaction provider can encapsulate transacitons to each database into a single application transaciton.

Normally you shouldn't use session.connection(), and sesion.connection().commit() is even rarer in production code. Transaction.commit() is preferred. This allows you to replace your JDBC transactions with better ones later, and not have to change any application code.

Session.flush is frequently not needed, because the default behaviour forces a flush immediately before a commit. It is most useful when doing batch updates, where you're updating many things in a loop, all inside a single transaciton. Flushing every few dozen loops allows hibernate to rationalize its resource usage.

_________________
Code tags are your friend. Know them and use them.


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