热门标签 | HotTags
当前位置:  开发笔记 > 数据库 > 正文

TransactionScope和EnterpriseLibray3.0DataAccessApplicat

EnterpriseLibray3.0已经发布了,具体可参见TerryLee的EnterpriseLibrary3.0发布.下载了看看,有非常激动人心的更新.我只是看看DataAccessApplicationBlock代码,代码中有这个类TransactionScopeConnections,是个内部类,设计意图很明显就是使用数据库

Enterprise Libray 3.0已经发布了,具体可参见TerryLee的 Enterprise Library 3.0 发布.下载了看看,有非常激动人心的更新.我只是看看Data Access Application Block代码,代码中有这个类TransactionScopeConnections,是个内部类,设计意图很明显就是使用数据库

Enterprise Libray 3.0已经发布了,具体可参见TerryLee的 Enterprise Library 3.0 发布.下载了看看,有非常激动人心的更新.我只是看看Data Access Application Block代码,代码中有这个类TransactionScopeConnections,是个内部类,设计意图很明显就是使用数据库的事务模型.我觉得设计为内部类有点瑕疵,我的习惯是事务和提交在业务逻辑层. .NET 2.0的System.Transactions应该是一个更好的选择。就将Data Access Application Block的QuickStart例子代码:

///


/// Transfers an amount between two accounts.
///

/// Amount to transfer.
/// Account to be credited.
/// Account to be debited.
/// true if sucessful; otherwise false.
/// Demonstrates executing multiple updates within the
/// context of a transaction.

public bool Transfer(int transactionAmount, int sourceAccount, int destinationAccount)
{
bool result = false;

// Create the Database object, using the default database service. The
// default database service is determined through configuration.
Database db = DatabaseFactory.CreateDatabase();

// Two operations, one to credit an account, and one to debit another
// account.
string sqlCommand = "CreditAccount"
DbCommand creditCommand = db.GetStoredProcCommand(sqlCommand);

db.AddInParameter(creditCommand, "AccountID", DbType.Int32, sourceAccount);
db.AddInParameter(creditCommand, "Amount", DbType.Int32, transactionAmount);

sqlCommand = "DebitAccount"
DbCommand debitCommand = db.GetStoredProcCommand(sqlCommand);

db.AddInParameter(debitCommand, "AccountID", DbType.Int32, destinationAccount);
db.AddInParameter(debitCommand, "Amount", DbType.Int32, transactionAmount);

using (DbConnection cOnnection= db.CreateConnection())
{
connection.Open();
DbTransaction transaction = connection.BeginTransaction();

try
{
// Credit the first account
db.ExecuteNonQuery(creditCommand, transaction);
// Debit the second account
db.ExecuteNonQuery(debitCommand, transaction);

// Commit the transaction
transaction.Commit();

result = true;
}
catch
{
// Rollback transaction
transaction.Rollback();
}
connection.Close();

return result;
}
}

按照TransactionScope类进行改造,试验成功了,代码如下:

public bool Transfer(int transactionAmount, int sourceAccount, int destinationAccount)
{
bool result = false;
Database database = DatabaseFactory.CreateDatabase();

using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
{
TestCommand1(database, transactionAmount, sourceAccount);
TestCommand2(database, transactionAmount, destinationAccount);
scope.Complete();
result = true;
}
return result;

}

private void TestCommand1(Database db, int transactionAmount, int sourceAccount)
{
string sqlCommand = "CreditAccount"
DbCommand creditCommand = db.GetStoredProcCommand(sqlCommand);

db.AddInParameter(creditCommand, "AccountID", DbType.Int32, sourceAccount);
db.AddInParameter(creditCommand, "Amount", DbType.Int32, transactionAmount);

// Credit the first account
db.ExecuteNonQuery(creditCommand);
}

private void TestCommand2(Database db, int transactionAmount, int destinationAccount)
{
string sqlCommand = "DebitAccount"
DbCommand debitCommand = db.GetStoredProcCommand(sqlCommand);

db.AddInParameter(debitCommand, "AccountID", DbType.Int32, destinationAccount);
db.AddInParameter(debitCommand, "Amount", DbType.Int32, transactionAmount);

// Debit the second account
db.ExecuteNonQuery(debitCommand);
}

DAAB 在一个事务中可以在一个数据库连接中检测到几个命令的执行,这样可以避免虽然一个数据库连接执行的几个命令而启用 分布式事务 。在企业类库2.0的DAAB常常启用了分布式事务,就凭这一点,使用企业类库2.0的同学们有必要升级到企业类库3.0。

Parameter Discovery on Ms Access and SqlServer. using Microsoft Patterns and Practices DataBlock version 3.0 final
http://www.codeproject.com/useritems/Parameter_DiscoveryV292.asp

推荐阅读
  • Oracle优化新常态的五大禁止及其性能隐患
    本文介绍了Oracle优化新常态中的五大禁止措施,包括禁止外键、禁止视图、禁止触发器、禁止存储过程和禁止JOB,并分析了这些禁止措施可能带来的性能隐患。文章还讨论了这些禁止措施在C/S架构和B/S架构中的不同应用情况,并提出了解决方案。 ... [详细]
  • 本文介绍了OpenStack的逻辑概念以及其构成简介,包括了软件开源项目、基础设施资源管理平台、三大核心组件等内容。同时还介绍了Horizon(UI模块)等相关信息。 ... [详细]
  • 加密世界下一个主流叙事领域:L2、跨链桥、GameFi等
    本文介绍了加密世界下一个主流叙事的七个潜力领域,包括L2、跨链桥、GameFi等。L2作为以太坊的二层解决方案,在过去一年取得了巨大成功,跨链桥和互操作性是多链Web3中最重要的因素。去中心化的数据存储领域也具有巨大潜力,未来云存储市场有望达到1500亿美元。DAO和社交代币将成为购买和控制现实世界资产的重要方式,而GameFi作为数字资产在高收入游戏中的应用有望推动数字资产走向主流。衍生品市场也在不断发展壮大。 ... [详细]
  • Elasticsearch1Elasticsearch入门1.1Elasticsearch术语1.1.16.0以前的Elasticsearch术语1.1.26.0以后的Elasti ... [详细]
  • 在当前金融科技 ... [详细]
  • 一句话解决高并发的核心原则
    本文介绍了解决高并发的核心原则,即将用户访问请求尽量往前推,避免访问CDN、静态服务器、动态服务器、数据库和存储,从而实现高性能、高并发、高可扩展的网站架构。同时提到了Google的成功案例,以及适用于千万级别PV站和亿级PV网站的架构层次。 ... [详细]
  • Python开源库和第三方包的常用框架及库
    本文介绍了Python开源库和第三方包中常用的框架和库,包括Django、CubicWeb等。同时还整理了GitHub中最受欢迎的15个Python开源框架,涵盖了事件I/O、OLAP、Web开发、高性能网络通信、测试和爬虫等领域。 ... [详细]
  • SpringBoot整合SpringSecurity+JWT实现单点登录
    SpringBoot整合SpringSecurity+JWT实现单点登录,Go语言社区,Golang程序员人脉社 ... [详细]
  • Java和JavaScript是什么关系?java跟javaScript都是编程语言,只是java跟javaScript没有什么太大关系,一个是脚本语言(前端语言),一个是面向对象 ... [详细]
  • Centos下安装memcached+memcached教程
    本文介绍了在Centos下安装memcached和使用memcached的教程,详细解释了memcached的工作原理,包括缓存数据和对象、减少数据库读取次数、提高网站速度等。同时,还对memcached的快速和高效率进行了解释,与传统的文件型数据库相比,memcached作为一个内存型数据库,具有更高的读取速度。 ... [详细]
  • 解决Sharepoint 2013运行状况分析出现的“一个或多个服务器未响应”问题的方法
    本文介绍了解决Sharepoint 2013运行状况分析中出现的“一个或多个服务器未响应”问题的方法。对于有高要求的客户来说,系统检测问题的存在是不可接受的。文章详细描述了解决该问题的步骤,包括删除服务器、处理分布式缓存留下的记录以及使用代码等方法。同时还提供了相关关键词和错误提示信息,以帮助读者更好地理解和解决该问题。 ... [详细]
  • Sleuth+zipkin链路追踪SpringCloud微服务的解决方案
    在庞大的微服务群中,随着业务扩展,微服务个数增多,系统调用链路复杂化。Sleuth+zipkin是解决SpringCloud微服务定位和追踪的方案。通过TraceId将不同服务调用的日志串联起来,实现请求链路跟踪。通过Feign调用和Request传递TraceId,将整个调用链路的服务日志归组合并,提供定位和追踪的功能。 ... [详细]
  • 2021最新总结网易/腾讯/CVTE/字节面经分享(附答案解析)
    本文分享作者在2021年面试网易、腾讯、CVTE和字节等大型互联网企业的经历和问题,包括稳定性设计、数据库优化、分布式锁的设计等内容。同时提供了大厂最新面试真题笔记,并附带答案解析。 ... [详细]
  • ElasticSerach初探第一篇认识ES+环境搭建+简单MySQL数据同步+SpringBoot整合ES
    一、认识ElasticSearch是一个基于Lucene的开源搜索引擎,通过简单的RESTfulAPI来隐藏Lucene的复杂性。全文搜索,分析系统&# ... [详细]
  • 分布式系统一致性专题:3PC协议的优化和问题
    本文介绍了分布式系统一致性专题中的3PC协议,该协议是对2PC协议的优化和改进。文章详细解释了3PC协议的三个阶段:CanCommit、PreCommit和DoCommit,并分析了每个阶段可能出现的情况和处理方式。同时,文章也指出了3PC协议存在的问题,如参与者超时机制可能导致数据不一致等。总体来说,3PC协议在优化和改进方面取得了一定效果,但仍需继续努力解决数据不一致问题。 ... [详细]
author-avatar
rsidugjig
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有