Spring 事务和 hibernate.current_session_context_class

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18832889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:14:17  来源:igfitidea点击:

Spring Transactions and hibernate.current_session_context_class

springhibernatespring-transactionshibernate-session

提问by user1781028

I have a Spring 3.2 application that uses Hibernate 4 and Spring Transactions. All the methods were working great and I could access correctly the database to save or retrieve entities. Then, I introduced some multithreading, and since each thread was accessing to db I was getting the following error from Hibernate:

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

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

I read from the web that I've to add <prop key="hibernate.current_session_context_class">thread</prop>to my Hibernate configuration, but now every time I try to access the db I get:

我从网上读到我必须添加<prop key="hibernate.current_session_context_class">thread</prop>到我的 Hibernate 配置中,但现在每次我尝试访问数据库时,我都会得到:

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

However my service methods are annotated with @Transactional, and all was working fine before the add of <prop key="hibernate.current_session_context_class">thread</prop>.

但是,我的服务方法是用 注释的@Transactional,并且在添加<prop key="hibernate.current_session_context_class">thread</prop>.

Why there is no transaction although the methods are annotated with @Transactional? How can I solve this problem?

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

Here is my Hibernate configuration (including the session context property):

这是我的 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"/>

回答by M. Deinum

When using spring and spring managed transactions nevermess around with the hibernate.current_session_context_classproperty UNLESSyou are using JTA.

使用 spring 和 spring 托管事务时,除非您使用 JTA,否则永远不要弄乱hibernate.current_session_context_class属性。

Spring will by default set its own CurrentSessionContextimplementation (the SpringSessionContext), however if you set it yourself this will not be the case. Basically breaking proper transaction integration.

Spring 将默认设置自己的CurrentSessionContext实现(SpringSessionContext),但是如果您自己设置它,则情况并非如此。基本上打破了适当的交易整合。

The only reason for changing this setting is whenever you want to use JTA managed transactions, then you have to setup this to properly integrate with JTA.

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