1、SQL事务
优点:执行效率最佳
限制:事务上下文仅在
数据库
中调用,难以实现复杂的业务逻辑。
-
CREATE
PROCEDURE
Tran1
-
as
-
begin
tran
-
set
xact_abort
on
-
Insert
Into
trantest (id,test)
values
(1,
‘test’
)
-
Insert
Into
trantest (id,test)
values
(2,
‘test’
)
-
commit
tran
-
GO
-
–set xact_abort on 表示遇到错误立即回滚
-
–当然你也可以这么写
-
CREATE
PROCEDURE
tran1
-
as
-
begin
tran
-
insert
into
trantest(id,test)
values
(1,
‘test’
)
-
if(@@error<>0)
-
rollback
tran
-
else
-
begin
-
insert
into
trantest(id,test)
values
(2,
‘test’
)
-
if(@@error<>0)
-
rollback
tran
-
else
-
commit
tran
-
end
-
GO
-
CREATE
PROCEDURE
Tran1
-
as
-
begin
tran
-
set
xact_abort
on
-
Insert
Into
trantest (id,test)
values
(1,
‘test’
)
-
Insert
Into
trantest (id,test)
values
(2,
‘test’
)
-
commit
tran
-
GO
-
–set xact_abort on 表示遇到错误立即回滚
-
–当然你也可以这么写
-
CREATE
PROCEDURE
tran1
-
as
-
begin
tran
-
insert
into
trantest(id,test)
values
(1,
‘test’
)
-
if(@@error<>0)
-
rollback
tran
-
else
-
begin
-
insert
into
trantest(id,test)
values
(2,
‘test’
)
-
if(@@error<>0)
-
rollback
tran
-
else
-
commit
tran
-
end
-
GO
2、ADO.NET 事务
在ADO.NET 中,可以使用Connection 和Transaction 对象来控制事务。若要执行事务,请执行下列操作:
调用Connection 对象的BeginTransaction 方法来标记事务的开始。
将Transaction 对象分配给要执行的Command的Transaction 属性。
执行所需的命令。
调用Transaction 对象的Commit 方法来完成事务,或调用Rollback 方法来取消事务。
优点:简单,效率和数据库事务差不多快。
缺点:事务执行在数据库连接层上,所以你需要在事务过程中手动的维护一个连接。
-
SqlConnection conn =
new
SqlConnection(ConfigurationManager.ConnectionStrings[
“Test”
].ConnectionString);
-
conn.Open();
-
SqlTransaction tx = conn.BeginTransaction(IsolationLevel.ReadCommitted);
-
SqlCommand cmd =
new
SqlCommand();
-
cmd.Connection = conn;
-
cmd.Transaction = tx;
-
-
try
-
{
-
cmd.CommandText =
“INSERT INTO [Test]([Name],[Value]) VALUES (‘测试1′,’1’)”
;
-
cmd.ExecuteNonQuery();
-
cmd.CommandText =
“INSERT INTO [Test]([Name],[Value]) VALUES (‘测试2′,’2’)”
;
-
cmd.ExecuteNonQuery();
-
-
tx.Commit();
-
}
-
catch
(Exception ex)
-
{
-
tx.Rollback();
-
throw
new
Exception(ex.Message, ex);
-
}
-
finally
-
{
-
conn.Close();
-
}
-
SqlConnection conn =
new
SqlConnection(ConfigurationManager.ConnectionStrings[
“Test”
].ConnectionString);
-
conn.Open();
-
SqlTransaction tx = conn.BeginTransaction(IsolationLevel.ReadCommitted);
-
SqlCommand cmd =
new
SqlCommand();
-
cmd.Connection = conn;
-
cmd.Transaction = tx;
-
-
try
-
{
-
cmd.CommandText =
“INSERT INTO [Test]([Name],[Value]) VALUES (‘测试1′,’1’)”
;
-
cmd.ExecuteNonQuery();
-
cmd.CommandText =
“INSERT INTO [Test]([Name],[Value]) VALUES (‘测试2′,’2’)”
;
-
cmd.ExecuteNonQuery();
-
-
tx.Commit();
-
}
-
catch
(Exception ex)
-
{
-
tx.Rollback();
-
throw
new
Exception(ex.Message, ex);
-
}
-
finally
-
{
-
conn.Close();
-
}
3、TransactionScope事务
在.NET 2.0中新添加了一个名为System.Transactions的命名空间,其提供了一个“轻量级”的、易于使用的事务框架,通过这个框架可以大大简化事务的操作。
这个框架提供了如下优点:
(1)在简单(不涉及分布式)事务中也可以使用声明式的事务处理方法,而不必使用Com+容器和目录注册。
(2)用户根本不需要考虑是简单事务还是分布式事务。它实现一种所谓自动提升事务机制(Promotable Transaction),会自动根据事务中涉及的对象资源判断使用何种事务管理器。
TransactionScope事务类,它可以使代码块成为事务性代码。并自动提升为分布式事务
优点:实现简单,同时能够自动提升为分布式事务
-
TransactionOptions option =
new
TransactionOptions();
-
option.IsolationLevel = IsolationLevel.ReadCommitted;
-
-
using
(TransactionScope ts =
new
TransactionScope(TransactionScopeOption.Required,option))
-
{
-
using
(SqlConnection conn =
new
SqlConnection(ConfigurationManager.ConnectionStrings[
“Test”
].ConnectionString))
-
{
-
conn.Open();
-
SqlCommand cmd =
new
SqlCommand(conn);
-
cmd.CommandText =
“INSERT INTO [Test]([Name],[Value]) VALUES (‘测试1′,’1’)”
;
-
cmd.ExecuteNonQuery();
-
cmd.CommandText =
“INSERT INTO [Test]([Name],[Value]) VALUES (‘测试2′,’2’)”
;
-
cmd.ExecuteNonQuery();
-
}
-
ts.Complete();
-
}
-
TransactionOptions option =
new
TransactionOptions();
-
option.IsolationLevel = IsolationLevel.ReadCommitted;
-
-
using
(TransactionScope ts =
new
TransactionScope(TransactionScopeOption.Required,option))
-
{
-
using
(SqlConnection conn =
new
SqlConnection(ConfigurationManager.ConnectionStrings[
“Test”
].ConnectionString))
-
{
-
conn.Open();
-
SqlCommand cmd =
new
SqlCommand(conn);
-
cmd.CommandText =
“INSERT INTO [Test]([Name],[Value]) VALUES (‘测试1′,’1’)”
;
-
cmd.ExecuteNonQuery();
-
cmd.CommandText =
“INSERT INTO [Test]([Name],[Value]) VALUES (‘测试2′,’2’)”
;
-
cmd.ExecuteNonQuery();
-
}
-
ts.Complete();
-
}
4、
EnterpriseServices实现事务
就是利用com+实现自动处理事务。
添加引用System.EnterpriseServices.dll
using System.EnterpriseServices;
使用方法参考:
http://support.microsoft.com/default.aspx?scid=kb;zh-cn;816141
随便建立一个按钮,在按钮中进行如下操作:
-
try
-
{
-
work1();
-
work2();
-
ContextUtil.SetComplete();
-
}
-
catch
(System.Exception except)
-
{
-
ContextUtil.SetAbort();
-
Response.Write(except.Message);
-
}
-
try
-
{
-
work1();
-
work2();
-
ContextUtil.SetComplete();
-
}
-
catch
(System.Exception except)
-
{
-
ContextUtil.SetAbort();
-
Response.Write(except.Message);
-
}
然后在页面中添加2个操作,模拟一下在逻辑层调用不同类中的操作的情况 :
-
private void work1()
-
{
-
SqlConnection
conn
=
new
SqlConnection(System.Configuration.ConfigurationSettings.AppSettings[“conn”]);
-
SqlCommand
cmd1
=
new
SqlCommand(“Insert Into trantest (id,test)values(1,’test’)”,conn);
-
conn.Open();
-
cmd1.ExecuteNonQuery();
-
conn.Close();
-
}
-
-
private void work2()
-
{
-
SqlConnection
conn
=
new
SqlConnection(System.Configuration.ConfigurationSettings.AppSettings[“conn”]);
-
SqlCommand
cmd2
=
new
SqlCommand(“Insert Into trantest (id,test)values(2,’test’)”,conn);
-
conn.Open();
-
cmd2.ExecuteNonQuery();
-
conn.Close();
-
}
-
-
修改前台页面在
<
%Page后面添加
Transaction
=
“Required”
即可