zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    如何更好地理解闭包
    抽象类和抽象方法以及和接口区别
    JavaScript中如何理解如何理解Array.apply(null, {length:5})
    Java线程中的同步
    Python前世今生以及种类、安装环境
    大数据中的用户画像
    Java web每天学之Servlet工作原理详情解析
    Go语言操作MySQL数据库
    老集群RAC双网卡绑定
    nmcli配置ipv6
  • 原文地址:https://www.cnblogs.com/boblogsbo/p/5274875.html
Copyright © 2011-2022 走看看