• Exception in thread "main" org.hibernate.HibernateException: save is not valid without active transaction


    在spring4+hibernate4整合过程中,使用@Transactional注解事务会报"Exception in thread "main" org.hibernate.HibernateException: save is not valid without active transaction"错误,具体错误如下:

    Exception in thread "main" org.hibernate.HibernateException: save is not valid without active transaction
    at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)
    at $Proxy18.save(Unknown Source)
    at com.zainagou.dao.UserDaoImpl.addUser(UserDaoImpl.java:23)
    at com.zainagou.dao.UserDaoImpl$$FastClassBySpringCGLIB$$98e1b102.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
    at com.zainagou.dao.UserDaoImpl$$EnhancerBySpringCGLIB$$ac7c23a8.addUser(<generated>)
    at com.zainagou.dao.MyTest.main(MyTest.java:19)

    ,后来发现是由于使用了如下配置导致上述问题:

    <!-- session绑定当前线程 -->
    <prop key="hibernate.current_session_context_class">thread</prop>

    改配置在单独使用hibernate时进行当前线程绑定,在spring+hiberante整合中不需要这句配置,

    spring提供了一个SpringSessionContext这个类来帮你获得和管理session
  • 相关阅读:
    16、集合--Map接口
    LVM快照备份与恢复
    CentOS 6.3下配置LVM(逻辑卷管理)
    Tomcat 9.0 配置问题 403 Access Denied
    nginx和php-fpm调用方式
    Linux下安装php环境并且配置Nginx支持php-fpm模块
    Nginx 服务器安装及配置文件详解
    Linux编辑器vim键盘详解
    Nginx配置文件详解
    keepalive配置文件详解
  • 原文地址:https://www.cnblogs.com/boblogsbo/p/5274875.html
Copyright © 2020-2023  润新知