Announcement Announcement Module
Collapse
No announcement yet.
Multiple transaction managers, no session found. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:

    Code:
    @Transactional("txManagerHSQL")
    public class LocalSynchroServiceImpl extends SynchroServiceImpl implements LocalSynchroService {
    
        public List<Synchro> findAll() {
            return getDaoAgent().getSynchroDao().findAllLocal();
        }
    }
    2nd service class

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

    Code:
    <tx:annotation-driven />
    <bean id="txManagerHSQL" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactoryHSQL"/>
    </bean>
    <bean id="txManagerMySQL" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactoryMySQL"/>
    </bean>
    Exception:
    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:941)
        at pl.mbrnwsk.clientsRegister.dao.DaoImpl.getCurrentSession(DaoImpl.java:75)
    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.


    Mark

    Comment

    Working...
    X