在ASP数据库事务处理

事务处理|数据|数据库

ASP Transactions

by Chris Payne Introduction

Transactions are important to maintain data integrity, among other things, and have been used with
databases for some time now. Luckily, transactions aren't restricted to databases - you can use them in
Active Server Pages as well, and without having to create custom components using Microsoft Transaction
Server (MTS). This article will take a look at what transactions are, how they will help ASP developers,
and how to implement them.

What are Transactions?

Before we dive into the meat, we have to understand the terminology (believe me, I was lost for a long
time because no one explained these terms to me).

Basically, using a transaction means it's an all-or-none deal. No halfways, no maybes. Let's suppose
you're modifying a database, and you have to add 100 new items, one at a time. Before adding each item,
however, you have to check to make sure it fits certain criteria - if it doesn't, then your database
cries, you receive an error, and the process quits. It may not be desirable to quit halfway - for whatever
reason, you either want all the new rows or none of them.

Enter transactions. If you put the process in a transaction, and if an error occurs, the transaction
will "roll back" to a state just before you started your process. Thus, if any of the new items doesn't
meet the criteria, none of items gets added, instead of only the first few.

The classic example is a banking application. Suppose someone wants to transfer funds from one account to
another. You would deduct x amount from account A, and then add it to account B. Suppose after you have
deducted the money from account A (and before you add it to account B), you notice a problem (perhaps
account B has restrictions, or is not available for some reason). If the application just quits here with
some error message, then account A will be in an incorrect state, meaning it has funds missing. These
funds are not in account B yet, so they are just floating off in cyberspace, and you have one very unhappy
customer. With transactions, should you encounter this error, the process will roll back to a previous
state, and the money will automatically go back to where it started. If there are no problems, however,
the transaction will commit, and everything will go through.

Another example more apropos to web development is user registration. Suppose a user comes to your
community building web site and decides to register. Once they submit their information, you have to enter
it into a database, and also set up their personal web page. Thus, every user in the database must have a
personal web page as well, or no one will ever know that they are there. Your ASP script adds the user
into the database perfectly, but a server error causes your web page building routine to fail
unexpectedly. You now have a floating user - a user in the database, but without a web page. If this
process were transactional, you could roll back to just before you inserted the user into the database
(thereby eliminating the floating user problem), and gracefully display a nice error message to the user.
Pretty slick, huh?

So How Does it Help Me?

It's always got to be about "me," doesn't it? Well, if you didn't gather it from above, transactions are
mainly used for error handling. They stop you from getting in weird positions that you can't get out of by
easily allowing you to 'undo' the changes you've made. As you develop more and more complex web
applications, the need for strong state protection will become increasingly necessary.

What is MTS?

MTS is Microsoft's answer to transactions. We won't go into too much detail here, but MTS is the engine
that keeps track of object states, and rolls them back or commits them when necessary. Even though it
won't be apparent in your ASPs, MTS is working busily behind the scenes to keep track of what you're
doing.

MTS requires all objects that use transactions to be registered in MTS. IIS itself has many different
components that process web applications that are all registered with MTS, which is why ASPs can run
transactionally through MTS, invisible to the user (and sometimes to the developer). So just trust us when
we say you need MTS.

NOTE: Though MTS is installed with Windows NT, it usually is not set up to run automatically. Therefore,
before running any of the examples below, make sure the MTS service (MSDTC) is running (in the services
contr

时间: 2024-09-23 17:04:02

在ASP数据库事务处理的相关文章

利用ASP实现事务处理的方法

事务处理|事务处理 在开发Web应用时,无一例外地需要访问数据库,以完成对数据的查询.插入.更新.删除等操作.受应用逻辑的影响,有时需要将多条数据库操作指令组成一个工作单元(事务).在数据库中,所谓事务是指一组逻辑操作单元,它使数据从一种状态变换到另一种状态.为确保数据库中数据的一致性,应当用离散的成组的逻辑单元操作数据:当它全部完成时,数据的一致性可以保持:而当单元中的一部分操作失败时,整个事务会被全部忽略,所有从起始点以后的操作全部退回到开始状态. 实际上,在默认方式下对数据库的每一次操作都

利用ASP实现事务处理的方法_应用技巧

 利用ASP实现事务处理的方法     选择自 AppleBBS 的 Blog   关键字   利用ASP实现事务处理的方法  出处      在开发Web应用时,无一例外地需要访问数据库,以完成对数据的查询.插入.更新.删除等操作.受应用逻辑的影响,有时需要将多条数据库操作指令组成一个工作单元(事务).在数据库中,所谓事务是指一组逻辑操作单元,它使数据从一种状态变换到另一种状态.为确保数据库中数据的一致性,应当用离散的成组的逻辑单元操作数据:当它全部完成时,数据的一致性可以保持:而当单元中的一

ASP数据库服务器SQL Server

server|服务器|数据|数据库|服务器 微软的SQL Server是一个关系数据库,它是一项完美的客户/服务器系统.SQL Server需要安装在Windows NT的平台上,而Windows NT可以支持Intel 386,Power PC,MIPS,Alpha PC和RISC等平台,它使SQL Server具备足够的威力和功能. 这里所有的文章所采用的数据库应用程序都是基于SQL Server之上的,采用ODBC及标准的SQL查询,可以非常简单的移植到任何一个支持ODBC的数据库之上,如

SQL和Oracle对数据库事务处理的差异性

在吉日嘎拉的软件编程走火入魔之:数据库事务处理入门(适合初学者阅读)文章中关于MS SQL Server和Oracle对数据库事务处理的差异性引起一些争论,因此记录我对数据库事务处理的想法. 简介 本文讲述MS SQL Server和Oracle对数据库事务处理的差异性,以及Oracle如何对事务处理的实现. 什么是事务 数据库事务(Database Transaction)是一组数据库操作的处理单元.事务符合ACID的特性: Atomic:原子性,要么全部要么一无所有.All or None.

ASP数据库简单操作教程

教程|数据|数据库 ASP数据库简单操作教程 <1 >.数据库连接(用来单独编制连接文件conn.asp) < % Set conn = Server.CreateObject("ADODB.Connection") conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("\bbs\db1\user.mdb") % >

如何做一个高效的ASP数据库操作程序

程序|数据|数据库 <!-- 蛙蛙推荐:如何做一个高效的ASP数据库操作程序一般情况下我们做的ASP数据库程序都是ADO+ACCESS,并且都是使用一些查询字符串加记录集来操作数据库,最多也只使用了connection和recordset两个对象以及它们的几个常用的属性和方法,其实ADO的使用远不仅这些,我们还有command对象和Parameters对象没有用呢,而这两个对象用好了会提高你整个ASP程序的性能.我这里写了一个歌词管理程序,用的是sqlserver数据库和存储过程实现的,(这里没

asp数据库操作类

<%'=========================================================================='文件名称:clsDbCtrl.asp'功 能:数据库操作类'作 者:coldstone (coldstone[在]qq.com)'程序版本:v1.0.5'完成时间:2005.09.23'修改时间:2007.10.30'版权声明:可以在任意作品中使用本程序代码,但请保留此版权信息.' 如果你修改了程序中的代码并得到更好的应用,请发送一份给我,谢

asp+数据库做电子商务

问题描述 后台程序用什么比较好? 解决方案 解决方案二:就别用asp了解决方案三:那用什么?做毕业设计而已解决方案四:毕业设计...呵呵,做出来也不是为了用的.既然你自己都说asp+数据库了,为什么还问后台程序用什么好呢?后台程序不就是asp么?解决方案五:asp.NET好学么?我现在还不会...解决方案六:引用4楼shanxidaxuehoupeng的回复: asp.NET好学么?我现在还不会... 好学.有基础的话,很快上手的.解决方案七:都毕业设计了还说这样的话就算你立马学,能做出东西??

asp数据库备份程序

  asp数据库备份程序,这程序是我初学asp时用fso写的一个在线文件备份代码,简单也比较实用了,只是只对access数据库,备份原理就是把文件复制到我们指点的地方了,好了下面看源码. <%   dim action,start,index1   start=request.Form("start")   index1=request.Form("index")   action=request.QueryString("action")