title: "Could not obtain transaction-synchronized Session for current thread" error in Spring transaction management
body: <p>I am working on migrating an application from Spring 3.2  to Spring 5. The application uses AWS RDS with a primary database as primary or master datasource and a replica database as read-only datasource. The application creates one session factory ( primarySessionFacotry) instance for the primary data source and another one ( readOnlySessionFactory) for the read-only data source so that the same DAO can be used for both datasource by wiring each session factories.</p>
  <p><a href="https://i.stack.imgur.com/lz3hb.png" rel="nofollow noreferrer">appContext-hibernate.xml</a></p>
  <pre><code>&lt;bean id=&quot;primaryDataSource&quot; class=&quot;com.mchange.v2.c3p0.ComboPooledDataSource&quot; destroy-method=&quot;close&quot;&gt;
      &lt;property name=&quot;driverClass&quot;&gt;
          &lt;value&gt;com.mysql.jdbc.Driver&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name=&quot;jdbcUrl&quot;&gt;
          &lt;value&gt;${database.jdbcurl}&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name=&quot;user&quot;&gt;
          &lt;value&gt;${database.dbuser}&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name=&quot;password&quot;&gt;
          &lt;value&gt;${database.dbpassword}&lt;/value&gt;
      &lt;/property&gt;
  
  &lt;/bean&gt;
  
  &lt;bean id=&quot;primarySessionFactory&quot;
        class=&quot;org.springframework.orm.hibernate5.LocalSessionFactoryBean&quot;&gt;
      &lt;property name=&quot;dataSource&quot; ref=&quot;primaryDataSource&quot;/&gt;
      &lt;property name=&quot;mappingResources&quot; ref=&quot;hbmFileLocations&quot;&gt;
      &lt;/property&gt;
      &lt;property name=&quot;hibernateProperties&quot;&gt;
          &lt;props&gt;
              &lt;prop key=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
              &lt;prop key=&quot;hibernate.show_sql&quot;&gt;${hibernate.showSQL}&lt;/prop&gt;
              &lt;prop key=&quot;hibernate.format_sql&quot;&gt;${hibernate.formatSQL}&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;util:list id=&quot;hbmFileLocations&quot; value-type=&quot;java.lang.String&quot;&gt;
      &lt;value&gt;com/xxxx/yyyyy/persistence/mappings/users.hbm.xml&lt;/value&gt;
  &lt;/util:list&gt;
  
  &lt;bean id=&quot;primaryTransactionManager&quot; class=&quot;org.springframework.orm.hibernate5.HibernateTransactionManager&quot;&gt;
      &lt;property name=&quot;sessionFactory&quot; ref=&quot;primarySessionFactory&quot;&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;tx:annotation-driven transaction-manager=&quot;primaryTransactionManager&quot;/&gt;
  </code></pre>
  <p><a href="https://i.stack.imgur.com/po3nT.png" rel="nofollow noreferrer">appContext-hibernate-ro.xml</a></p>
  <pre><code>&lt;bean id=&quot;readOnlyDataSource&quot; class=&quot;com.mchange.v2.c3p0.ComboPooledDataSource&quot; destroy-method=&quot;close&quot;&gt;
      &lt;property name=&quot;driverClass&quot;&gt;
          &lt;value&gt;com.mysql.jdbc.Driver&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name=&quot;jdbcUrl&quot;&gt;
          &lt;value&gt;${database.readonly.jdbcurl}&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name=&quot;user&quot;&gt;
          &lt;value&gt;${database.readonly.dbuser}&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name=&quot;password&quot;&gt;
          &lt;value&gt;${database.readonly.dbpassword}&lt;/value&gt;
      &lt;/property&gt;
      
  &lt;/bean&gt;
  
  &lt;bean id=&quot;readOnlySessionFactory&quot;
        class=&quot;org.springframework.orm.hibernate5.LocalSessionFactoryBean&quot;&gt;
      &lt;property name=&quot;dataSource&quot; ref=&quot;readOnlyDataSource&quot;/&gt;
      &lt;property name=&quot;mappingResources&quot; ref=&quot;hbmFileLocations&quot;&gt;
      &lt;/property&gt;
      &lt;property name=&quot;hibernateProperties&quot;&gt;
          &lt;props&gt;
              &lt;prop key=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
              &lt;prop key=&quot;hibernate.show_sql&quot;&gt;${hibernate.readonly.showSQL}&lt;/prop&gt;
              &lt;prop key=&quot;hibernate.format_sql&quot;&gt;${hibernate.readonly.formatSQL}&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  
  &lt;/bean&gt;
  
  &lt;bean id=&quot;readOnlyTransactionManager&quot; class=&quot;org.springframework.orm.hibernate5.HibernateTransactionManager&quot;&gt;
      &lt;property name=&quot;sessionFactory&quot; ref=&quot;readOnlySessionFactory&quot;/&gt;
  &lt;/bean&gt;
  
  &lt;tx:annotation-driven transaction-manager=&quot;readOnlyTransactionManager&quot;/&gt;
  </code></pre>
  <p>DAOs are instantiated like this</p>
  <pre><code>&lt;bean id=&quot;userDao&quot; class=&quot;xxxxx.persistence.UserDao&quot;&gt;
      &lt;property name=&quot;sessionFactory&quot; ref=&quot;primarySessionFactory&quot;/&gt;
  &lt;/bean&gt;
  &lt;bean id=&quot;readOnlyUserDao&quot; class=&quot;xxxxx.persistence.UserDao&quot;&gt;
      &lt;property name=&quot;sessionFactory&quot; ref=&quot;readOnlySessionFactory&quot;/&gt;
  &lt;/bean&gt;
  </code></pre>
  <p>This UserDao has a method like below</p>
  <pre><code>@Transactional
  public List&lt;User&gt; getItems()
  {
      return sessionFactory.getCurrentSession().createQuery(&quot;from User user where isDeleted=false&quot;).list();
  }
  </code></pre>
  <p>We are getting below exception when using the UserDao.getItems() method with readOnlyUserDao intance.</p>
  <pre><code>org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
  at org.springframework.orm.hibernate5.SpringSessionContext.currentSession(SpringSessionContext.java:143)
  at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:497)
  </code></pre>
  <p>Please note that the same code is perfectly working with Spring ( spring-orm and spring-tx 3.2)</p>
  <p>Please assist to resolve this.</p>