Spring.Net+WCF实现分布式事务

  1.在教师服务端,我们需要在WCF的接口上加上WCF分布式事务特性:

[OperationContract]
[TransactionFlow(TransactionFlowOption.Allowed)]
bool AddTeacher(TeacherViewModel vmTeacher);

      在教师服务端的B层实现上加上WCF分布式事务特性:

 /// <summary>
        /// 增加学生
        /// </summary>
        /// <param name="vmTeacher">教师ViewModel</param>
        /// <returns>布尔值</returns>
         [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public bool AddTeacher(TeacherViewModel vmTeacher)
        {
            //创建转换规则
            Mapper.CreateMap<TeacherViewModel, TeacherEntity>();
            //转换实体
            TeacherEntity enStudent = Mapper.Map<TeacherEntity>(vmTeacher);
            //调用dal层增加方法
            this.CurrentDal.Add(enStudent);
            //提交事务,返回结果
            return this.DbSession.SaveChanges() > 0;
        }

   2.在学生服务端的B层采用system.transactions进行分布式事务处理:

```

public void AddStudentTeacher(StudentViewModel vmStudent)
{
//创建转换规则
Mapper.CreateMap<StudentViewModel, StudentEntity>();
//转换实体
StudentEntity enStudent = Mapper.Map<StudentEntity>(vmStudent);

        //调用dal层增加方法
        this.CurrentDal.Add(enStudent);
        this.DbSession.SaveChanges();

        //调用老师的添加方法
        ITeacherContracts teacherContracts = ServiceFactory.GetTeacherService();

        TeacherViewModel vmTeacher = new TeacherViewModel { TeacherID = "123", TeacherName = "11" };
        teacherContracts.AddTeacher(vmTeacher);
    }
       3.由于每个事务处理都需要using TransactionScope、 trans.Complete,所以我们用Spring.Net的AOP管理这些相同的处理:

public class AroundAdvice : IMethodInterceptor
{
public object Invoke(IMethodInvocation invocation)
{
object result;
using (TransactionScope trans = new TransactionScope())
{
result = invocation.Proceed();
trans.Complete();
}
return result;
}
}


   4.配置数据库服务器的MSDTC服务:
     4.1开启MSDTC服务,在DOS中输入net start msdtc;

     4.2设置MSDTC,在DOS中输入dcomcnfg.exe,按下图所示设置:
![](https://s1.51cto.com/images/blog/201810/28/9ab6fb5d815622f1cad479fe7af7375f.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
      4.3关闭windows防火墙;

     4.4开启数据库的分布式事务支持,设置如下图所示:
     5.![](https://s1.51cto.com/images/blog/201810/28/a1fc537db968195eef6d55adc98c5400.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
     6. 当前版本是用system.transactions实现的分布式事务,用Spring.Net的AOP切了一下

欢迎工作一到五年的Java工程师朋友们加入Java架构开发:855801563 获取更多免费视频教程。

合理利用自己每一分每一秒的时间来学习提升自己,不要再用"没有时间“来掩饰自己思想上的懒惰!趁年轻,使劲拼,给未来的自己一个交代

转载于:https://blog.51cto.com/13981400/2309975

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值