开发者

No Hibernate Session bound to thread

I'm using Struts + Hibernate + Spring for my project development. And here is my Spring Context XML file. When I called "sessionFactory.getCurrentSession()" in the beginning of userDao.getXXXX method, the exception whose detail message is "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here" was thrown.

<!-- Hibernate Configuration -->
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property> 

</bean>

<!-- Spring Transaction Manager -->
<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref bean="sessionFactory"/>    
    </property>
</bean>

<!-- Spring Transaction Descriptions -->
<bean id="transactionAttributeSource"
    class="org.springframework.transaction.interceptor.MethodMapTransactionAttributeSource">
    <property name="methodMap">
        <map>
            <entry key="com.miaozhen.monitor.service.LoginServiceImpl.*">
                <value>PROPAGATION_REQUIRED</value>
            </entry>
        </map>
    </property>
</bean>

<bean id="transactionInterceptor"
    class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager">
        <ref bean="tr开发者_C百科ansactionManager"/>
    </property>
    <property name="transactionAttributeSource">
        <ref bean="transactionAttributeSource"/>    
    </property>
</bean>

<bean id="transactionAdvisor"
    class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
    <constructor-arg>
        <ref bean="transactionInterceptor"/>
    </constructor-arg>
</bean>

<bean id="autoproxy"
    class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">     
</bean>

<!-- DAO -->
<bean id="userDao"
    class="com.miaozhen.dbservice.hibernate.dao.AUserDAO">
    <property name="sessionFactory">
        <ref local="sessionFactory"/>   
    </property>
</bean>

<!-- Service Layer -->

<bean id="loginService"
    class="com.miaozhen.monitor.service.LoginServiceImpl">
    <property name="userDao">
        <ref bean="userDao"/>
    </property>
</bean>

<!-- Struts Actions for DelegatingActionProxy -->
<bean name="/login"
    class="com.miaozhen.monitor.struts.action.LoginAction">
    <property name="loginService">
        <ref bean="loginService"/>
    </property>
</bean>


That's quite a complicated configuration, but I suspect that the loginService bean is somehow not being proxied with the transactionAdvisor, even though I can see that's what you're trying to do.

Try making sure that the loginService that's being injected into the controller is in fact a generated proxy object rather than the raw LoginServiceImpl object. A debugger would also be very useful in making sure that the code execution passes through the TransactionInterceptor.

Is there a reason you're doing things this way? There are much easier ways to achieve the same thing, which don't involve creating advisors, autoproxy factories, transaction attribute sources, and so on. For example, using <tx:annotation-driven> and @Transactional make this stuff easy. Maybe it's because your current approach means you should have no references to Spring in your code is what appealed to you?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜