-->
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.  [ 5 posts ] 
Author Message
 Post subject: Data Scope of a Session
PostPosted: Mon Jul 28, 2008 5:16 am 
Newbie

Joined: Mon Jul 28, 2008 5:11 am
Posts: 3
hi!

Consider following code:

Code:
      Session s1=HibernateFactory.openSession();
      Session s2=HibernateFactory.openSession();
      
      System.out.println(s1.hashCode());
      System.out.println(s2.hashCode());
      
      Transaction t1=s1.beginTransaction();
      Transaction t2=s2.beginTransaction();
      
      Pojo p1=new Pojo();
      
      Integer id=(Integer)s1.save(p1);
      
      s2.get(Pojo.class, id);



I got 2 Independent Sessions (verified via hashcode) and 2 transaktions. But i get an uncommited Object of session 1 via session2. (last line returns p1!)

have i understood the Session Model of Hibernate wrong? Shouldnt Session 2 get NULL becasue Sessions have a different Scope?

DB is HSQL, Hibernate 3.1

Thanks in Advance!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 28, 2008 4:07 pm 
Newbie

Joined: Mon Jul 28, 2008 5:11 am
Posts: 3
nobody an idea? or is the answer so obvious? does hibernate isolate different sessions or not?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 7:19 am 
Newbie

Joined: Wed Jul 23, 2008 2:50 am
Posts: 16
mkdigital wrote:
nobody an idea? or is the answer so obvious? does hibernate isolate different sessions or not?


Object Comparisons

Once you start mixing and matching persistent and detached objects within your code, which pretty much any J2EE application will do at some point in time, you will find some not-so-funny, and potentially non-intuitive, problems that come up when you start doing comparisons of instances that you would think would be the same.

For example, take a look at the following code that creates two instances, user1 and user2, based on the same, identical, database record. What do you think the output of the code snippet would be?
Session session=HibernateUtil.beginTransaction();

User user1 = new User();
user1.setPassword("aaaaaa");
Long id = (Long)session.save(user1);
session.evict(user1);
User user2 = (User)session.get(User.class, id);


System.out.print("The instances are the same: ");
System.out.println( user1.equals(user2));

HibernateUtil.commitTransaction();

Since both instances of the User class are based on the same database record, they will have all of their properties set to the same values, which means the two objects are essentially the same, but the comparison of the two objects returns false. It's somewhat non-intuitive, but if you know what's going on under the covers of the Java Virtual Machine, it actually makes sense.

By default, when you compare two instances using .equals(), the compiler simply compares the memory locations of the two instances, as opposed to comparing their actual property values. Since we have two separate instances, we end up having two separate memory locations, and a .equals() comparison returns false. To overcome such situations, a Hibernate best practice is to have all of your JPA annotated classes properly override the inherited .hashcode() and .equals() methods, providing an implementation that makes sense for the class. That way, when two instances with exactly the same state are compared, the actual properties the object contains will be compared, and the compiler will not simply look at the memory locations of objects when performing an equality comparison.

get from http://jpa.ezhibernate.com/Javacode/lea ... rnateworks

maybe help....


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 7:41 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Wow! Someone reference my website before I could!

:)

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 9:45 am 
Newbie

Joined: Mon Jul 28, 2008 5:11 am
Posts: 3
thanks for your answer, but i think that was not my problem.

i tried (with the code of my first post) to find out if hibernate sessions are isolated.

the sessions ARE not equal


Code:
      Session s1=HibernateFactory.openSession();
      Session s2=HibernateFactory.openSession();
     
      System.out.println(s1.hashCode());
      System.out.println(s2.hashCode());
     
      Transaction t1=s1.beginTransaction();
      Transaction t2=s2.beginTransaction();
     
      Pojo p1=new Pojo();
     
      Integer id=(Integer)s1.save(p1);

   System.out.println(s1);
   System.out.println(s2);
     
      s2.get(Pojo.class, id);



results in

Code:
SessionImpl(PersistenceContext[entityKeys=[[b]EntityKey[test.Pojo#2[/b]]],...
SessionImpl(PersistenceContext[entityKeys=[],



they have even a different PersistenceContext, so they must be different independend sessions.

your answer was about that 2 objects which are not equal in the "==" sense can be equal in the database. but my questions was about the isolation of sessions. is there any way that a session doesnt read uncommited data from another one?


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