title: How to resolve the Exception org.hibernate.SessionException: Session was already closed
body: <p>I am new to Hibernate and I was doing some programming myself until I encountered this problem. I am trying to add a object into the database and then retrieve that object from the database. I have tried looking online for help but haven't been able to resolve this error. Any help would be appreciated. The stack trace I get:</p>
  
  <pre class="lang-none prettyprint-override"><code>Exception in thread "main" org.hibernate.SessionException: Session was already closed
      at org.hibernate.internal.SessionImpl.close(SessionImpl.java:359)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:356)
      at com.sun.proxy.$Proxy6.close(Unknown Source)
      at com.project.hibernate.HibernateTest.main(HibernateTest.java:39) 
  </code></pre>
  
  <p>This is the code I have written so far:</p>
  
  <p><strong>HiberateTest.java</strong></p>
  
  <pre><code>public class HibernateTest {
  
      public static void main(String[] args) {
          Logger log = LoggerFactory.getLogger(HibernateTest.class);
  
          UserDetails user = new UserDetails();
          log.debug("Testing");
          user.setUserId(1);
          user.setUserName("First User");
          user.setAddress("First user's address");
          user.setJoinedDate(new Date());
          user.setDescription("user description");
  
          Configuration configuration = new Configuration();
          configuration.configure("com/project/hibernate/hibernate.cfg.xml");
          ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
                  configuration.getProperties()).build();
          SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
  
          Session session = sessionFactory.getCurrentSession();
  
          //add object
          session.beginTransaction();
          session.save(user);
          session.getTransaction().commit();
          session.close();
  
          //retrieve object that was added
          session = sessionFactory.openSession();
          user = null;
          session.beginTransaction();
          user =  (UserDetails) session.get(UserDetails.class,1);
          session.getTransaction().commit();
          session.close();
  
          System.out.println("User Name retrieved is " + user.getUserName());
  
      }
  }
  </code></pre>
  
  <p><strong>UserDetails.java</strong></p>
  
  <pre><code>@Entity
  @Table(name="USER_DETAILS")
  public class UserDetails {
      @Id
      private int userId;
  
      private String userName;
      @Temporal(TemporalType.DATE)
  
      private Date joinedDate;
      private String address;
  
      private String description;
  
      public Date getJoinedDate() {
          return joinedDate;
      }
      public void setJoinedDate(Date joinedDate) {
          this.joinedDate = joinedDate;
      }
      public String getAddress() {
          return address;
      }
      public void setAddress(String address) {
          this.address = address;
      }
      public String getDescription() {
          return description;
      }
      public void setDescription(String description) {
          this.description = description;
      }
  
      public int getUserId() {
          return userId;
      }
      public void setUserId(int userId) {
          this.userId = userId;
      }
      public String getUserName() {
          return userName;
      }
      public void setUserName(String userName) {
          this.userName = userName;
      }
  
  }
  </code></pre>
  
  <p><strong>hibernate.cfg.xml</strong></p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;
  
  &lt;hibernate-configuration&gt;
  
      &lt;session-factory&gt;
  
          &lt;!-- Database connection settings --&gt;
          &lt;property name="connection.driver_class"&gt;org.h2.Driver&lt;/property&gt;
          &lt;property name="connection.url"&gt;jdbc:h2:~/test&lt;/property&gt;
          &lt;property name="connection.username"&gt;sa&lt;/property&gt;
          &lt;property name="connection.password"&gt;v15t4.&lt;/property&gt;
  
          &lt;!-- JDBC connection pool (use the built-in) --&gt;
          &lt;property name="connection.pool_size"&gt;1&lt;/property&gt;
  
          &lt;!-- SQL dialect --&gt;
          &lt;property name="dialect"&gt;org.hibernate.dialect.H2Dialect&lt;/property&gt;
  
          &lt;!-- Disable the second-level cache --&gt;
          &lt;property name="cache.provider_class"&gt;org.hibernate.cache.internal.NoCacheProvider&lt;/property&gt;
  
          &lt;!-- Echo all executed SQL to stdout --&gt;
          &lt;property name="show_sql"&gt;true&lt;/property&gt;
  
          &lt;!-- Drop and re-create the database schema on startup --&gt;
          &lt;property name="hbm2ddl.auto"&gt;create&lt;/property&gt;
  
          &lt;property name="hibernate.current_session_context_class"&gt;
              org.hibernate.context.internal.ThreadLocalSessionContext
          &lt;/property&gt;
  
          &lt;mapping class="com.project.hibernate.UserDetails" /&gt;
  
      &lt;/session-factory&gt;
  
  &lt;/hibernate-configuration&gt;
  </code></pre>