-->
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: openSession() vs getCurrentSession()
PostPosted: Wed Oct 31, 2007 6:44 am 
Newbie

Joined: Wed Oct 31, 2007 6:27 am
Posts: 5
Hi, I am using hibernate 3.2 and I try to implement the "session per request" paragdim with the org.hibernate.context.ThreadLocalSessionContext .

I don't understand as well the difference between openSession() and getCurrentSession() methods of the SessionFactory class.

I know that the session returned by openSession() is not attached to the current thread while the one return by getCurrentSession is. But the 2 session seems to be in different states. For example, the following code works fine :

Code:
org.hibernate.Session sessionHibernate = com.apis.b7afn.wda.mapping.util.HibernateUtil.getSessionFactory().openSession();
sessionHibernate.createQuery("from B7AFNWDATTypologie where valeur = 'typeAchat'").list();
out.println("ok");


and this one don't work :
Code:
org.hibernate.Session sessionHibernate = com.apis.b7afn.wda.mapping.util.HibernateUtil.getSessionFactory().getCurrentSession();
sessionHibernate.createQuery("from B7AFNWDATTypologie where valeur = 'typeAchat'").list();
out.println("ok");


In the second piece of code I have to open a transaction to run my query. This is not necessary in the first one.

How can I execute a query using the second way without opening a transaction?

Thanks for reply.

My apologies for my english.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 05, 2007 4:42 am 
Newbie

Joined: Wed Oct 31, 2007 6:27 am
Posts: 5
Any idea?.....


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 05, 2007 5:00 am 
Beginner
Beginner

Joined: Tue Oct 30, 2007 7:57 am
Posts: 47
Sorry if I say something too obvious. openSession retrieves a newly created sesion, while getCurrentSession gets the last sesion created with openSession (if you didn't close it). The second is a cheaper option, as it reuses the session (The session stores in session cache all objects used, so that less database access are required if you access the same object twice within the same session)


Top
 Profile  
 
 Post subject: ThreadLocalSessionContext class
PostPosted: Mon Nov 05, 2007 5:56 am 
Newbie

Joined: Wed Oct 31, 2007 6:27 am
Posts: 5
Thank you for the reply.

Ok Rober2D2, I am agree with you.

I just don't understand the difference of state between the two instances. I want to implement the "session per request" pattern using the ThreadLocalSessionContext class.

When I do "openSession()" or "getCurrentSession()" in a new thread I got a new session. How to put the two session in the same state?

Doing this test :

Code:
org.hibernate.Session sessionHibernate = com.apis.b7afn.wda.mapping.util.HibernateUtil.getSessionFactory().openSession();//getCurrentSession();////
   System.out.println("active : "+sessionHibernate.getTransaction().isActive());
   System.out.println("open : "+sessionHibernate.isOpen());
   System.out.println("connected : "+sessionHibernate.isConnected());


I have the results :

with openSession() :

active : false
open : true
connected : true


with getCurrentSession() :

active : false
open : true
org.hibernate.HibernateException: isConnected is not valid without active transaction
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:297)
at $Proxy0.isConnected(Unknown Source)
at org.apache.jsp.Public.test_jsp._jspService(org.apache.jsp.Public.test_jsp:49)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:99)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:325)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)


I don't now how to open the connection without opening a transaction....


Top
 Profile  
 
 Post subject: Re: ThreadLocalSessionContext class
PostPosted: Mon Nov 05, 2007 6:28 am 
Beginner
Beginner

Joined: Tue Oct 30, 2007 7:57 am
Posts: 47
Keeg wrote:
Thank you for the reply.

Ok Rober2D2, I am agree with you.

I just don't understand the difference of state between the two instances. I want to implement the "session per request" pattern using the ThreadLocalSessionContext class.

When I do "openSession()" or "getCurrentSession()" in a new thread I got a new session. How to put the two session in the same state?

Doing this test :

Code:
org.hibernate.Session sessionHibernate = com.apis.b7afn.wda.mapping.util.HibernateUtil.getSessionFactory().openSession();//getCurrentSession();////
   System.out.println("active : "+sessionHibernate.getTransaction().isActive());
   System.out.println("open : "+sessionHibernate.isOpen());
   System.out.println("connected : "+sessionHibernate.isConnected());


I have the results :

with openSession() :

active : false
open : true
connected : true


with getCurrentSession() :

....

I don't now how to open the connection without opening a transaction....


I don't know the exact difference between the two methods. It seems that openSession initializes something in the session, so it must be called once per Thread to get a new session. Once you've called openSession within a Thread, you may call getCurrentSession as many times as needed.

And something I read in the ThreadLocalSesionContext docs:

Quote:
In the interest of usability, it was decided to have this default impl actually generate a session upon first request and then clean it up after the Transaction associated with that session is committed/rolled-back. In order for ensuring that happens, the sessions generated here are unusable until after Session.beginTransaction() has been called


http://www.hibernate.org/hib_docs/v3/ap ... ntext.html


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 05, 2007 7:09 pm 
Newbie

Joined: Thu Oct 25, 2007 3:37 pm
Posts: 17
Actually, I think you just call getCurrentSession(), if you want to do the thread bound thing, and it will make a new session if one doesn't currently exist. Its a completely different thing that openSession().

I suspect that if you call openSession() first, it creates a normal session. If you then call getCurrentSession(), it creates another new session and binds it to the thread. Then if you call getcurrentSession() subsequent to this, it will return that thread bound session.

Also, I think all of your work is supposed to occur with in the bounds of a transaction, so you should probably be manually creating the transaction even with the openSession() method. I'm kind of suprized that it seems to be automatically making one for you. Perhaps you have another transaction manager configured in your environemnt that hibernate will automatically pick up on, i.e. JTA.


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.