Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Does committing transaction closes session?

 
Sagar Kale
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you comment the line and run the application, are the changes saved to the database when the transaction is commited?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sagar Kale
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Paul and Sridhar, it worked properly with openSession().
 
naveen putrevu
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic