外国优秀设计网站推荐,国外对旅游网站的建设,谷歌seo新手快速入门,制作大型网站如果在C#中使用TransactionScope类(分布式事务),则须注意如下事项: 1、在项目中引用using System.Transactions命名空间#xff08;先要在添加net组件的引用#xff09;;
2、具体示例如下#xff1a; public static void sendMessage(){ TransactionOptions tran…如果在C#中使用TransactionScope类(分布式事务),则须注意如下事项: 1、在项目中引用using System.Transactions命名空间先要在添加net组件的引用;
2、具体示例如下 public static void sendMessage(){ TransactionOptions transactionOption new TransactionOptions();//设置事务隔离级别transactionOption.IsolationLevel System.Transactions.IsolationLevel.ReadCommitted;// 设置事务超时时间为60秒transactionOption.Timeout new TimeSpan(0, 0, 60);using (TransactionScope scope new TransactionScope(TransactionScopeOption.Required, transactionOption)){try{//TODOscope.Complete();}catch (Exception ex) {throw new Exception(ex.Message);}finally{//释放资源scope.Dispose();} }}namespace System.Transactions
{//// Summary:// Specifies the isolation level of a transaction.public enum IsolationLevel{//// Summary:// Volatile data can be read but not modified, and no new data can be added during the transaction.可读取但不可修改易失性数据并且在此过程中不能添加新数据交易。Serializable 0,//// Summary:// Volatile data can be read but not modified during the transaction. New data can be added during the transaction.在事务期间可以读取但不能修改易失性数据。新数据可以在事务处理期间添加。RepeatableRead 1,//// Summary:// Volatile data cannot be read during the transaction, but can be modified.事务期间无法读取易失性数据但可以对其进行修改。ReadCommitted 2,//// Summary:// Volatile data can be read and modified during the transaction.在事务期间可以读取和修改易失性数据。ReadUncommitted 3,//// Summary:// Volatile data can be read. Before a transaction modifies data, it verifies if// another transaction has changed the data after it was initially read. If the// data has been updated, an error is raised. This allows a transaction to get to// the previously committed value of the data.可以读取易失性数据。在事务修改数据之前它会验证另一个事务在最初读取数据后更改了数据。如果数据已更新出现错误。这使得事务可以到达以前提交的数据值。Snapshot 4,//// Summary:// The pending changes from more highly isolated transactions cannot be overwritten.无法覆盖来自高度隔离事务的挂起更改。Chaos 5,//// Summary:// A different isolation level than the one specified is being used, but the level cannot be determined. An exception is thrown if this value is set.正在使用与指定隔离级别不同的隔离级别但无法确定。如果设置了此值则会引发异常。Unspecified 6}
} 事务五种隔离级别IsolationLevel属性一共支持五种事务设置具体介绍如下 1DEFAULT 使用数据库设置的隔离级别默认由DBA 默认的设置来决定隔离级别。 2READ_UNCOMMITTED 这是事务最低的隔离级别它充许别外一个事务可以看到这个事务未提交的数据。 会出现脏读、不可重复读、幻读 隔离级别最低并发性能高。 3READ_COMMITTED 保证一个事务修改的数据提交后才能被另外一个事务读取。另外一个事务不能读取该事务未提交的数据。 可以避免脏读但会出现不可重复读、幻读问题锁定正在读取的行。 4REPEATABLE_READ 可以防止脏读、不可重复读但会出幻读锁定所读取的所有行。 5SERIALIZABLE 这是花费最高代价但是最可靠的事务隔离级别事务被处理为顺序执行。 保证所有的情况不会发生锁表。 3、对MSDTC组件设置: 在控制面板---管理工具---服务 中开启Distributed Transaction Coordinator 服务。 a.控制面板-管理工具-组件服务-计算机-我的电脑-右键-属性 b.选择MSDTC页, 确认使用本地协调器 c.点击下方安全配置按钮 d.勾选: 允许网络DTC访问,允许远程客户端,允许入站,允许出站,不要求进行身份验证. e.对于数据库服务器端, 可选择要求对呼叫方验证 f.勾选:启用事务Internet协议TIP)事务。 g.在双方防火墙中增加MSDTC.exe例外 可用命令行: netsh firewall set allowedprogram %windir%\system32\msdtc.exe MSDTC enable
4、重启IIS服务器。 2 锁
锁有乐观锁和悲观锁
2.1乐观锁实现方法有两种
2.1.1加入时间戳
2.1.2 在UPDATE数据前对数据进行比对
Set moneySelect money from account where accountId1Update account set money 2000 where accountId1 and moneymoney
这就实现了一个最简单的乐观锁在update之前对拿到的数据进行判断或者加入时间搓机制
2.2 悲观锁
在事务一开始就对你后面要修改的记录锁定
Select * from account with (UPDLOCK) where accountId1
一但锁住accountId1 这一个记录 在此事务结束前别人都无法对其进行修改或读取要等待此事务结束。
使用悲观锁千万不要用以下语句
Select top 1 * from account with (UPDLOCK)
这样会锁住account整个表其他事务都无法对此表进行读取或者修改
在并发量不大的时候可以使用悲观锁一但我要修改某条记录我就锁住它直到我整个事务完成。
并发量比较大的还是要使用乐观锁但是要有失败处理机制。 参考 TransactionScop事务机制的使用 脏读、不可重复读 共享锁、悲观锁 和 事务五种隔离级别