小编典典

Spring Transactions和hibernate.current_session_context_class

spring

我有一个使用Hibernate 4和Spring Transactions的Spring 3.2应用程序。所有方法都运行良好,我可以正确访问数据库以保存或检索实体。然后,我介绍了一些多线程,并且由于每个线程都在访问数据库,所以我从Hibernate中收到以下错误:

org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions

我从网上读到我必须添加thread到Hibernate配置中的内容,但是现在每次我尝试访问数据库时,都会得到:

org.hibernate.HibernateException: saveOrUpdate is not valid without active transaction

但是,我的服务方法带有注释@Transactional,并且在添加之前一切工作正常<prop key="hibernate.current_session_context_class">thread</prop>

尽管方法带有@Transactional注释,但为什么没有事务?我怎么解决这个问题?

这是我的Hibernate配置(包括会话上下文属性):

<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

<!-- Hibernate session factory -->
<bean
    id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
    <property name="dataSource" >
        <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties" >
        <props>
            <prop key="hibernate.hbm2ddl.auto">create</prop> 
            <prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.current_session_context_class">thread</prop>  
        </props>
    </property>   
    <property name="annotatedClasses" >
        <list>
            ...
        </list>
    </property> 
</bean>

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

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

阅读 473

收藏
2020-04-13

共1个答案

小编典典

当使用spring和spring管理的事务时,除非你使用的是JTA,否则请不要乱用hibernate.current_session_context_class属性。

默认情况下,Spring会设置自己的CurrentSessionContext实现(SpringSessionContext),但是如果你自行设置,则情况并非如此。基本上破坏了适当的交易集成。

更改此设置的唯一原因是,每当你要使用JTA管理的事务时,都必须进行设置以与JTA正确集成。

2020-04-13