-->
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.  [ 4 posts ] 
Author Message
 Post subject: Strange problem with update
PostPosted: Mon Jun 20, 2011 12:38 pm 
Beginner
Beginner

Joined: Mon Nov 15, 2010 10:39 am
Posts: 27
Hi,

I am encountering a quite strange problem when updating persistent objects with Hibernate. I am using Struts2 for my UI. When i update an entry more than once, its value changes with each refresh (and thus each query to Hibernate), alternating between the newly updated value, and the one of the last values that the entry had (I have checked with test printouts and ensured that its not a caching issue on the frontend, Hibernate actually returns different values for each call).

Some code:

HibernateUtil.java

Code:
    public void persistObject(DataElement o) {
       getSessionFactory().getCurrentSession().beginTransaction();
       getSessionFactory().getCurrentSession().saveOrUpdate(o);

       getSessionFactory().getCurrentSession().getTransaction().commit();

    }


Code:
   public List runQuery(String query) {
       List result;
       getSessionFactory().getCurrentSession().beginTransaction();
       result = getSessionFactory().getCurrentSession().createQuery(query).list();
       return result;
    }


The latter is in turn called by this method, within PersistenceFactory.java:

Code:
   public Artifact getArtifact(long uuid) {
      List result = hibernateUtil.runQuery("from Artifact where uuid = " + uuid);
      System.out.println("Result size: " + result.size());
      return (Artifact) result.get(0);
   }


Which again is called by a method in the action class:

Code:
   public String editArtifact() {
      artifactBean = persistenceFactory.getArtifact(uuid);
      System.out.println("uuid: " + uuid + " version: " + artifactBean.getVersion());
      return "success";
   }


Upon refreshing the action responsible for calling the editArtifact() method, the printouts that i get does not make much sense:
Code:
4 4
Hibernate: select artifact0_.uuid as uuid14_, artifact0_.Description as Descript2_14_, artifact0_.Name as Name14_, artifact0_.version as version14_, artifact0_.ArtifactStateType as Artifact1_21_, artifact0_.clazz_ as clazz_ from ( select uuid, Description, Name, version, ArtifactStateType, 7 as clazz_ from Artifact union all select uuid, Description, Name, version, ArtifactStateType, 8 as clazz_ from Specification union all select uuid, Description, Name, version, ArtifactStateType, 9 as clazz_ from Request union all select uuid, Description, Name, version, ArtifactStateType, 10 as clazz_ from Report union all select uuid, Description, Name, version, ArtifactStateType, 11 as clazz_ from Plan union all select uuid, Description, Name, version, ArtifactStateType, 12 as clazz_ from Log union all select uuid, Description, Name, version, ArtifactStateType, 13 as clazz_ from List union all select uuid, Description, Name, version, ArtifactStateType, 14 as clazz_ from Instruction union all select uuid, Description, Name, version, ArtifactStateType, 15 as clazz_ from Diagram union all select uuid, Description, Name, version, ArtifactStateType, 16 as clazz_ from Description ) artifact0_
Hibernate: select artifact0_.uuid as uuid14_, artifact0_.Description as Descript2_14_, artifact0_.Name as Name14_, artifact0_.version as version14_, artifact0_.ArtifactStateType as Artifact1_21_, artifact0_.clazz_ as clazz_ from ( select uuid, Description, Name, version, ArtifactStateType, 7 as clazz_ from Artifact union all select uuid, Description, Name, version, ArtifactStateType, 8 as clazz_ from Specification union all select uuid, Description, Name, version, ArtifactStateType, 9 as clazz_ from Request union all select uuid, Description, Name, version, ArtifactStateType, 10 as clazz_ from Report union all select uuid, Description, Name, version, ArtifactStateType, 11 as clazz_ from Plan union all select uuid, Description, Name, version, ArtifactStateType, 12 as clazz_ from Log union all select uuid, Description, Name, version, ArtifactStateType, 13 as clazz_ from List union all select uuid, Description, Name, version, ArtifactStateType, 14 as clazz_ from Instruction union all select uuid, Description, Name, version, ArtifactStateType, 15 as clazz_ from Diagram union all select uuid, Description, Name, version, ArtifactStateType, 16 as clazz_ from Description ) artifact0_ where artifact0_.uuid=1
Result size: 1
uuid: 1 version: 2
Hibernate: select artifactst0_.uuid as uuid2_, artifactst0_.Description as Descript2_2_, artifactst0_.Name as Name2_ from ArtifactStateType artifactst0_
Hibernate: select artifact0_.uuid as uuid14_, artifact0_.Description as Descript2_14_, artifact0_.Name as Name14_, artifact0_.version as version14_, artifact0_.ArtifactStateType as Artifact1_21_, artifact0_.clazz_ as clazz_ from ( select uuid, Description, Name, version, ArtifactStateType, 7 as clazz_ from Artifact union all select uuid, Description, Name, version, ArtifactStateType, 8 as clazz_ from Specification union all select uuid, Description, Name, version, ArtifactStateType, 9 as clazz_ from Request union all select uuid, Description, Name, version, ArtifactStateType, 10 as clazz_ from Report union all select uuid, Description, Name, version, ArtifactStateType, 11 as clazz_ from Plan union all select uuid, Description, Name, version, ArtifactStateType, 12 as clazz_ from Log union all select uuid, Description, Name, version, ArtifactStateType, 13 as clazz_ from List union all select uuid, Description, Name, version, ArtifactStateType, 14 as clazz_ from Instruction union all select uuid, Description, Name, version, ArtifactStateType, 15 as clazz_ from Diagram union all select uuid, Description, Name, version, ArtifactStateType, 16 as clazz_ from Description ) artifact0_ where artifact0_.uuid=1
Result size: 1
uuid: 1 version: 4
Hibernate: select artifactst0_.uuid as uuid2_, artifactst0_.Description as Descript2_2_, artifactst0_.Name as Name2_ from ArtifactStateType artifactst0_
Hibernate: select artifact0_.uuid as uuid14_, artifact0_.Description as Descript2_14_, artifact0_.Name as Name14_, artifact0_.version as version14_, artifact0_.ArtifactStateType as Artifact1_21_, artifact0_.clazz_ as clazz_ from ( select uuid, Description, Name, version, ArtifactStateType, 7 as clazz_ from Artifact union all select uuid, Description, Name, version, ArtifactStateType, 8 as clazz_ from Specification union all select uuid, Description, Name, version, ArtifactStateType, 9 as clazz_ from Request union all select uuid, Description, Name, version, ArtifactStateType, 10 as clazz_ from Report union all select uuid, Description, Name, version, ArtifactStateType, 11 as clazz_ from Plan union all select uuid, Description, Name, version, ArtifactStateType, 12 as clazz_ from Log union all select uuid, Description, Name, version, ArtifactStateType, 13 as clazz_ from List union all select uuid, Description, Name, version, ArtifactStateType, 14 as clazz_ from Instruction union all select uuid, Description, Name, version, ArtifactStateType, 15 as clazz_ from Diagram union all select uuid, Description, Name, version, ArtifactStateType, 16 as clazz_ from Description ) artifact0_ where artifact0_.uuid=1
Result size: 1
uuid: 1 version: 2
Hibernate: select artifactst0_.uuid as uuid2_, artifactst0_.Description as Descript2_2_, artifactst0_.Name as Name2_ from ArtifactStateType artifactst0_


The "4 4" printout at the top shows the version variable of the element that is being persisted, as well as the version variable of the element through a Hibernate query. This looks as it should. But then, upon editing this same element, the value of version is suddenly 2 (which it was a couple of updates ago). Another refresh (and another Hibernate query), and the value is 4. When refreshing again, the value is back to 2. And all of this without a single call to Hibernate asking it to update or change anything.

The way an element is added/updated:
Code:
      if(artifactBean != null) { //if an artifact has been added through struts:
         if(artifactBean.getUuid() != null) { //check if it has been stored in hibernate
            Artifact tmp = persistenceFactory.getArtifact(artifactBean.getUuid()); //get existing version
               tmp.setUuid(artifactBean.getUuid());
                     //tmp is populated with the values of artifactBean, defined by the user through struts
               hibernateUtil.persistObject(tmp); //save tmp
                                               
             }else { //if the artifact has not been stored, just save it
            hibernateUtil.persistObject(artifactBean);
}



Does anyone know what might be wrong here? (I can add more code if needed.)

Regards,

Tobb


Top
 Profile  
 
 Post subject: Re: Strange problem with update
PostPosted: Mon Jun 20, 2011 4:37 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
My guess is that there is a problem with closing and clearing the current session at the end of the request. Since the web app is re-using the same thread in a later request the call to getSessionFactory().getCurrentSession() will return an old session which contains old data in the first-level cache. I have no experience with Struts but basically you need to implement/enable something called open-session-in-view pattern. I guess this has been done before so hopefully you can find something to just plug in.


Top
 Profile  
 
 Post subject: Re: Strange problem with update
PostPosted: Mon Jun 20, 2011 5:23 pm 
Beginner
Beginner

Joined: Mon Nov 15, 2010 10:39 am
Posts: 27
I was thinking something along those lines as well, that the session didn't close properly. I tried to use session.open() and session.close(), but then i get an exception saying that the session is already closed. I'll do some googleing tomorrow for that pattern, thanks.


Top
 Profile  
 
 Post subject: Re: Strange problem with update
PostPosted: Wed Jun 22, 2011 5:27 am 
Beginner
Beginner

Joined: Mon Nov 15, 2010 10:39 am
Posts: 27
I found the error, I had actually forgotten to close the session after running a query, so old queries were still lying around. That also explains the values going back and forth.


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