Service层进行事务管理,Dao层获得session进行事务处理抛异常的情况

   1.  spirng中配置service层的事务管理

       在一个web项目中,如果使用SSH框架,一般在spring中配置事务管理。我们一般不会在dao层使用transaction,事务被配置在service层上更为合理,因为业务层方法表示逻辑上的一个原子操作。

         spring中配置service层的事务管里,配置文件中的配置如下:

      <bean id="txProxy"   
         class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">    
        <property name="beanNames">    
          <list>        
             <value>*Service</value> //这里表示
          </list>    
        </property>    
        <property name="interceptorNames">    
          <list>    
             <value>transactionInterceptor</value>    
          </list>    
        </property>    
   </bean> 
 

      <bean id="txManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>
 
 
  <!-- 配置事务拦截器-->    
    <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">    
            <property name="transactionManager">     
                  <ref bean="txManager" />    
           </property> <!-- 配置事务属性 --> 
       <property name="transactionAttributes">    
          <props>    
            <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>    
            <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
            <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>   
            <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>    
            <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
            <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
            <prop key="create*">PROPAGATION_REQUIRED,-Exception</prop>
          </props>    
       </property>    
     </bean>      

2,我尝试在Dao层获取sesssion,然后进行事务处理。代码如下

  Session session=manureDao.getHibernateTemplate().getSessionFactory().getCurrentSession();
  session.beginTransaction();
  ManureInfo manureInfo=(ManureInfo)session.load(ManureInfo.class, 15);
  System.out.println( manureInfo.getAreaNum());
  session.getTransaction().commit();
  session.close();

然后报以下异常:

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
 at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
 at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:687)
 at cn.sinobest.scscms.test.process.TestManure.testUpdateManureInfo(TestManure.java:81)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
 at java.lang.reflect.Method.invoke(Unknown Source)
 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
 at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
 at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:274)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:242)
 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:58)
 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:240)
 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:48)
 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:233)
 at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
 at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
 at org.junit.runners.ParentRunner.run(ParentRunner.java:303)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

原因:使用sessionfactory.getcurrentsession执行hibernate操作时,hibernate的操作默认必须包含在一个transaction中,

            也就是开始要用session.begiontransaction得到一个transaction 实例(譬如tx), 操作结束时在这个实例上进行事务的

           提交tx.commit或回滚tx.rollback. 如果这些crud操作不被包括在一个具体的transaction中,hibernate就会抛出上述异常。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值