Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate connection close issue.

 
chets patel
Ranch Hand
Posts: 77
Eclipse IDE Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
session.flush();
 
Reddy Prashanth
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
session.setFlushmode(FlushMode.AUTO)
 
chets patel
Ranch Hand
Posts: 77
Eclipse IDE Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
Eclipse IDE Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic