-->
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.  [ 9 posts ] 
Author Message
 Post subject: session.isConnected() ?
PostPosted: Tue Mar 13, 2007 3:21 am 
Newbie

Joined: Wed Feb 07, 2007 4:34 am
Posts: 8
Hi All,

What exactly does session.isConnected() do?

when i call this api on the session object returned from a newSession using sessionFactory.openSession()

>>it prints true.

does it say whether this session object has a jdbc connection opened?

But if so, hibernate doc says that, a connection to a database is obtained and sql statements are triggered only when a transaction is commited or before some hql query.

moreover it states that, creating sessions is inexpensive, so this means that creating new session does not immedeately obtain a connection to a database otherwise how session creation is inexpensive.

Can somebody demistify this.

Thanks,
Lakshmi


Top
 Profile  
 
 Post subject: session.isConnected
PostPosted: Tue Mar 13, 2007 4:59 am 
Beginner
Beginner

Joined: Tue Dec 12, 2006 7:52 am
Posts: 20
hi
that session.isConnected() it takes the JDBC connection from the hibernate session it gives tru value


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 13, 2007 5:42 am 
Newbie

Joined: Wed Feb 07, 2007 4:34 am
Posts: 8
apparently, it intrigues me more,
meaning that, since the session.isConnected is returning true just after opening a new session from the sessionfactory and even before calling any persist, query apis or any trans.commits.

Then how does session creation become inexpensive?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 13, 2007 5:47 am 
Newbie

Joined: Tue Mar 13, 2007 4:15 am
Posts: 12
Location: Trivandrum
lakshmi_raga wrote:
apparently, it intrigues me more,
meaning that, since the session.isConnected is returning true just after opening a new session from the sessionfactory and even before calling any persist, query apis or any trans.commits.

Then how does session creation become inexpensive?


hi;

Once u opened a session means it will renain opened until u r closing the
particular session u have to close the session if its is of no use further. u can use threadLocale design pattern. in wthe session will be closed automatically while u r commiting a transaction....

_________________
Regards
Belto


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 13, 2007 8:40 am 
Newbie

Joined: Wed Feb 07, 2007 4:34 am
Posts: 8
The answers i am receiving for this question are totally off the topic...


Top
 Profile  
 
 Post subject: sessionFactory.openSession() will open DB connection
PostPosted: Wed Mar 14, 2007 2:20 am 
Newbie

Joined: Wed Mar 07, 2007 3:26 am
Posts: 16
Location: Hyderabad,India
hi lakshmi, this session.isConnected() method returns true/false whether the session is opend() or closed() . but what u think is after calling
Transaction.commit() only we will get connection to the DB no thats correct i think so..as soon as u call sessionFactory.openSession() will get a DBconnection and a gets a session on it , after tx.commit() only ur all operations will hit DB once and makes the updates...

concluding that : sessionFactory().openSession() method only gets the connection not tx.commit().....see sessionFactory in api ok

_________________
sam


Top
 Profile  
 
 Post subject: sessionFactory.openSession() will open DB connection
PostPosted: Wed Mar 14, 2007 2:23 am 
Newbie

Joined: Wed Mar 07, 2007 3:26 am
Posts: 16
Location: Hyderabad,India
hi lakshmi, this session.isConnected() method returns true/false whether the session is opend() or closed() . but what u think is after calling
Transaction.commit() only we will get connection to the DB no thats correct i think so..as soon as u call sessionFactory.openSession() will get a DBconnection and a gets a session on it , after tx.commit() only ur all operations will hit DB once and makes the updates...

concluding that : sessionFactory().openSession() method only gets the connection not tx.commit().....see sessionFactory in api ok

_________________
sam


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 14, 2007 2:57 am 
Newbie

Joined: Wed Feb 07, 2007 4:34 am
Posts: 8
if session.isConnected() specifies whether the session is open or closed then what does session.isOpen() do?

ok, sessionFactory API says that openSession creates a new connection on it.
Suppose i have a getCurrentSession api defined to maintain session-per-request, and i close the session after each operation within the unit of work, then this session is saved within threadlocal space.
Do you mean that the session holds a connection for the entire duration?

And if I open 100 new sessions, according to the sessionFactory API,
each session will have a connection opened.
The author for this api is Gaving King.
But the book "Hibernate in Action" written by Gaving King says that unless it is necessary hibernate does not even require even a connection?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 14, 2007 9:55 am 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
The documentation for the hibernate.connection.release_mode property may be helpful:

Quote:
Specify when Hibernate should release JDBC connections. By default, a JDBC connection is held until the session is explicitly closed or disconnected. For an application server JTA datasource, you should use after_statement to aggressively release connections after every JDBC call. For a non-JTA connection, it often makes sense to release the connection at the end of each transaction, by using after_transaction. auto will choose after_statement for the JTA and CMT transaction strategies and after_transaction for the JDBC transaction strategy.

eg. auto (default) | on_close | after_transaction | after_statement

Note that this setting only affects Sessions returned from SessionFactory.openSession. For Sessions obtained through SessionFactory.getCurrentSession, the CurrentSessionContext implementation configured for use controls the connection release mode for those Sessions. See Section 2.5, “Contextual Sessions”


My understanding is that if hibernate.connection.release_mode is set to auto, connections are obtained when the session is opened, and released when the session is closed. If it's set to after_transaction, I think it doesn't obtain a connection until session.beginTransaction (and then releases it after transaction.commit()).

Anyway, obtaining a connection should not be expensive because you should be using a connection pool. The connections have already been created, you are just checking it out of the pool (an inexpensive operation).

_________________
nathan


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