Announcement Announcement Module
No announcement yet.
Multiple transaction managers, no session found. Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Multiple transaction managers, no session found.

    I need to have two transaction managers in my Spring application. I use them by annotating service classes by @Transactional("nameOfTxManager"). Howewer, I am only able to use one of them - when I call some method of service class annotated with second transaction manager, I get "org.hibernate.HibernateException: No Session found for current thread" exception. Here is some code that reflects problem:

    1st service class:

    public class LocalSynchroServiceImpl extends SynchroServiceImpl implements LocalSynchroService {
        public List<Synchro> findAll() {
            return getDaoAgent().getSynchroDao().findAllLocal();
    2nd service class

    public class RemoteSynchroServiceImpl extends SynchroServiceImpl implements RemoteSynchroService {
        public List<Synchro> findAll() {
            return getDaoAgent().getSynchroDao().findAllRemote();
    Piece of configuration connected with transactions:

    <tx:annotation-driven />
    <bean id="txManagerHSQL" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactoryHSQL"/>
    <bean id="txManagerMySQL" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactoryMySQL"/>
    org.hibernate.HibernateException: No Session found for current thread
        at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(
        at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(
        at pl.mbrnwsk.clientsRegister.dao.DaoImpl.getCurrentSession(
    The annotation of 2nd service class is interpreted correctly, because when I change name of transaction manager to some random name, I get the exception that such bean was not found. And also, when I set name as @Transactional("txManagerHSQL"), it works fine but with not the transaction manager I want.

  • #2
    Well, it looks like both are using the same dao, which probably has the same sessionFactory injected into it. So the dao has only one of the sessionFactories, pointing to one of the datasources, being used with only one possible TransactionManager.

    So when you call sessionFactory.getCurrentSession, it will only work with the one and only one TransactionManager tied to that sessionFactory. So the other TransactionManager won't work with it.