当前位置:编程学习 > JAVA >>

关于spring3中No Session found for current thread!and Transaction的配置和管理

今天我是特别的郁闷,本来项目做到一半,以前都好好的,结果下午就出现问题,苦逼的到现在才解决。它出现问题的时候都一声不坑,,(天啦,现在才发现CSDN啥时候把QQ表情给整过来了)就在注册用户的时候,咦,后台发现咋SQL语句特么的不对劲,仔细一看数据根本就没有送到数据库去,只是简单的执行了一下查询操作,当时我就震惊了。首先就去看了Action是否没有写save方法,结果是没有任何错误。后来我再去再其他业务层是否也出现了问题,结果都能正常操作,让我哭笑不得。后来只能查配置。当时我就看到这两个配置是在项目中同时出现的,天啦,我在惊叹,项目以前是怎么运行起来的。

[html] 
<SPAN style="FONT-SIZE: 14px"><prop key="hibernate.current_session_context_class">thread</prop></SPAN> 

<prop key="hibernate.current_session_context_class">thread</prop>
 

[html] 
<SPAN style="FONT-SIZE: 14px"><prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop></SPAN> 

<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
这两个配置在最新的spring3.1.1和hibernate中4.1.3中都是多余的,

那是因为在Spring事务管理中,current Session是绑定到SpringSessionContext中的,而不是ThreadLocalSessionContext中的

而我的事务特性也是在spring配置了的,hibernate也交由了spring管理。spring真是个大管家啊,

 [html] 
<SPAN style="FONT-SIZE: 14px"><!--配置事务管理  --> 
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
        <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 
     
    <!-- 配置事务特性 --> 
    <tx:advice id="txAdvice" transaction-manager="transactionManager"> 
        <tx:attributes> 
             <tx:method name="exists" read-only="true" />  
        <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="update*" propagation="REQUIRED" />    
        <tx:method name="merge*" propagation="REQUIRED" />    
        <tx:method name="del*" propagation="REQUIRED" />    
        <tx:method name="remove*" propagation="REQUIRED" />    
        <tx:method name="put*" propagation="REQUIRED" />    
        <tx:method name="use*" propagation="REQUIRED"/>    
        <!-- hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到    --> 
        <tx:method name="get*" propagation="REQUIRED" />    
        <tx:method name="count*" propagation="REQUIRED" read-only="true" />    
        <tx:method name="find*" propagation="REQUIRED" read-only="true" />    
        <tx:method name="list*" propagation="REQUIRED" read-only="true" />   
         <tx:method name="*" propagation="REQUIRED" />  
             
        </tx:attributes> 
    </tx:advice> 
 
     
    <!-- 配置哪些类的方法进行事务管理 --> 
    <aop:config proxy-target-class="true"> 
        <aop:pointcut id="bussinessService" expression="execution(* com.shop.service..impl.*.*(..))" /> 
        <aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" /> 
    </aop:config> 
    <aop:config proxy-target-class="true"> 
        <aop:pointcut id="dao" expression="execution(* com.shop.dao.*.*(..))" /> 
        <aop:advisor pointcut-ref="dao" advice-ref="txAdvice" /> 
    </aop:config></SPAN> 

<!--配置事务管理  -->
 <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory"/>
 </bean>
 
 <!-- 配置事务特性 -->
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
    <tx:method name="exists" read-only="true" />
  <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="update*" propagation="REQUIRED" />  
        <tx:method name="merge*" propagation="REQUIRED" />  
        <tx:method name="del*" propagation="REQUIRED" />  
        <tx:method name="remove*" propagation="REQUIRED" />  
        <tx:method name="put*" propagation="REQUIRED" />  
        <tx:method name="use*" propagation="R

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,