-->
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.  [ 5 posts ] 
Author Message
 Post subject: hibernate quickly CH03: flush does not work, tx.commit works
PostPosted: Wed Mar 21, 2007 8:11 am 
Newbie

Joined: Wed Mar 21, 2007 8:01 am
Posts: 2
hi all,

i'm studying chapter 3 of the book hibernate quickly.

at the same time, i'm trying everyting in a sample application.
now, i came at the point where i have everything set up correctly.

i have one class "Event", from which I try to put an instance in the database.

When I try this, it works fine and i see my created line in the database
Code:
        Event e = new Event();
        e.setId(3);
        e.setName("Meeting C");
        e.setDuration(2005);
     
        Session session = factory.openSession();
       
        Transaction tx = session.beginTransaction();
       
        session.save(e);
       
        tx.commit();
        session.close();



But when I try this (with flush() instead of transaction commit()), i get no errors, but there's no row created in the database. Should i maybe indicate somewhere in a config file "not to use transactions and commit by default "?
Code:
        Event e = new Event();
        e.setId(3);
        e.setName("Meeting C");
        e.setDuration(2005);
       
        Session session = factory.openSession();
             
        session.save(e);
        log.debug("6");
       
        session.flush();
        session.close();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 21, 2007 8:35 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
When you call save() (or actually when you call flush()), Hibernate executes an SQL INSERT statement. If you don't commit the transaction, that statement never gets committed on the database level, hence, you don't get a record. This is how transactions work.

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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 21, 2007 8:36 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
And what you are asking for is called the "auto commit mode", where ever SQL statement is wrapped automatically in a very short transaction. This is totally evil in most applications, read the "Transactions and concurrency" chapter.

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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 21, 2007 8:52 am 
Newbie

Joined: Wed Mar 21, 2007 8:01 am
Posts: 2
Hibernate Quickly 3.4 Persisting objects page 69: wrote:
The flush() call forces persistent objects held in memory to be synchronized to the database. Sessions don’t immediately write to the database when an object is saved. Instead, the Session queues a number of database writes to maximize performance.


christian wrote:
When you call save() (or actually when you call flush()), Hibernate executes an SQL INSERT statement. If you don't commit the transaction, that statement never gets committed on the database level, hence, you don't get a record.


so what you say here is in contradiction to the book?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 21, 2007 10:29 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Oh, you are reading "Hibernate Quickly"! I don't know how you ended up here but this is the forum for the book "Java Persistence with Hibernate". Please direct further questions to the authors of "Hibernate Quickly", they must have a forum on the Manning website.

In any case, the statement you quoted does not in any way contradict what I said. Flushing synchronizes in-memory state with the database by executing SQL statements. These statements need to be committed or rolled back with a transaction.

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.