jQuery in Action, 2nd edition*
The moose likes Object Relational Mapping and the fly likes Hibernate connection close issue. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate connection close issue." Watch "Hibernate connection close issue." New topic

Hibernate connection close issue.

chets patel
Ranch Hand

Joined: Apr 27, 2010
Posts: 73

Below is sample hibernate program.

I am using MS ACCESS database and I created my own dialect.
Below is configuration file.

<property name="hibernate.connection.driver_class">sun.jdbc.odbc.JdbcOdbcDriver</property>
<property name="hibernate.connection.url">jdbcdbc:mydsn</property>
<property name="hibernate.connection.username"></property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">com.hibernate.dialect.MSAccessDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.connection.release_mode">auto</property>
<property name="hibernate.current_session_context_class">jta</property>
<property name="connection.release_mode">after_statement</property>
<property name="transaction.auto_close_session">true</property>
<property name="max_fetch_depth">2</property>

When I run the above program, I am getting proper values but I am not able to close the connection.
There is ldb file which indicates that connection is not closed.

One more thing I want to ask is how can I come to know that connection is successfully closed if I am using some standard database like mySQL or Oracle.

My next doubt is what is the difference between
session.save(); and
Reddy Prashanth
Ranch Hand

Joined: Aug 09, 2005
Posts: 61
Hibernate internally uses connection pool and when you call session.close(), the connection is returned to the pool.
All the connections will be closed when you close the sessionfactory.

By default, hibernate will execute the sql statements when you the transaction is committed. so that all the operations in one session will be executed in one shot . You can ask hibernate to execute the sql anywhere within the session by calling flush(). Also, you can change this behavior by setting the flush mode on session like
chets patel
Ranch Hand

Joined: Apr 27, 2010
Posts: 73

Hi Prashanth
Thanks for your reply.

Can you please share the best practices for hibernate
I want to use session per request strategy.
How can I implement that.
Thanks in advance
chets patel
Ranch Hand

Joined: Apr 27, 2010
Posts: 73

One more thing I wanted to ask is
If I want to use CMT (Container Managed Transaction), is it mandatory to use EJB.
Cant I use CMT without EJB in hibernate?
Reddy Prashanth
Ranch Hand

Joined: Aug 09, 2005
Posts: 61
You can find the best practices in the hibernate reference documentation chapter 24.

Session per request can be implemented using a filter/interceptors. "Open session in View " (OSIV) pattern is a best fit for most of the applications. Please go through this article.

CMT makes sense in the context of EJB. However,I guess, with Spring declarative transaction support, we can avoid EJBs and gain similar benefits. May be someone from the forum can answer it more accurately
I agree. Here's the link: http://aspose.com/file-tools
subject: Hibernate connection close issue.
Similar Threads
Could not parse mapping document from resource PojoTestTable.hbm.xml
hibernate with struts
InvalidMappingException in Hibernate
query.list() and query.uniqueResult() in hibernate return null
Hibernate Maping error