您的位置:首页 > 其它

本地事务和分布式事务工作实践

2016-08-08 13:13 281 查看
摘要: 在关系型数据库中的事务是通过begin transaction,rollback transaction, commit 等关键字来实现事务的。

支持本地事务。本地事务相对简单,这儿不作重点简述。
  2:分布式事务
  理解分布式事务是怎样实现的,事务提交树是关键。
  事务提交树:事务提交树的根是事务初始化服务所在的机器的DTC,它在整个事务提交过程中充当着总协调者,又被称为全局提交协调器。资源管理器充当着事务提交树的叶子节点,它们的父结点为本机的DTC,分布于不同机器的DTC按照事务的传播路径形成了上下级关系。
  


  在一个分布式事务中,事务的初始化和提交是属于一个对象,只有最初开始的事务才能被提交,我们将这种能被初始化和提交的事务称作可提交事务。随着参与者逐个登记到事务中,它们本地的事务实际上依赖着这个最初开始的事务,所以我们称这种事务叫依赖事务。
四:示例

我们如果是仔细阅读这篇文章不难发现他提供了一个.exe类型文件的下载。先把这个TxF2007_07.exe文件下载到本地硬盘,执行它,可以得到一个关于 c#的 KtmIntegration.csproj 的项目,我们用visual studio来打开这个项目,并且重新重成这个项目,可以得到一个KtmIntegration.dll文件。在你要实现的文件事务的项目中引入这个.dll文件,那你就可以很顺利的实现文件事务的操作了。具体代码:

using System;
using System.IO;
using System.Transactions;
using Microsoft.KtmIntegration;
namespace Exercise.WebLocalTransaction
{
public partial class Test02 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
using (TransactionScope transactionScope = new TransactionScope())
{
try
{
FileStream stream = TransactedFile.Open(
@"D:/Sam Xiao.txt"
, FileMode.OpenOrCreate
, FileAccess.ReadWrite
, FileShare.ReadWrite);
StreamWriter writer = new StreamWriter(stream);
writer.WriteLine(String.Concat("执行一个事务代码:",DateTime.Now.ToString(),Environment.NewLine));
writer.Close();
//int x = 0;
//int y = 10;
//int z = y / x;
transactionScope.Complete();
}
catch
{
}
}
}
}
}

不要忘记了引入using Microsoft.KtmIntegration;名称空间。在段代码没有异常的情况下,我们可以看到D盘里顺利创建了一个关于Sam Xiao.txt的文件。我们故意在这段代码中抛出一个被0整除的异常,那么整个操作就会回滚。
  分布式事务
  在.net平台上,主要是通过WCF的手段来实现程序的分布式开发。在WCF事务体系:主要解决了事务在服务中的流转,以及解决服务内部直接或间接访问事务型资源的协作。
  用WCF来演示事务的时候,要选择好WCF的绑定类型,有一部份绑定是不支持WCF的事务传播的。我们选择wsHttpBinding 来做WCF的事务演示。
  1,首先定义好WCF的服务契约

[ServiceContract(Name = "IBankingService")]
public interface IBankingService
{

[TransactionFlow(TransactionFlowOption.Mandatory
8000
)]
[OperationContract(Name = "Transfer")]
void Transfer(string fromAccountId, string toAccountId, double amount);
[TransactionFlow(TransactionFlowOption.Mandatory)]
[OperationContract(Name = "Pay")]
bool Pay(String accountID, double amount);
[TransactionFlow(TransactionFlowOption.Mandatory)]
[OperationContract(Name = "Receipt")]
bool Receipt(String accountID, double amount);
}

  2,实现WCF的服务

[ServiceBehavior(TransactionIsolationLevel = System.Transactions.IsolationLevel.Serializable)]
public class BankingService : IBankingService
{
[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
public void Transfer(string fromAccountId, string toAccountId, double amount)
{
throw new NotImplementedException();
}
[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
public bool Pay(string accountID, double amount)
{
throw new NotImplementedException();
}
[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
public bool Receipt(string accountID, double amount)
{
throw new NotImplementedException();
}
}

  3,WCF宿主配置

<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="sBehaviorConfig">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<wsHttpBinding>
<binding name="wshttpConfig" transactionFlow="true" >
<security mode="None" />
</binding>
</wsHttpBinding>
</bindings>

<services>
<service name="Exercise.Service.BankingService" behaviorConfiguration="sBehaviorConfig">
<endpoint address="mex"  binding="wsHttpBinding" bindingConfiguration="wshttpConfig" contract="Exercise.Contract.IBankingService"></endpoint>
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>

  4,WCF客户端配置

<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IBankingService" transactionFlow="true">
<security mode="None" />
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:9100/BankingService.svc/mex"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IBankingService"
contract="Exercise.Contract.IBankingService" name="WSHttpBinding_IBankingService" />
</client>
</system.serviceModel>

  5,调用服务

IBankingService bankService = WcfProxy.CreateProxy<IBankingService>("WSHttpBinding_IBankingService");
protected void Page_Load(object sender, EventArgs e)
{
using (TransactionScope transactionScope = new TransactionScope())
{
bankService.Pay("111", 50);
bankService.Receipt("222", 50);
transactionScope.Complete();
}
}
框架简介:
本系统一款通用的SOA中间件平台,用来开发各类J2EE企业级应用,节省时间和人力成本。本系统采用MVC模式、AOP引擎、任务调度器、工作流、Ajax、拦截器、过滤器、缓存、日志监控、数据访问、表达式、国际化等技术。

用户权限系统:
组织结构:角色、用户、用户组、组织机构;权限点:页面、方法、按钮、数据权限、分级授权

项目管理新体验:
快速出原型系统、组件树、版本控制、模块移植、协同开发、实时监控、发布管理

可持续集成:
所有组件可移植、可定制、可扩充,开发成果不断积累,形成可持续发展的良性循环

框架/平台构成:
Springmvc + Mybatis + Shiro(权限)+SSO(单点登录) + Tiles(模板) +ActiveMQ(消息队列) + Rest(服务) + WebService(服务)+ EHcache(缓存) + Lucene(搜索引擎) + Quartz(定时调度)+ Html5(支持PC、IOS、Android)

支持平台平台:
Windows XP、Windows 7 、Windows 10 、 Linux 、 Unix

服务器容器:
Tomcat 5/6/7 、Jetty、JBoss、WebSphere 8.5
项目源码结构截图:




项目运行截图:

























































































内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: