title: Hibernate Could not obtain transaction-synchronized Session for current thread
body: <p>I have been having a similar problem as many people have been having, but I can't seem to figure out what is going wrong in my specific case. I am making a simple database call to test the database connection, and Hibernate is throwing the following exception:</p>
  
  <pre><code>Exception in thread "main" org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
  at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134)
  at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
  at boardwalk.computeServer.dao.DbDaoHibernateImpl.getInterpolationJob(DbDaoHibernateImpl.java:73)
  at boardwalk.computeServer.ComputeServer.test(ComputeServer.java:39)
  at boardwalk.computeServer.ComputeServer.main(ComputeServer.java:32)
  </code></pre>
  
  <p>Here is the relevant code and configuration:</p>
  
  <p>pom.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema  instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-    4.0.0.xsd"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
    &lt;groupId&gt;boardwalk&lt;/groupId&gt;
    &lt;artifactId&gt;computeServer&lt;/artifactId&gt;
    &lt;version&gt;1.0&lt;/version&gt;
    &lt;packaging&gt;jar&lt;/packaging&gt;
  
    &lt;name&gt;marketserver&lt;/name&gt;
    &lt;url&gt;http://maven.apache.org&lt;/url&gt;
  
    &lt;properties&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
    &lt;/properties&gt;
  
    &lt;dependencies&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;junit&lt;/groupId&gt;
        &lt;artifactId&gt;junit&lt;/artifactId&gt;
        &lt;version&gt;4.11&lt;/version&gt;
        &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
      &lt;version&gt;4.1.1.RELEASE&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;mysql&lt;/groupId&gt;
      &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
      &lt;version&gt;5.1.33&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-beans&lt;/artifactId&gt;
      &lt;version&gt;4.1.1.RELEASE&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-aspects&lt;/artifactId&gt;
      &lt;version&gt;4.1.1.RELEASE&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
      &lt;version&gt;4.1.1.RELEASE&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;javax.transaction&lt;/groupId&gt;
      &lt;artifactId&gt;jta&lt;/artifactId&gt;
      &lt;version&gt;1.1&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;activesoap&lt;/groupId&gt;
      &lt;artifactId&gt;jaxp-api&lt;/artifactId&gt;
      &lt;version&gt;1.3&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
      &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
      &lt;version&gt;4.3.7.Final&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.apache.activemq&lt;/groupId&gt;
      &lt;artifactId&gt;activemq-all&lt;/artifactId&gt;
      &lt;version&gt;5.10.0&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.apache.activemq&lt;/groupId&gt;
      &lt;artifactId&gt;activemq-spring&lt;/artifactId&gt;
      &lt;version&gt;5.10.0&lt;/version&gt;
      &lt;type&gt;xsd&lt;/type&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-jms&lt;/artifactId&gt;
      &lt;version&gt;4.1.1.RELEASE&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-tx&lt;/artifactId&gt;
      &lt;version&gt;4.1.1.RELEASE&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
      &lt;version&gt;4.1.1.RELEASE&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;c3p0&lt;/groupId&gt;
      &lt;artifactId&gt;c3p0&lt;/artifactId&gt;
      &lt;version&gt;0.9.1.2&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.hibernate.javax.persistence&lt;/groupId&gt;
      &lt;artifactId&gt;hibernate-jpa-2.0-api&lt;/artifactId&gt;
      &lt;version&gt;1.0.1.Final&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;net.rforge&lt;/groupId&gt;
      &lt;artifactId&gt;REngine&lt;/artifactId&gt;
      &lt;version&gt;0.6-8.1&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;net.rforge&lt;/groupId&gt;
      &lt;artifactId&gt;Rserve&lt;/artifactId&gt;
      &lt;version&gt;0.6-8.1&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.R&lt;/groupId&gt;
      &lt;artifactId&gt;RSession&lt;/artifactId&gt;
      &lt;version&gt;1.0&lt;/version&gt;
      &lt;/dependency&gt;
    &lt;/dependencies&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>Hibernate DAO (mapping objects have been omitted for clarity, as I don't think they are causing the issue, as the exception is being thrown before any of them can be used):</p>
  
  <pre><code>import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.springframework.dao.DataAccessException;
  
  import boardwalk.computeServer.data.InterpolationDesiredPoint;
  import boardwalk.computeServer.data.InterpolationJob;
  
  public class DbDaoHibernateImpl implements DbDao {
  
      private final SessionFactory sessionFactory;
  
      public DbDaoHibernateImpl(SessionFactory sessionFactory){
          // Save the session factory
          this.sessionFactory = sessionFactory;
      }
  
  
      @Override
      public InterpolationJob getInterpolationJob(int jobId) {
  
          // Get the session
          Session s = sessionFactory.getCurrentSession();
  
          // Load the object from the database and return it
          return (InterpolationJob) s.get(InterpolationJob.class, jobId);
  
      }
  
  }
  </code></pre>
  
  <p>Main class:</p>
  
  <pre><code>import org.apache.log4j.Logger;
  import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
  import org.springframework.transaction.annotation.Transactional;
  
  import boardwalk.computeServer.dao.DbDao;
  
  public class ComputeServer {
  
      private static final Logger LOG = Logger.getLogger(ComputeServer.class);
  
      /**
       * Runs the server
       * @param args none
       */
      public static void main(String[] args) {
  
          // Create the application context
          @SuppressWarnings("resource")
          ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/beans.xml");
      context.registerShutdownHook();
  
          // Log the server start
          LOG.info("Server has started");
  
          test(context);
      }
  
  
     @Transactional
     public static void test(ClassPathXmlApplicationContext context){
        DbDao dao = (DbDao) context.getBean("dbDao");
        System.err.println(dao.getInterpolationJob(1));
     }
  
  }
  </code></pre>
  
  <p>Spring configuration file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
      http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-4.1.xsd"&gt;
  
  &lt;!-- Load the properties file --&gt;
  &lt;context:property-placeholder location="classpath:application.properties" /&gt;
  
  &lt;!-- C3PO pooled database connections --&gt;
  &lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
      destroy-method="close"&gt;
      &lt;property name="driverClass" value="${jdbc.driverClassName}" /&gt;
      &lt;property name="jdbcUrl" value="${jdbc.url}" /&gt;
      &lt;property name="user" value="${jdbc.username}" /&gt;
      &lt;property name="password" value="${jdbc.password}" /&gt;
  
      &lt;!-- These are C3P0 properties --&gt;
      &lt;property name="acquireIncrement" value="1" /&gt;
      &lt;property name="minPoolSize" value="1" /&gt;
      &lt;property name="maxPoolSize" value="10" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- Hibernate session factory --&gt;
  &lt;bean id="hibernateSessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="packagesToScan" value="boardwalk.computeServer.data" /&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;${jdbc.dialect}&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;!-- The database DAO --&gt;
  &lt;bean id="dbDao" class="boardwalk.computeServer.dao.DbDaoHibernateImpl"&gt;
      &lt;constructor-arg ref="hibernateSessionFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- The transaction manager --&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="hibernateSessionFactory" /&gt;
  &lt;/bean&gt;
  
  
  &lt;context:annotation-config&gt;&lt;/context:annotation-config&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>I should note that the program runs fine when I substitute "getCurrentSession()" with "openSession()". </p>
  
  <p>Thank you in advance!</p>