欢迎光临
我们一直在努力

用Microsoft.net实现数据库事务(二)-.NET教程,数据库应用

建站超值云服务器,限时71元/月

数据库事务

数据库事务是其他事务模型的基础,本文讨论的事务都是基于数据库事务实现的,他们仅是隐藏了处理事务的复杂的或者容器专有的代码。当一个事务创建时不同数据库系统都有自己的规则。缺省地,sql server工作在自动提交的模式下,每个语句执行完后会立即提交,与此对照的是oracle需要你包含一个提交语句。但是当一个语句通过oledb执行时,它执行完后一个提交动作会被附加上去。为了使下面的例子适用于oracle,你需要去掉begin transation和commit transation两个语句,因为这两个语句会当作一个单独的事务来运行。

优势

l 所有的事务逻辑包含在一个单独的调用中

l 拥有运行一个事务的最佳性能

l 独立于应用程序

限制

l 事务上下文仅存在于数据库调用中

l 数据库代码与数据库系统有关

例子:

create procedure up_purchaseitem (

@customerid as int,

@itemid int,

@itemqty int)

as

declare @orderid int

begin transaction

— update the inventory based on purchase

update inventory

set qtyinstock = qtyinstock – @itemqty

where inventory.productid = @itemid;

if @@error != 0 goto error_handler

— insert the order into the database

insert into orders

values (@customerid, @itemid, @itemqty, getdate());

if @@error != 0 goto error_handler

set @orderid = @@identity

commit transaction

return @orderid

error_handler:

rollback transaction

set nocount off

return 0

go

ado.net事务

创建一个ado.net事务是很简单的,仅仅是标准代码的一个小的扩展。只要你知道如何使用ado.net来访问数据库,那就差不多知道了。区别仅仅是你需要把代码放到一个事务上下文中。

还是原来的ado.net类库引用,在实现事务的类里面引入system.data和system.data.sqlclient类库,为了执行一个事务,你需要创建一个sqltransation对象,可以调用你的sqlconnection对象begintransation()方法来创建它,一旦你把sqltransation对象存为本地变量,你就可以把它赋给你的sqlcommand对象的事务属性,或者把它作为构造器的一个参数来创建sqlcommand。在执行sqlcommand动作之前,你必须调用begintransaction()方法,然后赋给sqlcommand事务属性。

一单事务开始了,你就可以执行任何次数的sqlcommand动作,只要它是属于同一个事务和连接。最后你可以调用sqltransation的commit()方法来提交事务。

ado.net事务实际上是把事务上下文传递到数据库层,如果事务中发生一个错误,数据库会自动回滚。在你的错误处理代码中,每次调用rollback()方法之前检查事务对象是否存在是一种良好的习惯。这样的一个例子是当一个死锁发生的同时,数据库正在执行自动回滚。

优势:

l 简单性

l 和数据库事务差不多的快

l 事务可以跨越多个数据库访问

l 独立于数据库,不同数据库的专有代码被隐藏了

限制:

事务执行在数据库连接层上,所以你需要在事务过程中手动的维护一个连接

例子:

public int purchaseitem(int customerid, int itemid, int itemqty)

{

sqlconnection con = null;

sqltransaction tx = null;

int orderid = 0;

try

{

con = new sqlconnection("data source=localhost; user

id=sa;password=;initial catalog=trans_db;");

con.open();

tx = con.begintransaction(isolationlevel.serializable);

string updatesqltext = "update inventory set qtyinstock

= qtyinstock – " + itemqty.tostring()

+ " where inventory.productid = " + itemid.tostring();

sqlcommand cmd = new sqlcommand(updatesqltext, con, tx);

cmd.executenonquery();

// string is 2 sql statements: the first is the insert,

the second selects the identity column

string insertsqltext = "insert into orders values

(" + customerid.tostring() + "," + itemid.tostring()

+ "," + itemqty.tostring() + " , getdate() ); select @@identity";

cmd.commandtext = insertsqltext;

// retrieve the order id from the identity column

orderid = convert.toint32(cmd.executescalar());

cmd.dispose();

tx.commit();

}

catch (sqlexception sqlex)

{

// specific catch for deadlock

if (sqlex.number != 1205)

{

tx.rollback();

}

orderid = 0;

throw(sqlex);

}

catch (exception ex)

{

tx.rollback();

orderid = 0;

throw (ex);

}

finally

{

tx.dispose();

con.close();

}

}

asp.net事务

asp.net事务可以说是在.net平台上事务实现方式中最简单的一种,你仅仅需要加一行代码。在aspx的页面声明中加一个额外的属性,即是事务属性,它可以有 如下的值:disabled (缺省), notsupported, supported, required 和 requiresnew,这些设置和com+以及企业级服务中的设置一样,典型地如果你想在页面上下文中运行事务,那么要设置为required。如果页面中包含有用户控件,那么这些控件也会包含到事务中,事务会存在于页面的每个地方。

优势:

l 实现简单,不需要额外的编码

限制:

l 页面的所有代码都是同一个事务,这样的事务可能会很大,而也许我们需要的是分开的、小的事务

l 事务实在web层

例子:

aspx page

<%@ page transaction="required" language="c#" codebehind="aspnet_transaction.aspx.cs"

inherits="transtest.aspnet_transaction" autoeventwireup="false"%>

<html>

<head>

<title>asp.net transaction</title>

</head>

<body ms_positioning="gridlayout">

<form id="aspnet_transaction" method="post" runat="server">

</form>

</body>

</html>

aspx code behind page

using system;

using system.collections;

using system.componentmodel;

using system.data;

using system.data.sqlclient;

using system.drawing;

using system.web;

using system.web.sessionstate;

using system.web.ui;

using system.web.ui.webcontrols;

using system.web.ui.htmlcontrols;

namespace transtest

{

/// summary description for aspnet_transaction.

public class aspnet_transaction : system.web.ui.page

{

public int purchaseitem(int customerid, int itemid, int itemqty)

{

sqlconnection con = null;

int orderid = 0;

try

{

con = new sqlconnection("data source=localhost; user

id=sa;password=;initial catalog=trans_db;");

con.open();

string updatesqltext = "update inventory set qtyinstock = qtyinstock

– " + itemqty.tostring() + " where inventory.productid = " +

itemid.tostring();

sqlcommand cmd = new sqlcommand(updatesqltext, con);

cmd.executenonquery();

string insertsqltext = "insert into orders values

(" + customerid.tostring() + "," + itemid.tostring() + ","

+ itemqty.tostring() + " , getdate() ); select @@identity";

cmd.commandtext = insertsqltext;

orderid = convert.toint32(cmd.executescalar());

cmd.dispose();

}

catch (exception ex)

{

orderid = 0;

throw (ex);

}

finally

{

con.close();

}

return orderid;

}

private void page_load(object sender, system.eventargs e)

{

int orderid = purchaseitem(1, 1, 10);

response.write(orderid);

}

#region web form designer generated code

}

}

注意到这些和ado.net事务的代码基本一样,我们只是移除了对sqltransation对象的引用。这些事务被aspx页面的声明transaction="required"所代替。在事务统计中,当页面执行后,事务数目会增加,一个语句失败后,所有的语句将会回滚,你会看到事务被取消了。

图7:asp.net事务的统计

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 用Microsoft.net实现数据库事务(二)-.NET教程,数据库应用
分享到: 更多 (0)

相关推荐

  • 暂无文章