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: One session see no update if Entity in 2nd session updated
PostPosted: Fri Oct 14, 2011 9:31 am 
Newbie

Joined: Wed May 11, 2005 7:16 am
Posts: 9
Hello,

can somebody explain , what the philosophy of following behaviour is?

1. Open a session s1, load an entity e
2. Open a 2nd session, update e, commit transaction
3. Query in s1 find entity e with old values?? Only refresh updates values.

This is only for updates, creates and deletes are immediatly visible in session 1 at step 3.

EDIT: Strange for me is that the query in step 3 send a select to database. Updates values are there (coz of commit) but entity values in session 1 are not updated.

SampleCode: Assertion Failed:

Code:
      // Create Object in session 1
      Session createObjectSession = sessionFactory.openSession();
      createObjectSession.beginTransaction();
      
      Event originalEvent = new Event("Our very first event!", new Date());
      
      createObjectSession.save( originalEvent );
      
      Long id = originalEvent.getId();
      
      createObjectSession.getTransaction().commit();
      

      //Update object in session2 
      
      Session updateObjectSession = sessionFactory.openSession();
      
      updateObjectSession.beginTransaction();
      
      Event updatedEvent = (Event)updateObjectSession.load(Event.class, id);
      
      updatedEvent.setTitle("Our very first event! Changed!!!");
      
      updateObjectSession.saveOrUpdate(updatedEvent);
      
      updateObjectSession.getTransaction().commit();
      
      // Reread object in session 1
      
      createObjectSession.beginTransaction();
      
      Event refreshedEvent=(Event)createObjectSession.createQuery("from Event").uniqueResult();
      
      String titleRefreshedFromOriginal = refreshedEvent.getTitle();
      String titleUpdate = updatedEvent.getTitle();
      
      createObjectSession.getTransaction().commit();
      
      logger.info(titleRefreshedFromOriginal+"/"+titleUpdate);
      
      
      // Compare
      
      
      assertEquals(titleRefreshedFromOriginal,titleUpdate);
      
      createObjectSession.close();
      
      updateObjectSession.close();


Top
 Profile  
 
 Post subject: Re: One session see no update if Entity in 2nd session updated
PostPosted: Mon Oct 17, 2011 5:14 am 
Newbie

Joined: Wed May 11, 2005 7:16 am
Posts: 9
I think that this behaviour is an effect of the 1st level cache. Strange for me is

1. session cache works with fetching by id....step 3 is a fetch by query, NOT by id. Ok maybe these results are cached too but why
2. step 3 sends a select, get the updated values but does't update the session cache?? Why not, if the data already retrieved from the DB?

I read dozens of articles and topics about that. Most say use evict or clear and everything is fine..ok this works but i wanna know much more background....

Is there anybody here to explain this design? What's the background of this architecture?


Top
 Profile  
 
 Post subject: Re: One session see no update if Entity in 2nd session updated
PostPosted: Mon Oct 17, 2011 8:32 am 
Newbie

Joined: Wed Oct 12, 2011 10:11 am
Posts: 7
I also faced same issue.. but after update i called refresh to synch my table and entity.
I appreciate for explanation.


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.