-->
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.  [ 6 posts ] 
Author Message
 Post subject: Reusing Session with Native Query Execution
PostPosted: Fri Sep 04, 2009 2:42 am 
Newbie

Joined: Fri Sep 04, 2009 2:29 am
Posts: 7
I am working on a web application using hibernate and spring. We are using spring for managing transactions. Our application uses native queries heavily along with Hibernate queries. Below is code in a DAO how we execute it:

Session hibernateSession = getSession();
myList = hibernateSession.createSQLQuery(query).list();
hibernateSession.flush();
hibernateSession.close();

As shown in above code every time the query executes new session is created and hence separate connection is obtained for each call. This makes number of busy connections jumps to maximum pool size. With this and new incoming request are not entertained with exception failed to check out connection from pool.

Using the sessionFactory.getCurrentSession() throws exception "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here"


Top
 Profile  
 
 Post subject: Re: Reusing Session with Native Query Execution
PostPosted: Fri Sep 04, 2009 3:20 am 
Beginner
Beginner

Joined: Sat Aug 01, 2009 5:28 am
Posts: 42
are you creating new session in your getSession method or retrieving the already open session

_________________
Warm Regards,
Tarun Walia


Top
 Profile  
 
 Post subject: Re: Reusing Session with Native Query Execution
PostPosted: Fri Sep 04, 2009 3:24 am 
Newbie

Joined: Fri Sep 04, 2009 2:29 am
Posts: 7
code calls getSession() from HibernateDaoSupport.clas and always create new session.

I am looking is it possible to use existing session just like Spring do for whole request instead of for each method call.


Top
 Profile  
 
 Post subject: Re: Reusing Session with Native Query Execution
PostPosted: Fri Sep 04, 2009 4:39 am 
Beginner
Beginner

Joined: Sat Aug 01, 2009 5:28 am
Posts: 42
i have no idea about spring but it is possible to reuse the hibernate session,depends how u r using it
i use session per transaction rather session per request

_________________
Warm Regards,
Tarun Walia


Top
 Profile  
 
 Post subject: Re: Reusing Session with Native Query Execution
PostPosted: Fri Sep 04, 2009 4:40 am 
Newbie

Joined: Fri Sep 04, 2009 2:29 am
Posts: 7
Ok could you give bit more details. If a transaction require multiple DAO method calls and native queries how you use all these under single transaction.


Top
 Profile  
 
 Post subject: Re: Reusing Session with Native Query Execution
PostPosted: Fri Sep 04, 2009 6:57 am 
Beginner
Beginner

Joined: Sat Aug 01, 2009 5:28 am
Posts: 42
//Open your session
Session sess=...............;
Transaction trans=s.beginTransaction();
//call your dao method
//if u r passing session

daoMethod(....,sess);


//in dao simply get the session check whether transaction is active execute native queries


trans.commit();

_________________
Warm Regards,
Tarun Walia


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