-->
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: Problem with using getCurrentSession() to query database
PostPosted: Tue Apr 27, 2010 6:26 am 
Newbie

Joined: Tue Apr 27, 2010 2:02 am
Posts: 2
Hi,

I am currently experiencing an issue using hibernate-tools 3.2.4.GA.

Consider following snippet

Code:

methodA(String namedQuery) {
session = sessionFactory().getCurrentSession();
Query query = session.getNamedQuery(namedQuery);

results = query.list();

}


when i call methodA twice in a loop, I'm getting weird results. Ex: say for 1st call, resultset should contain ObjectA and for 2nd call resultset should contain ObjectB. What happens in my loop is: For first call i get the result correctly, but for second call i get (ObjectA + ObjectB) instead of only ObjectB.

I have been able to resolve this, using:
Code:
session = sessionFactory().openSession();


But I am wondering wether I missed any configuration which would have allowed me to use getCurrentSession().

Also, performance-wise what impact would this be having if I am to open a new session for each call?

Can anyone help please??


Top
 Profile  
 
 Post subject: Re: Problem with using getCurrentSession() to query database
PostPosted: Tue Apr 27, 2010 11:27 am 
Newbie

Joined: Thu Apr 01, 2010 6:09 pm
Posts: 8
Generally, the usage of opensession() is not recommended since it opens a new session that needs manual closure since this comes away from spring or container managed sessions.

However opencurrentsession() asks the container to get the session that is already open and is the current session in use. Using this is highly recommended!

But can you let me know the query details which gives you such result sets. And can you let me know the content in your db that results in this output?

Thanks
Priya
Myblog: [url="http://javaprogrammingtips4u.blogspot.com"]Spring/Hibernate tips[/url]


Top
 Profile  
 
 Post subject: Re: Problem with using getCurrentSession() to query database
PostPosted: Mon May 03, 2010 1:33 am 
Newbie

Joined: Tue Apr 27, 2010 2:02 am
Posts: 2
Hi Priya,

Thanks for the reply. My queries are basic hibernate select statements.

Ex:

Code:

SELECT user
FROM UserDTOImpl user
WHERE user.userId=:localUserId



Now consider the following calls being made under one transaction:

Transaction 1
MethodA is executed once with userId = 3
ResultSet is User("xxx") is returned


Transaction 2
MethodA is executed once with userId = 5
ResultSet is User("yyy") is returned

Transaction 3
MethodA is executed twice, once with userId = 3 and then with userId = 5
ResultSet is User("xxx") is returned for first call
For Second Call, instead of User("yyy") being returned, User("xxx") and User("yyy") are returned.


The only ways the I have been able to resolve this is through openming a new session for each call, or perform each call under a seperate transaction.

I wanted to do each call under the same transaction but without having to open a new session each time. Can anyone help?


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.