没有Hibernate Session绑定到线程

时间:2009-10-22 06:07:32

标签: hibernate spring struts transactions

我正在使用Struts + Hibernate + Spring进行项目开发。 这是我的Spring Context XML文件。当我在userDao.getXXXX方法的开头调用“sessionFactory.getCurrentSession()”时,抛出了一个异常,其详细消息是“没有Hibernate Session绑定到线程,并且配置不允许在这里创建非事务性的”。< / p>

<!-- 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="transactionManager"/>
    </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>

1 个答案:

答案 0 :(得分:0)

这是一个相当复杂的配置,但是我怀疑loginService bean在某种程度上没有被transactionAdvisor代理,尽管我可以看到你正在尝试做的事情。

尝试确保注入控制器的loginService实际上是生成的代理对象而不是原始LoginServiceImpl对象。调试器在确保代码执行通过TransactionInterceptor

时非常有用

你有这样做的原因吗?有更简单的方法来实现相同的功能,不涉及创建顾问程序,autoproxy工厂,事务属性源等。例如,使用<tx:annotation-driven>@Transactional可以轻松完成这些操作。也许是因为你当前的方法意味着你的代码中没有对Spring的引用是什么吸引你?

相关问题