File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Does committing transaction closes session? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Does committing transaction closes session?" Watch "Does committing transaction closes session?" New topic
Author

Does committing transaction closes session?

Sagar Kale
Ranch Hand

Joined: May 02, 2008
Posts: 188
Hi All,

I am new to hibernate.

Does session.getTransaction().commit() , closes the session? Because in the api documentation it is not mentioned that it closes the session. In my code I have

session.getTransaction().commit();
session.close();

But I get following exception

org.hibernate.SessionException: Session was already closed


If I remove session.close() , then I do not get this exception.

Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
When you comment the line and run the application, are the changes saved to the database when the transaction is commited?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Sridhar Santhanakrishnan wrote:When you comment the line and run the application, are the changes saved to the database when the transaction is commited?

This will not demonstrate whether the session was closed.

It depends on your sesison management configuration. If you have an interceptor or use transaction scoped sessions then commiting the transaction will close the session (assuming you did not open it explicitly in your transaction and instead used the current session). If you don't then it wont.

Have a read of this.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Sagar Kale
Ranch Hand

Joined: May 02, 2008
Posts: 188
This is how my code and configuration is

Session session = Util.getSessionFactory().getCurrentSession();

session.beginTransaction();
..
...
..


session.getTransaction().commit();
//session.close();


Yes changes are saved in database even when I have commented session.close().


my session factory configuration is like this.


<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbcracle:thin:scott/tiger@(description=(address_list=(address=(protocol=tcp)(host=host1)(port=1521)))(source_route=yes)(connect_data=(sid=Ora9i)(SERVICE_NAME = Ora9i)) )</property>
<property name="connection.username">scott</property>
<property name="connection.password">tiger</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hibernate.default_schema">scott</property>
<mapping resource="Emp.hbm.xml"/>

<!--mapping resource="test100.hbm.xml"-->

</session-factory>
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
<property name="current_session_context_class">thread</property>



Here, the hibernate session is bound to the current thread.
Hence, when the transaction is commited, the session is closed too.

Alternatively, you can use SessionFactory.openSession() if you want to manage the session yourself.

The link mentioned by Paul explains it in detail.

And yeah, thanks Paul.

Sagar Kale
Ranch Hand

Joined: May 02, 2008
Posts: 188
Thank you very much Paul and Sridhar, it worked properly with openSession().
naveen putrevu
Greenhorn

Joined: Sep 09, 2007
Posts: 22
When you say session.commit() -- if any CRUD operation performed on a row; then that particular transaction after the CRUD operation will be committed in the DB.
When you say session.close() -- the session will be closed instantly.

May be you might have accessed any relevant code pertaining to that session after saying session.close();
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Does committing transaction closes session?