-->
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.  [ 3 posts ] 
Author Message
 Post subject: Session.flush() not flushing when using mysql
PostPosted: Tue Aug 07, 2007 2:56 am 
Newbie

Joined: Tue Aug 07, 2007 2:31 am
Posts: 11
Summary: When using mySQL 5.0.41 and stepping through the code, encountering a session.flush() operation causes Hibernate to output the appropriate sql to the command line, but the database does not get updated until committing the transaction. However with HSQL flush updates the DB correctly.

Environment:
Hibernate 3.2
mySQL 5.041
connector/j 5.0.7 (also tried 5.0.6)
Windows

I discovered this in the middle of my project, however I reproduced using the CaveatEmptor native example available here http://www.hibernate.org/400.html

Mapping documents:
Everything is as in the CaveatEmptor native example, however for User.hbm.xml I changed the ID generator to be <generator class="increment"/> and I commented out the hsql config and replaced it with (With HSQL everything works as expected):
Code:
        <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">test_writer</property>
        <property name="connection.password">password</property>


My test is as follows:
Code:
   public static void main(String[] args) {
      System.out.println("Starting");
        HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
        System.out.println("Middler");
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        User user = new User("firstname", "lastname", "abcdef", "abcdef", "abcdef@foobar.com");
       
        session.save(user);
        user.setFirstname("George");
        session.flush();
        user.setFirstname("sam");
        HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();
        System.out.println("End");
   }



The generated SQL (show_sql=true):
The generated SQL is being printed out at the expected times. Save causes the first select. The next two (insert & update) are a result of the flush (a bit surpised by the update, but not a huge concern.) The final update occurs after the commit. No change in the DB until after the commit.

Code:
    select
        max(USER_ID)
    from
        USERS
Hibernate:
    insert
    into
        USERS
        (OBJ_VERSION, FIRSTNAME, LASTNAME, USERNAME, PASSWORD, EMAIL, RANK, IS_ADMIN, CREATED, DEFAULT_BILLING_DETAILS_ID, HOME_STREET, HOME_ZIPCODE, HOME_CITY, USER_ID)
    values
        (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate:
    /* update
        auction.model.User */ update
            USERS
        set
            OBJ_VERSION=?,
            FIRSTNAME=?,
            LASTNAME=?,
            `PASSWORD`=?,
            EMAIL=?,
            RANK=?,
            IS_ADMIN=?,
            DEFAULT_BILLING_DETAILS_ID=?,
            HOME_STREET=?,
            HOME_ZIPCODE=?,
            HOME_CITY=?
        where
            USER_ID=?
            and OBJ_VERSION=?
Hibernate:
    /* update
        auction.model.User */ update
            USERS
        set
            OBJ_VERSION=?,
            FIRSTNAME=?,
            LASTNAME=?,
            `PASSWORD`=?,
            EMAIL=?,
            RANK=?,
            IS_ADMIN=?,
            DEFAULT_BILLING_DETAILS_ID=?,
            HOME_STREET=?,
            HOME_ZIPCODE=?,
            HOME_CITY=?
        where
            USER_ID=?
            and OBJ_VERSION=?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 07, 2007 4:58 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Quote:
but the database does not get updated until committing the transaction

How do you determine that the database hasn't been updated?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 07, 2007 11:49 am 
Newbie

Joined: Tue Aug 07, 2007 2:31 am
Posts: 11
Thank you, that answered my question :) I was creating InnoDB tables and not think that since the updates were part of a transaction they would not show up in an external tool when doing the select. If I switch to MyIsam, everything shows up as expected.

Out of curiosity, why do we see the first update as opposed to combining it with the insert since my understanding is that the queries seem to go to the DB at the same time?


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