WCF学习笔记(八)服务模式下的简单事务实例和非事务实例对比
2011-09-30 14:08
495 查看
1.契约
[ServiceContract]
public interface IWCFServicesTransaction
{
/*服务模式下,服务必须启动一个根事务,且不参与外部事务 */
[OperationContract]
[TransactionFlow(TransactionFlowOption.NotAllowed)]
bool AddData(string name);
[OperationContract]
bool AddDataEx(string name);
}
2.接口实现
注意:在TransactionScope 块下的,即是在事务模式下的添加数据库操作,需要没有异常才能把数据更新到数据库,在非事务模式直接更新进数据库
public class WCFServicesTransaction : IWCFServicesTransaction
{
#region IWCFServicesTransaction 成员
/// <summary>
/// 这就是一个根事务
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
[OperationBehavior(TransactionScopeRequired=true)]
public bool AddData(string productName)
{
//获取事务环境
Transaction transaction = Transaction.Current;
//DistributedIdentifier:获取升级的事务的唯一标识符。
//若是等于00000-0000就是个本地事务
Debug.Assert(transaction.TransactionInformation.DistributedIdentifier == Guid.Empty);
//LocalIdentifier:本地事务标示
//若是断言不等后面就不执行了,进程就阻塞在这个地方
//Debug.Assert(transaction.TransactionInformation.LocalIdentifier == Guid.Empty.ToString());
Console.WriteLine("本事务创建时间:{0}",transaction.TransactionInformation.CreationTime);
Console.WriteLine("本事务状态:{0}", transaction.TransactionInformation.Status);
Console.WriteLine("本事务本地事务标示:{0}", transaction.TransactionInformation.LocalIdentifier);
//DistributedIdentifier:全局事务
Console.WriteLine("本事务升级的事务的标示:{0}", transaction.TransactionInformation.DistributedIdentifier);
//创建事务执行范围
using(TransactionScope ts=new TransactionScope())
{
try
{
DBHelper db = new DBHelper();
Products p = new Products();
p.ProductID = 1235;
p.ProductName = productName;
Console.WriteLine("调用事务,传入的数据是:{0},长度:{1}", productName, productName.Length);
db.AddProducts(p);
if (productName.Length > 5)
{
Exception ex = new Exception();
ExceptionDetail exdetail = new ExceptionDetail(ex);
throw new FaultException<ExceptionDetail>(exdetail, "名字太长");
}
}
catch (Exception e)
{
transaction.Rollback();
Console.WriteLine("调用服务出错:{0}", e.Message);
return false;
}
ts.Complete();
return true;
}
}
#endregion
#region IWCFServicesTransaction 成员
public bool AddDataEx(string productName)
{
try
{
DBHelper db = new DBHelper();
Products p = new Products();
p.ProductID = 1234;
p.ProductName = productName;
Console.WriteLine("调用事务,传入的数据是:{0},长度:{1}", productName, productName.Length);
db.AddProducts(p);
if (productName.Length > 5)
{
Exception ex = new Exception();
ExceptionDetail exdetail = new ExceptionDetail(ex);
throw new FaultException<ExceptionDetail>(exdetail, "名字太长");
}
}
catch (Exception e)
{
Console.WriteLine("调用服务出错:{0}", e.Message);
return false;
}
return true;
}
#endregion
}
3.客户端调用(调用方式见WCF学习笔记(二))
static void Main(string[] args)
{
WCFServicesTransactionClient client = new WCFServicesTransactionClient();
//try
//{
// client.AddData("yoyoyo");
// Console.WriteLine("数据添加完成");
//}
//catch(Exception ex)
//{
// Console.WriteLine("服务调用异常:{0}",ex.Message);
//}
try
{
client.AddDataEx("yoyoyo");
Console.WriteLine("数据添加完成");
}
catch (Exception ex)
{
Console.WriteLine("服务调用异常:{0}", ex.Message);
}
Console.ReadKey();
}
[ServiceContract]
public interface IWCFServicesTransaction
{
/*服务模式下,服务必须启动一个根事务,且不参与外部事务 */
[OperationContract]
[TransactionFlow(TransactionFlowOption.NotAllowed)]
bool AddData(string name);
[OperationContract]
bool AddDataEx(string name);
}
2.接口实现
注意:在TransactionScope 块下的,即是在事务模式下的添加数据库操作,需要没有异常才能把数据更新到数据库,在非事务模式直接更新进数据库
public class WCFServicesTransaction : IWCFServicesTransaction
{
#region IWCFServicesTransaction 成员
/// <summary>
/// 这就是一个根事务
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
[OperationBehavior(TransactionScopeRequired=true)]
public bool AddData(string productName)
{
//获取事务环境
Transaction transaction = Transaction.Current;
//DistributedIdentifier:获取升级的事务的唯一标识符。
//若是等于00000-0000就是个本地事务
Debug.Assert(transaction.TransactionInformation.DistributedIdentifier == Guid.Empty);
//LocalIdentifier:本地事务标示
//若是断言不等后面就不执行了,进程就阻塞在这个地方
//Debug.Assert(transaction.TransactionInformation.LocalIdentifier == Guid.Empty.ToString());
Console.WriteLine("本事务创建时间:{0}",transaction.TransactionInformation.CreationTime);
Console.WriteLine("本事务状态:{0}", transaction.TransactionInformation.Status);
Console.WriteLine("本事务本地事务标示:{0}", transaction.TransactionInformation.LocalIdentifier);
//DistributedIdentifier:全局事务
Console.WriteLine("本事务升级的事务的标示:{0}", transaction.TransactionInformation.DistributedIdentifier);
//创建事务执行范围
using(TransactionScope ts=new TransactionScope())
{
try
{
DBHelper db = new DBHelper();
Products p = new Products();
p.ProductID = 1235;
p.ProductName = productName;
Console.WriteLine("调用事务,传入的数据是:{0},长度:{1}", productName, productName.Length);
db.AddProducts(p);
if (productName.Length > 5)
{
Exception ex = new Exception();
ExceptionDetail exdetail = new ExceptionDetail(ex);
throw new FaultException<ExceptionDetail>(exdetail, "名字太长");
}
}
catch (Exception e)
{
transaction.Rollback();
Console.WriteLine("调用服务出错:{0}", e.Message);
return false;
}
ts.Complete();
return true;
}
}
#endregion
#region IWCFServicesTransaction 成员
public bool AddDataEx(string productName)
{
try
{
DBHelper db = new DBHelper();
Products p = new Products();
p.ProductID = 1234;
p.ProductName = productName;
Console.WriteLine("调用事务,传入的数据是:{0},长度:{1}", productName, productName.Length);
db.AddProducts(p);
if (productName.Length > 5)
{
Exception ex = new Exception();
ExceptionDetail exdetail = new ExceptionDetail(ex);
throw new FaultException<ExceptionDetail>(exdetail, "名字太长");
}
}
catch (Exception e)
{
Console.WriteLine("调用服务出错:{0}", e.Message);
return false;
}
return true;
}
#endregion
}
3.客户端调用(调用方式见WCF学习笔记(二))
static void Main(string[] args)
{
WCFServicesTransactionClient client = new WCFServicesTransactionClient();
//try
//{
// client.AddData("yoyoyo");
// Console.WriteLine("数据添加完成");
//}
//catch(Exception ex)
//{
// Console.WriteLine("服务调用异常:{0}",ex.Message);
//}
try
{
client.AddDataEx("yoyoyo");
Console.WriteLine("数据添加完成");
}
catch (Exception ex)
{
Console.WriteLine("服务调用异常:{0}", ex.Message);
}
Console.ReadKey();
}
相关文章推荐
- 抽象工厂模式:简单工厂模式、工厂方法模式对比;在工厂方法模式中使用反射创建对象实例
- 分布式系统的消息&服务模式简单总结
- MyBatis代码实例系列-03:MyBatis单张表简单实现增删改查 + log4j + 手动事务控制
- 单例模式简单实例
- JS实现简单的图书馆享元模式实例
- 简单工厂方法模式-计算器实例
- JAVA项目中发布WebService服务—简单实例
- JAVA项目中发布WebService服务——简单实例
- ServiceStack 项目实例 002 REST服务与客户端模式
- Android 单例模式 Singleton 简单实例设计模式解析
- Android UDP通信的简单实例和用法归纳,以及与TCP对比、常见问题
- 转载:Python 与 C++ 程序的简单实例对比
- Java技术_每天掌握一种设计模式(004)_使用场景及简单实例(创建型:抽象工厂)
- MVP 框架模式(学习笔记简单实例)
- 单例模式和工厂模式 简单实例
- JavaScript中发布/订阅模式的简单实例
- Python与C++ 程序的简单实例对比
- 学习笔记:7种结构型设计模式简单对比
- 简单工程模式实例
- JAVA项目中发布WebService服务—简单实例