没有Hibernate Session绑定到线程异常

时间:2010-12-30 12:48:59

标签: java hibernate spring

我有Hibernate 3.6.0.Final和Spring 3.0.0.RELEASE

我得到“没有Hibernate会话绑定到线程,配置不允许在这里创建非事务性的”

如果我重新添加线程规范,我会得到“saveOrUpdate在没有活动事务的情况下无效”

有什么想法吗?

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>

和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();
    }
}

5 个答案:

答案 0 :(得分:2)

Spring版本不是你的问题。

我还建议不要用交易注释你的DAO。那些属于注入了DAO的服务层。这也是会话进入的地方:打开用例的会话,执行它,关闭事务,清理它。

答案 1 :(得分:1)

您忘记启用annotation driven transaction management。在链接中,搜索tx:advice

答案 2 :(得分:1)

添加

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

<tx:annotation-driven /> 

到你的上下文文件。

答案 3 :(得分:0)

在hibernate.cfg.xml文件中注释该属性

thread

答案 4 :(得分:0)

添加事务管理器:

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

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

并使用@Transactional

注释所有uro类