-->
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: Query.list returns old view of data
PostPosted: Sun Dec 21, 2008 9:49 pm 
Newbie

Joined: Sun Dec 21, 2008 8:24 pm
Posts: 2
Hibernate version:
3.2.5. Also tried 3.3.1.

Problem:
I have a woodstock table on a JSF page with the ability to add and delete records. This works fine and the database is always correct. However hibernate queries return random states in which the data was at.

To recreate I navigate to the page add 2 records. At this point the
database table has 2 records. I then navigate away from the page and
back numerous times. After a few attempts it will suddenly start showing
an empty table and then the records come back. It also happens when
records are deleted and the records come back again. I have proved with
logging that it's not the table but the list returned from the hibernate
query.

Has anybody got any ideas? I am just trying to learn Hibernate and I appear to be stuck on the basics. The project is actually on sourceforge if anyone feels like really digging in (http://sourceforge.net/projects/cnery/).

I recreated it with this scenario (see log below):
1. 8:26:42 Add record TEST1
2. 8:26:46 Add record TEST2
3. Navigate away and back a few times.
4. 8:26:52 Records have disappeared!
5. 8:26:58 They're back again.

Mapping document:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="cnerydb.Scene" table="scene">
    <id column="scene_id" name="sceneId">
      <generator class="increment"/>
    </id>
    <property column="name" name="name"/>
    <property column="group_address" name="groupAddress"/>
    <set cascade="all-delete-orphan" inverse="true" lazy="true" name="sceneActions" table="scene_action">
      <key column="scene_id"/>
      <one-to-many class="cnerydb.SceneAction"/>
    </set>
  </class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Code:
        try{
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            Query q = session.createQuery("from Scene");
            List<Scene> _scenes = (List<Scene>)q.list();
            setScenes(_scenes);

            sceneOptions = new Option[_scenes.size()];
            int i=0;
            String sceneDebugMessage = "updateScenes - ";
            boolean first = true;
            for(Scene scene : _scenes) {
                Option opt = new Option(scene.getSceneId(), scene.getName());
                if (!first)
                    sceneDebugMessage += ", ";
                first = false;
                sceneDebugMessage += "Scene: " + scene.getSceneId() + ", " + scene.getName();
                sceneOptions[i++] = opt;
            }
            Logger.getLogger(SessionBean1.class.getName()).log(Level.INFO, sceneDebugMessage);
        } catch(Exception e) {
            e.printStackTrace();
        }

Name and version of the database you are using:
MySQL 5.0.70
Log from application including SQL (show_sql=true):
Code:
Hibernate: select scene0_.scene_id as scene1_0_, scene0_.name as name0_, scene0_.group_address as group3_0_ from scene scene0_
Hibernate: select scene0_.scene_id as scene1_0_, scene0_.name as name0_, scene0_.group_address as group3_0_ from scene scene0_
Dec 21, 2008 8:26:36 PM cnery.SessionBean1 updateScenes
INFO: updateScenes -
Dec 21, 2008 8:26:36 PM cnery.SessionBean1 updateScenes
INFO: updateScenes -
Hibernate: select max(scene_id) from scene
Hibernate: insert into scene (name, group_address, scene_id) values (?, ?, ?)
Hibernate: select scene0_.scene_id as scene1_0_, scene0_.name as name0_, scene0_.group_address as group3_0_ from scene scene0_
Hibernate: select scene0_.scene_id as scene1_0_, scene0_.name as name0_, scene0_.group_address as group3_0_ from scene scene0_
Dec 21, 2008 8:26:42 PM cnery.SessionBean1 updateScenes
INFO: updateScenes - Scene: 1, TEST1
Dec 21, 2008 8:26:42 PM cnery.SessionBean1 updateScenes
INFO: updateScenes - Scene: 1, TEST1
Hibernate: insert into scene (name, group_address, scene_id) values (?, ?, ?)
Hibernate: select scene0_.scene_id as scene1_0_, scene0_.name as name0_, scene0_.group_address as group3_0_ from scene scene0_
Hibernate: select scene0_.scene_id as scene1_0_, scene0_.name as name0_, scene0_.group_address as group3_0_ from scene scene0_
Dec 21, 2008 8:26:46 PM cnery.SessionBean1 updateScenes
INFO: updateScenes - Scene: 1, TEST1, Scene: 2, TEST2
Dec 21, 2008 8:26:46 PM cnery.SessionBean1 updateScenes
INFO: updateScenes - Scene: 1, TEST1, Scene: 2, TEST2
Hibernate: select scene0_.scene_id as scene1_0_, scene0_.name as name0_, scene0_.group_address as group3_0_ from scene scene0_
Dec 21, 2008 8:26:50 PM cnery.SessionBean1 updateScenes
INFO: updateScenes - Scene: 1, TEST1, Scene: 2, TEST2
Hibernate: select scene0_.scene_id as scene1_0_, scene0_.name as name0_, scene0_.group_address as group3_0_ from scene scene0_
Dec 21, 2008 8:26:52 PM cnery.SessionBean1 updateScenes
INFO: updateScenes -
Hibernate: select scene0_.scene_id as scene1_0_, scene0_.name as name0_, scene0_.group_address as group3_0_ from scene scene0_
Dec 21, 2008 8:26:58 PM cnery.SessionBean1 updateScenes
INFO: updateScenes - Scene: 1, TEST1, Scene: 2, TEST2
Hibernate: select scene0_.scene_id as scene1_0_, scene0_.name as name0_, scene0_.group_address as group3_0_ from scene scene0_
Dec 21, 2008 8:27:03 PM cnery.SessionBean1 updateScenes
INFO: updateScenes -
Hibernate: select scene0_.scene_id as scene1_0_, scene0_.name as name0_, scene0_.group_address as group3_0_ from scene scene0_
Dec 21, 2008 8:27:07 PM cnery.SessionBean1 updateScenes
INFO: updateScenes - Scene: 1, TEST1, Scene: 2, TEST2
Hibernate: select scene0_.scene_id as scene1_0_, scene0_.name as name0_, scene0_.group_address as group3_0_ from scene scene0_
Dec 21, 2008 8:27:10 PM cnery.SessionBean1 updateScenes
INFO: updateScenes -
Hibernate: select scene0_.scene_id as scene1_0_, scene0_.name as name0_, scene0_.group_address as group3_0_ from scene scene0_
Dec 21, 2008 8:27:12 PM cnery.SessionBean1 updateScenes
INFO: updateScenes -
Hibernate: select scene0_.scene_id as scene1_0_, scene0_.name as name0_, scene0_.group_address as group3_0_ from scene scene0_
Dec 21, 2008 8:27:15 PM cnery.SessionBean1 updateScenes
INFO: updateScenes - Scene: 1, TEST1, Scene: 2, TEST2
Hibernate: select scene0_.scene_id as scene1_0_, scene0_.name as name0_, scene0_.group_address as group3_0_ from scene scene0_
Dec 21, 2008 8:27:18 PM cnery.SessionBean1 updateScenes
INFO: updateScenes -
Hibernate: select scene0_.scene_id as scene1_0_, scene0_.name as name0_, scene0_.group_address as group3_0_ from scene scene0_
Dec 21, 2008 8:27:20 PM cnery.SessionBean1 updateScenes
INFO: updateScenes - Scene: 1, TEST1, Scene: 2, TEST2


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 22, 2008 2:33 am 
Newbie

Joined: Sun Dec 21, 2008 8:24 pm
Posts: 2
I think I have found the problem. I had not setup the hibernate.connection.isolation and from what I've read Hibernate defaults to REPEATABLE_READ. Changing this to 2 (READ_COMMITTED) seems to have solved the problem.

So it looks like MySQL or the combination of MySQL and InnoDB tables does not work very well with REPEATABLE_READ.

Cheers,
Dave.


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.