Hibernate 春季及;休眠:没有绑定到线程的会话

Hibernate 春季及;休眠:没有绑定到线程的会话,hibernate,spring,session,transactions,Hibernate,Spring,Session,Transactions,试图让Spring的事务管理工作起来,但并不像我希望的那样 请求任何需要我的数据库的内容时,我会遇到异常: DEBUG: org.springframework.orm.hibernate3.SessionFactoryUtils - Opening Hibernate Session DEBUG: org.springframework.orm.hibernate3.SessionFactoryUtils - Opening Hibernate Session DEBUG: org.hiber

试图让Spring的事务管理工作起来,但并不像我希望的那样

请求任何需要我的数据库的内容时,我会遇到异常:

DEBUG: org.springframework.orm.hibernate3.SessionFactoryUtils - Opening Hibernate Session
DEBUG: org.springframework.orm.hibernate3.SessionFactoryUtils - Opening Hibernate Session
DEBUG: org.hibernate.impl.SessionImpl - opened session at timestamp: 12897642913
DEBUG: org.springframework.orm.hibernate3.SessionFactoryUtils - Closing Hibernate Session
DEBUG: org.springframework.orm.hibernate3.SessionFactoryUtils - Closing Hibernate Session
14-nov-2010 20:51:31 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet mvc-dispatcher threw exception
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
我已经将我的属性移动到我的Spring上下文中,看看这是否更好,但没有。 我的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <bean id="myDataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost/kidscalcula" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>

    <bean class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
        id="sessionFactory">
        <property name="dataSource" ref="myDataSource" />
        <property name="mappingResources">
            <list>
                <value>be/howest/kidscalcula/model/Foto.hbm.xml</value>
                <value>be/howest/kidscalcula/model/Kindleerplanonderdeel.hbm.xml
                </value>
                <value>be/howest/kidscalcula/model/Klas.hbm.xml</value>
                <value>be/howest/kidscalcula/model/Leerkracht.hbm.xml</value>
                <value>be/howest/kidscalcula/model/Leerling.hbm.xml</value>
                <value>be/howest/kidscalcula/model/Leerplan.hbm.xml</value>
                <value>be/howest/kidscalcula/model/LeerplanOefenreeks.hbm.xml
                </value>
                <value>be/howest/kidscalcula/model/Leerplanonderdeel.hbm.xml</value>
                <value>be/howest/kidscalcula/model/Niveau.hbm.xml</value>
                <value>be/howest/kidscalcula/model/Oefenreeks.hbm.xml</value>
                <value>be/howest/kidscalcula/model/Overgangsregel.hbm.xml</value>
                <value>be/howest/kidscalcula/model/Rapport.hbm.xml</value>
                <value>be/howest/kidscalcula/model/RapportLeerplanonderdeel.hbm.xml
                </value>
                <value>be/howest/kidscalcula/model/Schooljaar.hbm.xml</value>
                <value>be/howest/kidscalcula/model/Subonderdeel.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.connection.pool_size">3</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.use_sql_comments">true</prop>
                <prop key="hibernate.cache.use_second_level_cache">false</prop>
            </props>
        </property>
    </bean>


    <!-- Transaction manager for a single Hibernate SessionFactory (alternative 
        to JTA) -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>

    <tx:annotation-driven />
</beans>

有没有人知道我忘记了什么,配置错误?基本思想是,在这种配置下,只要在我的服务层中调用事务方法,就会打开会话。这还允许我在相同的事务方法中加载惰性集合。但由于某些原因,它甚至找不到线程。

由于您的配置看起来很好,有几个可能的原因:

  • 对使用
    new
    创建的对象调用
    @Transactional
    方法(即,不是从Spring获得的)

  • 您从同一对象的另一个方法调用
    @Transactional
    方法(在这种情况下,事务方面不应用,因为它是基于代理的)

  • 使用
    @Transactional
    方法的对象在
    无效的上下文中声明(例如,您的
    @Transactional
    对象在
    ..-servlet.xml
    中声明,而
    仅在
    applicationContext.xml
    中声明)


它是从Spring MVC控制器调用的,在那里注入服务。前两种可能性都不适用。至于第三个:可能就是这样!我来测试一下。然而,我并没有按照spring的建议拆分配置;只需要一个root-context.xml,它导入了另外3个xml,我的servlet dispatcher、hibernateconfiguration和另一个JSON。非常感谢。在这个问题上我已经绞尽脑汁两天了。没想到会有这么琐碎的事!但我很高兴,我学到了关于春天的另一件我永远不会忘记的事情;)获取异常的堆栈跟踪。TransactionInterceptor是否出现在其中?我发现了问题。我的注释扫描发生在另一个xml中,然后是我的。它从未发现它必须启动事务,因此从未找到会话。
@Repository
public class LeerlingDAOimpl implements LeerlingDAO {
    @Autowired
    public LeerlingDAOimpl(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }