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

  1.在教师服务端,咱们须要在WCF的接口上加上WCF分布式事务特性:数据库

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

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

/// <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;
}
}app

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

     4.2设置MSDTC,在DOS中输入dcomcnfg.exe,按下图所示设置:
![](https://s4.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://s4.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 获取更多免费视频教程。

合理利用本身每一分每一秒的时间来学习提高本身,不要再用"没有时间“来掩饰本身思想上的懒惰!趁年轻,使劲拼,给将来的本身一个交代
相关文章
相关标签/搜索