-->
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.  [ 4 posts ] 
Author Message
 Post subject: Connection Question
PostPosted: Tue Nov 04, 2008 2:33 pm 
Newbie

Joined: Tue Nov 04, 2008 2:22 pm
Posts: 4
I am new to hibernate but have been helping someone debug an issue they were having.

In the code they were getting the hibernate connection to reuse it outside of the hibernate transaction like this:

Code:

Session sesssion = SessionFactory.openSession();
.
.
.
Connection conn = session.connection();
PreparedStatement pstmt = conn.prepareStatement("someQuery");
.
.
.
conn.close();


Now the problem here is obviously that the connection shouldn't be being reused and closed like this. However, when the session is closed and a subsequent call to SessionFactory.OpenSession() is made the session it returns generates errors because the connection is closed.

It was my understanding that when calling openSession() from a SessionFactory it got a new connection.

Can someone explain how the SessionFactory is actually handling the connections?

There is nothing in hibernate.cfg.xml that specifies any kind of connection pooling.

Thanks in advance,

Ian


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2008 3:53 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
The creation of connections is managed in "the environment" in which your Hibernate application runs.
There can be a great variety of environments:
a simple Java client application, a Hibernate/Spring application running under a web server, a JPA or EJB application running on an application server, etc.

Evidently in your case, "the environment" was counting on that particular connection to continue existing. Thus, your assumption that one fresh connection is created every time a session is obtained is wrong. (It would be very expensive, resource-wise).

You should try not to acquire connections in that fashion, and you should most certainly never close the connections acquired in that way, because you would be making your Hibernate code assume a responsibility (managing connection state) that is not his.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2008 4:00 pm 
Newbie

Joined: Tue Nov 04, 2008 2:22 pm
Posts: 4
I understand that this is a bad thing to do and should never be done...I was fixing someone elses code where this was the issue.

The is a stand alone java app that doesnt use any external connection pool and so hibernate handles making the connection.

I'm simply trying to understand how the connections are handled as the API seems to be misleading.

From the hibernate API:

Quote:
openSession

public Session openSession()
throws HibernateException

Create database connection and open a Session on it.

Returns:
Session
Throws:
HibernateException


This would seem to imply that if i call openSession a database connection is created. If this was the case and a connection is being "created" I wouldnt expect to see that the connection is closed.

This being misleading I just want to understand how this actually works for my own learning benefit


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 12:25 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
A connection is not created.
The obtained connection should not be closed.

_________________
Gonzalo Díaz


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