title: getCurrentSession call fails right after openSession
body: <p>Hi I have configured sessionFactory and MysqL datasource with transaction manager using hibernate. When I try to call getCurrentSession() on that factory right after openSession(), it throws HibernateException. How do I make it work?  </p>
  
  <p>Stacktrace of error:</p>
  
  <pre><code>org.hibernate.HibernateException: No Session found for current thread
  at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
  at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:978)
  at com.xyz.CommonTest.initTx(CommonTest.java:56)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:616)
  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
  at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
  at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
  at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
  </code></pre>
  
  <p>Spring configuration:</p>
  
  <pre><code>&lt;bean id="sessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="relationalDataSource"&gt;&lt;/property&gt;
      &lt;property name="packagesToScan"&gt;
          &lt;list&gt;
              &lt;value&gt;....&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;${hibernate.dialect}&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
              &lt;prop key="hibernate.hbm2ddl.auto"&gt;${hibernate.hbm2ddl.auto}  &lt;/prop&gt;
              &lt;prop key="hibernate.format_sql"&gt;true&lt;/prop&gt;
              &lt;prop key="hibernate.order_updates"&gt;true&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
  &lt;bean id="transactionManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory"&gt;&lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Java code:</p>
  
  <pre><code>    logger.info("initTx called");
      // Either get a current session or open new one.
      Session session = null;
      try {
          session = sessionFactory.getCurrentSession();
      } catch (HibernateException exception) {
          logger.info("did not find a current session, will try to open a new one",
                      exception);
      }
      session = sessionFactory.openSession();
      tx = session.beginTransaction();
  
      try {
          session = sessionFactory.getCurrentSession();
      } catch (HibernateException exception) {
          logger.info("did not find a current session, will try to open a new one",
                      exception);
      }
  </code></pre>
  
  <p>edit: </p>
  
  <p>You can do this by using. </p>
  
  <p>hibernate.current_session_context_class=thread
  <a href="http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/session-configuration.html" rel="nofollow">http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/session-configuration.html</a></p>
  
  <p>If you are looking for an automatic way to do this, then Ryan's answer below id correct. </p>