在ASP資料庫交易處理

來源:互聯網
上載者:User
交易處理|資料|資料庫 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



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。