简体   繁体   English

没有Hibernate Session绑定到线程异常

[英]No Hibernate Session bound to thread exception

I have Hibernate 3.6.0.Final and Spring 3.0.0.RELEASE 我有Hibernate 3.6.0.Final和Spring 3.0.0.RELEASE

I get "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here" 我收到“没有Hibernate Session绑定到线程,并且配置不允许在此处创建非事务性会话”

If I add the thread specification back in, I get "saveOrUpdate is not valid without active transaction" 如果重新添加线程规范,则会收到“ saveOrUpdate在没有活动事务的情况下无效”

Any ideas? 有任何想法吗?

The spring-config.xml: spring-config.xml:

<context:annotation-config />
<context:component-scan base-package="com.maxheapsize" />

<bean id="dataSource"
         class="org.apache.commons.dbcp.BasicDataSource" 
         destroy-method="close">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
    <property name="url" value="jdbc:hsqldb:mem:jsf2demo"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
</bean>

<bean id="sampleSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="sampleDataSource"/>
    <property name="annotatedClasses">
        <list>
            <value>com.maxheapsize.jsf2demo.Book</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <!-- prop key="hibernate.connection.pool_size">0</prop-->
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
            <!-- prop key="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
            <prop key="hibernate.current_session_context_class">thread</prop-->
            <prop key="hibernate.hbm2ddl.auto">create</prop>
        </props>
    </property>
</bean>

 <bean id="sampleDataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName">
        <value>org.hsqldb.jdbcDriver</value>
    </property>
    <property name="url">
        <value>
            jdbc:hsqldb:file:/spring/db/springdb;SHUTDOWN=true
        </value>
    </property>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sampleSessionFactory"/>
</bean>

<bean id="daoTxTemplate" abstract="true"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager" ref="transactionManager"/>
    <property name="transactionAttributes">
        <props>
            <prop key="create*">
                PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED
            </prop>
            <prop key="get*">
                PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED
            </prop>
        </props>
    </property>
</bean>

<bean name="openSessionInViewInterceptor"
    class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
    <property name="sessionFactory" ref="sampleSessionFactory"/>
    <property name="singleSession" value="true"/>
</bean>

<bean id="nameDao" parent="daoTxTemplate">
    <property name="target">
        <bean class="com.maxheapsize.dao.NameDao">
            <property name="sessionFactory" ref="sampleSessionFactory"/>
        </bean>
    </property>
</bean>

and the DAO: 和DAO:

public class NameDao {
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    @Transactional
    @SuppressWarnings("unchecked")
    public List<Name> getAll() {
        Session session = this.sessionFactory.getCurrentSession();
        List<Name> names = (List<Name>)session.createQuery("from Name").list();
        return names;
    }

    //@Transactional(propagation= Propagation.REQUIRED, readOnly=false)
    @Transactional
    public void save(Name name){
        Session session = this.sessionFactory.getCurrentSession();
        session.saveOrUpdate(name);
        session.flush();
    }
}

Spring version isn't your issue. 春季版本不是您的问题。

I'd also recommend not annotating your DAO with transactions. 我也建议您不要在事务中注释DAO。 Those belong on a service tier that has the DAO injected in. That's where the session comes in as well: open the session for the use case, execute it, close the transaction, clean it up. 那些属于已注入DAO的服务层。会话也正是在其中进入的:为用例打开会话,执行会话,关闭事务,清理会话。

You forgot to enable annotation driven transaction management . 您忘记启用注释驱动的事务管理 In the link, search for tx:advice . 在链接中,搜索tx:advice

Add

<beans xmlns:tx="http://www.springframework.org/schema/tx

and

<tx:annotation-driven /> 

to your context file. 到您的上下文文件。

Comment the property in the hibernate.cfg.xml file 注释hibernate.cfg.xml文件中的属性

thread

add taransaction manager : 添加taransaction管理器:

    <bean class="org.springframework.orm.hibernate3.HibernateTransactionManager"
    id="hibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<tx:annotation-driven transaction-manager="hibernateTransactionManager"/>

and annotate all ur DAO classes with @Transactional 并使用@Transactional注释所有您的DAO类

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Hibernate异常:没有Hibernate Session绑定到线程 - Hibernate exception: No Hibernate Session bound to Thread @PostConstruct和“没有Hibernate Session绑定到线程”异常 - @PostConstruct and “No Hibernate Session bound to thread” exception Spring + Hibernate异常:没有绑定到线程的hibernate会话 - Spring+Hibernate Exception : No hibernate session bound to thread HibernateException:没有 Hibernate Session 绑定到线程 - HibernateException: No Hibernate Session bound to thread HibernateSystemException:没有 Hibernate Session 绑定到线程 - HibernateSystemException: No Hibernate Session bound to thread Hibernate 和 Spring3 事务管理注释-配置问题:休眠异常:没有 Hibernate Session 绑定到线程 - Hibernate and Spring3 Transaction Management Annotation-Configuration problems: Hibernate-Exception: No Hibernate Session bound to thread 带注释的Spring + Hibernate:没有Hibernate会话绑定到线程 - Spring + Hibernate with annotations: No Hibernate Session bound to thread Spring + Hibernate:没有Hibernate Session绑定到线程 - Spring + Hibernate: No Hibernate Session bound to thread HTTP状态500-请求处理失败; 嵌套的异常是org.hibernate.HibernateException:没有绑定到线程的Hibernate会话 - HTTP Status 500 - Request processing failed; nested exception is org.hibernate.HibernateException: No Hibernate Session bound to thread Spring MVC - 没有Hibernate Session绑定到线程 - Spring MVC - No Hibernate Session bound to thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM