C#分布式事务隔离级别问题?如何解释不同的隔离级别,结合案例。
2010-12-22 10:38
471 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Transactions;
namespace MyFriend
{
/*
* --隔离级别
级别一 read uncommitted System.Transactions.IsolationLevel.ReadUncommitted --未提交读
级别二 read committed System.Transactions.IsolationLevel.ReadCommitted --提交读
级别三 repeatable read System.Transactions.IsolationLevel.RepeatableRead --可重复读
级别四 serializable System.Transactions.IsolationLevel.Serializable --可串行读
*/
public partial class frmIsolationLevel : Form
{
public frmIsolationLevel()
{
InitializeComponent();
}
private static string GetConnectionString(int i)
{
if (i == 1)
return "Data Source=.;Initial Catalog=MyFriend;User ID=sa;Password=123456";
else
return "Data Source=.;Initial Catalog=student;User ID=sa;Password=123456";
}
private void button1_Click(object sender, EventArgs e)
{
string str1 = "insert into depart values('市场部3',19,'1982-02-03')";
string str2 = "insert into info values('拉灯3','078889897878','13989889899','阿富汗捣蛋部队','999','技术部')";
SqlConnection cn1 = new SqlConnection(GetConnectionString(1)); //连接库1
cn1.Open();
TransactionOptions op = new TransactionOptions();
//op.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted; //提交读
op.IsolationLevel = System.Transactions.IsolationLevel.RepeatableRead; //提交读
using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required, op)) //创建事务代码块
{
SqlCommand cmd1 = new SqlCommand(str1, cn1);
int rowsUpdated1 = cmd1.ExecuteNonQuery();
if (rowsUpdated1 > 0)
{
SqlConnection cn2 = new SqlConnection(GetConnectionString(1));//连接库2.
cn2.Open();
SqlCommand cmd2 = new SqlCommand(str2, cn2);
int rowsUpdated2 = cmd2.ExecuteNonQuery();
if (rowsUpdated2 > 0)
{
ts.Complete();
MessageBox.Show("事务提交完成!");
}
cn2.Close();
}
}
cn1.Close();
}
}
}
//*************db如下:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Transactions;
namespace MyFriend
{
/*
* --隔离级别
级别一 read uncommitted System.Transactions.IsolationLevel.ReadUncommitted --未提交读
级别二 read committed System.Transactions.IsolationLevel.ReadCommitted --提交读
级别三 repeatable read System.Transactions.IsolationLevel.RepeatableRead --可重复读
级别四 serializable System.Transactions.IsolationLevel.Serializable --可串行读
*/
public partial class frmIsolationLevel : Form
{
public frmIsolationLevel()
{
InitializeComponent();
}
private static string GetConnectionString(int i)
{
if (i == 1)
return "Data Source=.;Initial Catalog=MyFriend;User ID=sa;Password=123456";
else
return "Data Source=.;Initial Catalog=student;User ID=sa;Password=123456";
}
private void button1_Click(object sender, EventArgs e)
{
string str1 = "insert into depart values('市场部3',19,'1982-02-03')";
string str2 = "insert into info values('拉灯3','078889897878','13989889899','阿富汗捣蛋部队','999','技术部')";
SqlConnection cn1 = new SqlConnection(GetConnectionString(1)); //连接库1
cn1.Open();
TransactionOptions op = new TransactionOptions();
//op.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted; //提交读
op.IsolationLevel = System.Transactions.IsolationLevel.RepeatableRead; //提交读
using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required, op)) //创建事务代码块
{
SqlCommand cmd1 = new SqlCommand(str1, cn1);
int rowsUpdated1 = cmd1.ExecuteNonQuery();
if (rowsUpdated1 > 0)
{
SqlConnection cn2 = new SqlConnection(GetConnectionString(1));//连接库2.
cn2.Open();
SqlCommand cmd2 = new SqlCommand(str2, cn2);
int rowsUpdated2 = cmd2.ExecuteNonQuery();
if (rowsUpdated2 > 0)
{
ts.Complete();
MessageBox.Show("事务提交完成!");
}
cn2.Close();
}
}
cn1.Close();
}
}
}
//*************db如下:
相关文章推荐
- C#中分布式事务的超时处理问题
- spring事务的隔离级别。如何避免脏读或者幻读
- 数据库事务隔离级别-- 脏读、幻读、不可重复读(清晰解释)
- 数据库事务隔离级别-- 脏读、幻读、不可重复读(清晰解释)
- 数据库事务的四大特性、隔离级别以及Spring中如何利用AOP进行事务管理
- 关于数据库事务隔离级别的有关问题
- 数据库并发的五个问题以及四级封锁协议与事务隔离的四个级别
- C#中分布式事务的超时处理问题
- 事务的隔离级别与锁的关系?也即如何用锁实现事务的隔离级别?
- 数据库事务隔离级别-- 脏读、幻读、不可重复读(清晰解释)
- 数据库事务隔离级别-- 脏读、幻读、不可重复读(清晰解释)
- 数据库事务隔离级别-- 脏读、幻读、不可重复读(清晰解释)
- 数据库事务隔离级别-- 脏读、幻读、不可重复读(清晰解释)
- 事务有哪些特性?spring的事务管理有几种方式实现,如何实现?spring 中常用的两种事务配置方式以及事务的传播性、隔离级别
- C#.NET常见问题(FAQ)-如何在不同窗体之间传递值
- 事务特性,事务的隔离级别,并发事务可能出现的问题,spring事务 数据库锁
- MySQL(23):事务的隔离级别出现问题之 脏读
- MySQL(26):事务的隔离级别出现问题之 幻读
- 数据库事务隔离级别-- 脏读、幻读、不可重复读(清晰解释)
- C#中分布式事务的超时处理问题