程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Spring交易和hibernate.current_session_context_class大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Spring交易和hibernate.current_session_context_class?

开发过程中遇到Spring交易和hibernate.current_session_context_class的问题如何解决?下面主要结合日常开发的经验,给出你关于Spring交易和hibernate.current_session_context_class的解决方法建议,希望对你解决Spring交易和hibernate.current_session_context_class有所启发或帮助;

当使用spring和spring管理的事务时, *

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

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

解决方法

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

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

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

org.hibernate.HibernateException: saveOrupdate is not valid without active transaction
@H_674_20@

但是,我的服务方法带有注释@transactional,并且在添加之前@R_423_6944@<prop key="hibernate.current_session_context_class">thread</prop>

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

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/scheR_531_11845@a/beans"
xmlns:xsi="http://www.w3.org/2001/XMLscheR_531_11845@a-instance"
xmlns:tx="http://www.springframework.org/scheR_531_11845@a/tx"
xsi:scheR_531_11845@aLOCATIOn="
    http://www.springframework.org/scheR_531_11845@a/beans http://www.springframework.org/scheR_531_11845@a/beans/spring-beans-3.1.xsd
    http://www.springframework.org/scheR_531_11845@a/tx http://www.springframework.org/scheR_531_11845@a/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"/>
@H_674_20@
                

大佬总结

以上是大佬教程为你收集整理的Spring交易和hibernate.current_session_context_class全部内容,希望文章能够帮你解决Spring交易和hibernate.current_session_context_class所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。