Announcement Announcement Module
Collapse
No announcement yet.
Trouble with Multiple Session Factories/Transaction Managers Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trouble with Multiple Session Factories/Transaction Managers

    I am trying to connect to two different data sources with two different session factories and two transaction managers. All the services that use the dao's are in the same directory (in case that matters).

    It loads the session factories just fine, however when I attempt to do anything with the database in my application it throws this error:
    org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
    org.springframework.orm.hibernate3.SpringSessionCo ntext.currentSession(SpringSessionContext.java:63)
    org.hibernate.impl.SessionFactoryImpl.getCurrentSe ssion(SessionFactoryImpl.java:544)
    edu.suu.taskman.dao.hibernate.StandardDao.getCurre ntSession(StandardDao.java:26)
    edu.suu.taskman.dao.hibernate.PersonDaoImpl.getPer sonByUsername(PersonDaoImpl.java:67)
    edu.suu.taskman.service.impl.UserServiceImpl.loadU serByUsername(UserServiceImpl.java:31)
    org.springframework.security.providers.cas.CasAuth enticationProvider.authenticateNow(CasAuthenticati onProvider.java:146)
    org.springframework.security.providers.cas.CasAuth enticationProvider.authenticate(CasAuthenticationP rovider.java:131)
    org.springframework.security.providers.ProviderMan ager.doAuthentication(ProviderManager.java:195)
    org.springframework.security.AbstractAuthenticatio nManager.authenticate(AbstractAuthenticationManage r.java:46)
    org.springframework.security.ui.cas.CasProcessingF ilter.attemptAuthentication(CasProcessingFilter.ja va:75)
    org.springframework.security.ui.AbstractProcessing Filter.doFilterHttp(AbstractProcessingFilter.java: 249)
    org.springframework.security.ui.SpringSecurityFilt er.doFilter(SpringSecurityFilter.java:53)
    org.springframework.security.util.FilterChainProxy $VirtualFilterChain.doFilter(FilterChainProxy.java :371)
    org.springframework.security.ui.logout.LogoutFilte r.doFilterHttp(LogoutFilter.java:87)
    org.springframework.security.ui.SpringSecurityFilt er.doFilter(SpringSecurityFilter.java:53)
    org.springframework.security.util.FilterChainProxy $VirtualFilterChain.doFilter(FilterChainProxy.java :371)
    org.springframework.security.ui.SessionFixationPro tectionFilter.doFilterHttp(SessionFixationProtecti onFilter.java:68)
    org.springframework.security.ui.SpringSecurityFilt er.doFilter(SpringSecurityFilter.java:53)
    org.springframework.security.util.FilterChainProxy $VirtualFilterChain.doFilter(FilterChainProxy.java :371)
    org.springframework.security.context.HttpSessionCo ntextIntegrationFilter.doFilterHttp(HttpSessionCon textIntegrationFilter.java:235)
    org.springframework.security.ui.SpringSecurityFilt er.doFilter(SpringSecurityFilter.java:53)
    org.springframework.security.util.FilterChainProxy $VirtualFilterChain.doFilter(FilterChainProxy.java :371)
    org.springframework.security.util.FilterChainProxy .doFilter(FilterChainProxy.java:174)
    org.springframework.web.filter.DelegatingFilterPro xy.invokeDelegate(DelegatingFilterProxy.java:183)
    org.springframework.web.filter.DelegatingFilterPro xy.doFilter(DelegatingFilterProxy.java:138)
    org.springframework.orm.hibernate3.support.OpenSes sionInViewFilter.doFilterInternal(OpenSessionInVie wFilter.java:198)
    org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:75)
    My session factories are set up this way:

    Code:
    <bean id="taskSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    		<property name="dataSource" ref="dataSource"/>
    		<property name="configLocation" value="classpath:hibernate.cfg.xml"/>		
    	</bean>
    	
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    		<property name="dataSource" ref="otherDataSource"/>
    		<property name="configLocation" value="classpath:hibernate.other.cfg.xml"/>		
    	</bean>
    My Transaction Managers are set up this way:

    Code:
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="taskSessionFactory"/>
    	</bean>
    	
    	<tx:advice id="txAdvice" transaction-manager="transactionManager">
    		<tx:attributes>			
    			<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
    			<tx:method name="save*" propagation="REQUIRES_NEW" read-only="false"/>
    			<tx:method name="delete*" propagation="REQUIRED" read-only="false"/>
    			<tx:method name="index*" propagation="REQUIRED" read-only="false"/>
    			<tx:method name="send*" propagation="REQUIRED" read-only="true"/>
    		</tx:attributes>
    	</tx:advice>	
    	<aop:config>
    		<aop:pointcut id="serviceOperations" expression="execution(* edu.suu.taskman.service.*.*(..))"/>
        	<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperations"/>
    	</aop:config>
    	
    	<bean id="otherTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory"/>
    	</bean>
    	
    	<tx:advice id="otherTxAdvice" transaction-manager="otherTransactionManager">
    		<tx:attributes>			
    			<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
    		</tx:attributes>
    	</tx:advice>	
    	<aop:config>
    		<aop:pointcut id="otherServiceOperations" expression="execution(* edu.suu.taskman.service.*.*(..))"/>
        	<aop:advisor advice-ref="otherTxAdvice" pointcut-ref="otherServiceOperations"/>
    	</aop:config>
    This is my first time trying to set up multiple session factories so I'm sure I just made a simple little error... Any help would be much appreciated! Thanks.

  • #2
    For the first sight, it looks like your method was not adviced by txAdvice or otherTxAdvice. Check the pointcut configuration by making sure the match your method invocation in the code. According to stacktrace you provided either
    Code:
    edu.suu.taskman.dao.hibernate.PersonDaoImpl.getPersonByUsername(PersonDaoImpl.java:67)
    or
    Code:
    edu.suu.taskman.service.impl.UserServiceImpl.loadUserByUsername(UserServiceImpl.java:31)
    should be adviced by txAdvice otherwise code is executed out of hibernate session. Looking at your pointcut and advice definition I don't see a matching one.

    Comment

    Working...
    X