zoukankan      html  css  js  c++  java
  • Spring framework3.2整合hibernate4.1报错:No Session found for current thread

    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
    <property name="dataSource" ref="dataSource" /> 
    </bean> 
    <tx:advice id="txAdvice" transaction-manager="txManager"> 
    <tx:attributes> 
    <tx:method name="save*" propagation="REQUIRED"/> 
    <tx:method name="add*" propagation="REQUIRED"/> 
    <tx:method name="create*" propagation="REQUIRED"/> 
    <tx:method name="insert*" propagation="REQUIRED"/> 
    <tx:method name="*" read-only="true" /> 
    </tx:attributes> 
    </tx:advice> 
    
    <aop:config proxy-target-class="true"> 
    <aop:advisor advice-ref="txAdvice" pointcut="execution(* net.noday..service..*.*(..))" /> 
    </aop:config>

    getCurrentSession需要做如下配置:

    <propertyname="current_session_context_class" >thread</property>

    1. 前者打开一个新的,后者当前有session的话,则是使用当前的session,没有的话则创建一个新的;

    2. 如果使用前者获得一个session的话,需要手动关闭session,使用后者,当事务提交,session会自动关闭,如果再关闭session则会报如下异常:Session has already closed;

    3. sf.getCurrentSession方法需要在hibernate.cfg.xml做如下配置:

        <propertynamepropertyname="current_session_context_class" >thread</property>  

    没有配置的话会报如下异常:org.hibernate.HibernateException:No CurrentSessionContext configured!

  • 相关阅读:
    DapperExtensions 使用教程
    C#事件订阅及触发例子
    01、类与结构的区别
    内网信息搜集
    Anydesk拿下远程桌面
    C++对注册表的操作
    Linux下常见流编辑器的使用
    Typecho1.1反序列化漏洞复现
    Nmap脚本编写
    Github学习
  • 原文地址:https://www.cnblogs.com/exmyth/p/3184061.html
Copyright © 2011-2022 走看看