-->
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: sess.get() returns not actual instance (in multi-thread App)
PostPosted: Sat Nov 05, 2005 6:35 am 
Newbie

Joined: Sun Jun 19, 2005 3:02 am
Posts: 3
I have database record Cat(id='1', name='Unchanged name') and following code:

Code:
Session firstThreadSession = Config.getInstance().getSessionFactory().openSession();
firstThreadSession.get(Cat.class, "1");

//execute in another thread
Session secondThreadSession = Config.getInstance().getSessionFactory().openSession();

Transaction tx = secondThreadSession.beginTransaction();
Cat c = (Cat)secondThreadSession.get(Cat.class, "1");
c.setName("Changed name");
secondThreadSession.update(c);
tx.commit();
////////

System.out.println(((Cat)firstThreadSession.get(Cat.class, "1")).getName());


As result I see "Unchanged name". When I call firstThreadSession.clear() before System.out.println result is correct ('Changed name') .

Is there any way to get actual instance without using firstThreadSession.clear() ?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 05, 2005 6:46 am 
Beginner
Beginner

Joined: Fri Oct 28, 2005 10:46 am
Posts: 37
The Session is the first level cache. On a get, the Session is the first thing checked. If it finds a corresponding object, that is used. The clear method will remove all cached objects as well as all pending database updates. In short, I think the answer to your question is no. That would go against the design of Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 05, 2005 6:55 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
The answer is "no, because that would break transaction isolation and require process-level synchronization on entity instances".


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.