spring.net 的事物管理

 在spring.net中实现事物管理,可以有两种方式:

1.编程式事物

 

编程式事物有如下几步,

a.

 

 

 

 

2.Ioc容器注入事物

 

在通过aop注入事物时有多种配置方式

A.在Ioc容器中声明事物,然后在应用事物的方法上通过.net的attribute机制来应用事物。

示例代码:

application.xml代码:

<object id="transactionManager"
          type="Spring.Data.Core.TxScopeTransactionManager, Spring.Data">
  </object>


  <!-- Transaction aspect -->
  <tx:attribute-driven/>

 

应用事物的方法:

[Transaction(TransactionPropagation.Required)]
        public bool Save(Models.Student student)
        {
            object obj = HibernateTemplate.Save(student);
            return obj != null;
        }

 

但是必须注意的一点是,如果在nunit下测试时必须通过容器来提交

 

示例代码如下:


        [Test]
        public void TestStudentDAL()
        {
            bool bo = StudentDAL().Save(new Student("李四", "男", 22, "zhangsan@163.com"));
            transactionManager.Commit(transactionStatus);
            Assert.IsNotNull(bo);
        }

 

 

B:采用Aop的另外是用方式

 

 

如果Ioc容器中的配置是

<object id="transactionManager"
          type="Spring.Data.Core.ServiceDomainPlatformTransactionManager, Spring.Data">
  </object>


  <!-- Transaction aspect -->
  <tx:attribute-driven/>

 

这是针对于web服务的一种配置方式

 

C:这种配置方式是针对于使用Data Access数据访问的一种方式

<object id="transactionManager"
          type="Spring.Data.Core.AdoPlatformTransactionManager, Spring.Data">
    <property name="DbProvider" ref="DebitDbProvider"/>
  </object>

  <!-- Transaction aspect -->
 
  <tx:attribute-driven/>

 

D:切面方式的配置

<object id="txAttributePointcut" type="Spring.Aop.Support.AttributeMatchMethodPointcut, Spring.Aop">
    <property name="Attribute" value="Spring.Transaction.Interceptor.TransactionAttribute, Spring.Data"/>
  </object>

  <aop:config>

    <aop:advisor id="exceptionProcessAdvisor" order="1"
               advice-ref="exceptionAdvice"
               pointcut-ref="txAttributePointcut"/>

  </aop:config>

 

 

E:最后一种配置方式

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值