-->
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.  [ 13 posts ] 
Author Message
 Post subject: Object attribute value keeps changing after update????
PostPosted: Thu Sep 29, 2005 6:18 pm 
Beginner
Beginner

Joined: Wed Sep 21, 2005 12:02 am
Posts: 31
Env: Hibernate 3.0.5 and MySQL 4.0.21 on MacOS X 10.4.2, running in Tomcat 5.

I am having small issue where my Object seems to to have a different attribute value every time I do a query on it, after having updated its value. For example, if I update the value 5 times and then do a query 10 times, then returned value is akin to a random value in the previous values it had been set to. The attribute I am interested in my examples is 'status'.

This is really freaking me out and is proving to be a major block for me.

The code I am using to do the query is:

Code:
      Criteria criteria = session.createCriteria(Reservation.class);
      criteria.addOrder(Order.asc("time"));
      criteria.add(Expression.ge("date",startDate));
      criteria.add(Expression.le("date",endDate));


and the resulting query is:

Code:
Hibernate: select this_.id as id0_, this_.title as title2_0_, this_.description as descript3_2_0_, this_.startdate as startdate2_0_, this_.enddate as enddate2_0_, this_.starttime as starttime2_0_, this_.endtime as endtime2_0_, this_.allday as allday2_0_ from closeddates this_ where this_.startdate<=? and this_.enddate>=?


and the mapping file is as follows:

Code:
<hibernate-mapping>
   <class name="myco.domain.Reservation" table="reservations">
      
      <id
         name="key"
         column="id"
         type="integer">
         <generator class="native"/>
      </id>      

      <property
         name="date"
         column="resDate"
         type="date"/>
      <property
         name="time"
         column="resTime"
         type="time"/>
      <property
         name="partySize"
         column="pax"
         type="integer"/>
      <property
         name="tableSize"
         column="tableSize"
         type="integer"/>   
      <property
         name="tableNumber"
         column="tableNumber"
         type="integer"/>
      <property
         name="smoking"
         column="smoking"
         type="boolean"/>               
      <property
         name="walkin"
         column="walkin"
         type="boolean"/>   
      <property
         name="status"
         column="status"
         type="integer"/>   
      <property
         name="comments"
         column="comments"
         type="string"/>      
      
      <many-to-one
           name="guest"
           column="guestId"
           class="myco.domain.Guest"
           fetch="join"
           lazy="true"/>
           
   </class>   
   
</hibernate-mapping>


This is my update method:

Code:
  public void updateReservation (Reservation reservation) throws HibernateException{
    Transaction tx = null;
    Session session = null;   
    session = HibernateUtils.currentSession();
    tx = session.beginTransaction();     
    session.update(reservation);
    tx.commit();       
  }


BTW I have tried playing around with the values of Session.setCacheMode(), to no avail.

Any ideas??


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 3:20 pm 
Beginner
Beginner

Joined: Wed Sep 21, 2005 12:02 am
Posts: 31
Still no replies :( I looked at the database and the value in the database does correspond to what I am saving, so the issue is definetly in reading back.

Should I assume this is a bug in Hibernate?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 3:44 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
Well, this query is not on your primary, so it could potentially return multiple results.

Are you 100% sure that the same object (with the same primary key field) is really changing value?

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 4:49 pm 
Beginner
Beginner

Joined: Wed Sep 21, 2005 12:02 am
Posts: 31
I have done a few further tests and can say thay every time I call update the data in the database is being updated. I am using a separate cleint (Navicat), to check the state of the record.

I tried replacing the update with one in pure JDBC, but that makes no difference (I had assumed a cache issue, but this does not seem to be the case), so I doubt the update is the route of the issues.

I can confirm that there are multiple sessions, despite me thinking only two had been created.

In my logs I see that if I do multiple refreshes of my page, after I have stopped doing any modifications, so it is just the queries that are performed that the status for the object with the same ID changes.

I certainly don't want to blame Hibernate, but I just can't work out what could be going wrong.

BTW My session creation routine looks as follows:
Code:
  public static final ThreadLocal session = new ThreadLocal();
  public static SessionFactory sessionFactory;

  public static Session currentSession() throws HibernateException {
    Session s = (Session) session.get();
    // Open a new Session, if this Thread has none yet
    if (s == null) {
      s = sessionFactory.openSession();
      session.set(s);
           
    }
    log.debug("****** session: " + s.hashCode() + " - "+ s );
    return s;
  }


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 4:54 pm 
Beginner
Beginner

Joined: Wed Sep 21, 2005 12:02 am
Posts: 31
Further tests seem to indicate that the differing values seems to be related to which session is returning the data. This would tend to indicate that the data in the sessions is out of sync. Since the data in the database corresponds to what my last update committed, I am really confused.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 5:32 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
Well.

There is a cache associated to the session. If you work with object in one session and update in another, you will face these issues.


Why do you have multiple sessions? Is it something that you want to have or is it a problem you are trying to solve?

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 5:41 pm 
Beginner
Beginner

Joined: Wed Sep 21, 2005 12:02 am
Posts: 31
Well, I have been using a ThreadLocal approach to storing a session reference, that I have seen in numerous examples. Obvisually this does not work for me, since I am writing a web application and this would not always be using the same thread. This much I can modify.

This does raise another issue, if an entity's state changes in one session, surely it should be marked dirty in the other sessions, to force an update? Is there any way I can do this prior to doing a query.

At the moment I know that the data is only going to be modified from a single application, but what happens in the case that the database is updated from multiple sources?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 6:02 pm 
Beginner
Beginner

Joined: Wed Sep 21, 2005 12:02 am
Posts: 31
I replaced the ThreadLocal approach with a single static Session in my HibernateUtil class and that seems to have corrected the issue.

I still mantain that if one Session gets an instance of an entity updated, then that update should be passed across the active sessions to maintain data consistency.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 10:06 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Well I still maintain that pigs should fly ;)

Seriously, dude, stop and think about what you are saying. A Session equates to a "unit of work"...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 10:31 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
ajmas wrote:
I replaced the ThreadLocal approach with a single static Session


you know that thing isn't thread safe, right?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 10:41 pm 
Beginner
Beginner

Joined: Wed Sep 21, 2005 12:02 am
Posts: 31
dennisbyrne wrote:
ajmas wrote:
I replaced the ThreadLocal approach with a single static Session


you know that thing isn't thread safe, right?


That much I realise, but I needed something which worked. If you have a better solution, then I am all ears.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 10:54 am 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
Hi Ajmas,

if you are in a classic web application, having a static reference is really not going to do the job for you. Also, unless you have some funky stuff going on, in a classic web application, a unit of work should pretty much correspond to a request to the server.

If this is your case, then your transaction should begin when the request is issued and finish when the response is sent to the client. This way, every classes involved in your request will read and write to the same session.

I suggest you read the documentation on transaction boundaries. It might help you with your problem.

You should really re-think about using the ThreadLocal approach and copy the code from the example. It works fine.

Good luck and let me know if it worked for you.

Regards,
Vincent.

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 8:36 pm 
Beginner
Beginner

Joined: Wed Sep 21, 2005 12:02 am
Posts: 31
Sounds like I went from one extreme to another. Thanks Vincent for making it clear.


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