title: org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
body: <p>I'm using Spring 4.3.8 and Hibernate 5.2.10 and I'm trying to create table in database and insert records to that but I'm getting Error</p>
  
  <p>Dao Class</p>
  
  <pre><code>@Transactional
      public void saveDayBookData(DayBookData dayBook){
          hibernateTemplate.getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO);
          hibernateTemplate.saveOrUpdate(dayBook);
      }
  </code></pre>
  
  <p>For all spring and hibernate configuration</p>
  
  <p>applicationContext.xml </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:p="http://www.springframework.org/schema/p"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
          http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"&gt;
  
  
      &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver"&gt;&lt;/property&gt;
          &lt;property name="url" value="jdbc:mysql://localhost:3306/db1?autoReconnect=true&amp;amp;useSSL=false"&gt;&lt;/property&gt;
          &lt;property name="username" value="root"&gt;&lt;/property&gt;
          &lt;property name="password" value="root"&gt;&lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="mysessionFactory"
          class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"&gt;
          &lt;property name="packagesToScan" value="entityClasses"&gt;&lt;/property&gt;
          &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQL5Dialect&lt;/prop&gt;
                  &lt;prop key="hibernate.hbm2ddl.auto"&gt;create&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="template" class="org.springframework.orm.hibernate5.HibernateTemplate"&gt;
          &lt;property name="sessionFactory" ref="mysessionFactory"&gt;&lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
      &lt;bean id="transactionManager"
          class="org.springframework.orm.hibernate5.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="mysessionFactory"&gt;&lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="vendor" class="vendors.VendorDao"&gt;
          &lt;property name="hibernateTemplate" ref="template"&gt;&lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="dayBook" class="accountBooks.DayBookDao"&gt;
          &lt;property name="hibernateTemplate" ref="template"&gt;&lt;/property&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Main class</p>
  
  <pre><code>public static void main(String[] args) {
          // TODO Auto-generated method stub
  
          Resource r = new ClassPathResource("applicationContext.xml");
          BeanFactory bean = new XmlBeanFactory(r);
  
          DayBookDao dayBookDao = (DayBookDao) bean.getBean("dayBook");
          DayBookData dayBook =  new DayBookData();
          dayBook.setAccountType("Bank Account");
          dayBook.setTransType("Receipt");
          dayBook.setOppAccount("Profit-Loss");
          dayBook.setAmount(15000);
          dayBook.setTransDate(new Date());
          dayBookDao.saveDayBookData(dayBook);
      }
  </code></pre>
  
  <p>I'm getting this error</p>
  
  <pre><code>Exception in thread "main" org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
      at org.springframework.orm.hibernate5.SpringSessionContext.currentSession(SpringSessionContext.java:133)
      at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:456)
      at accountBooks.DayBookDao.saveDayBookData(DayBookDao.java:22)
      at accountBooks.TestDayBook.main(TestDayBook.java:28)
  </code></pre>