欢迎光临
我们一直在努力

ASP数据库事务处理

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

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 arent 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 its an all-or-none deal. no halfways, no maybes. lets suppose

youre 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 doesnt, 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 doesnt

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?

its always got to be about "me," doesnt it? well, if you didnt gather it from above, transactions are

mainly used for error handling. they stop you from getting in weird positions that you cant get out of by

easily allowing you to undo the changes youve 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 microsofts answer to transactions. we wont 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

wont be apparent in your asps, mts is working busily behind the scenes to keep track of what youre

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

control panel).

the basics

so what does transaction asp code look like? the answer is, exactly like regular asp code. we must simply

add a one line directive to the beginning code to get it to behave properly:

<%@ transaction = value %>

where value can be any one of the following:

requires_new

the script will always start a new transaction.

required

the script will use a transaction, and will participate in any open transaction, or create a new one if

none are open.

supported

the script will use any open transaction but will not start a new one if none are currently open.

not_supported

the script will neither initiate nor participate in a transaction.

typically, each asp page will be its own transaction, however, it is possible to continue transactions

across more than one page by using the server.transfer and server.execute methods (new to iis 5). if the

calling page is transacted, and the current page uses "transaction = required" or "transaction =

supported" then the current page will continue the existing transaction, which makes some very complex

applications possible.

now you simply write asp code as you would normally, and things will work transactionally.

committing and rolling back

there are two ways to commit a transaction (meaning that everything is okee-dokee and there are no errors

in the process). the first (and more simple) method is to do this inline with your code. lets take a

look:

<%@ transaction = required %>

<%

if request.form("submit") = "accept" then

objectcontext.setcomplete

response.write("operation completed successfully")

end if

%>

use the objectcontext.setcomplete method to tell mts that this process is ready to be committed, and no

roll backs are required. you can then do whatever you want after you know the transaction is committed

(for instance, transfer funds in the bank scenario, or simply alert the user). this method is easy to use

since you can do this anywhere in the code you wish – you can use it as a simple error checking procedure.

to roll back the transaction, you use the objectcontext.setabort method:

<%

if request.form("submit") = "decline" then

objectcontext.setabort

response.write("operation was unsuccessful")

end if

%>

this tells mts that there were errors, and we need to roll back to the previous state. calling

objectcontext.setabort rolls back any data that has changed, and then you can perform any tasks necessary

(such as displaying an error message to the user).

the second method to commit changes is a bit more complex, but offers you much more functionality. here we

use transaction events to perform the functions.

<%@ transaction = required %>

<%

strtext = request.form("textbox")

if strtext = "" then

objectcontext.setabort

response.write("you did not enter the text<p>")

else

response.write("processing transaction…<p>")

end if

%>

<%

commit and rollback code

sub ontransactioncommit()

response.write("operation was successful")

end sub

sub ontransactionabort()

response.write("operation was unsuccessful")

end sub

%>

in this scenario, weve moved the commit and abort functions out of the inline code to separate

procedures. our script runs normally in the first script block. in the second script block, we create the

sub procedures ontransactioncommit and ontransactionabort. these two procedures tell the application what

to do if the transaction is successful or unsuccessful.

note that the only way to let the application know the transaction is unsuccessful is by calling the

objectcontext.setabort method as outlined above. unless you explicitly call this method somewhere in the

code, mts will always think the transaction is successful, and therefore execute the code in

ontransactioncommit. for example, if your code completes its execution, and no setabort method has been

called, ontransactioncommit will run. on the other hand, as soon as you call setabort, the code execution

stops, the process rolls back, and then ontransactionabort runs.

in the following code, even though the application should err out when the variable strtext is empty, mts

thinks this is successful, and will always execute ontransactioncommit. this can have dire consequences in

a real world situation, so dont forget to tell the application where to stop in case of an error!

<%@ transaction = value %>

<%

strtext = request.form("textbox")

if strtext = "" then

response.write("you did not enter the text.<p>")

else

response.write("processing transaction…<p>")

end if

%>

<%

commit and rollback code

sub ontransactioncommit()

response.write("operation was successful")

end sub

sub ontransactionabort()

response.write("operation was unsuccessful")

end sub

%>

note: it is not necessary to separate code blocks as ive shown above. the examples are done in this way

simply for claritys sake. you can combine the @transaction directive and all other code in one <%…%>

script block.

we have done very simple examples here, but these can easily be expanded to very complex code. it is

simple even to modify existing code to take part in transactions simply by adding the @transactions

directive and making sure to properly use setabort and build the transaction events ontransactioncommit

and ontransactionabort.

com objects and mts

many times you will want to use transactions with custom built components, or com objects, in your asps.

in the bank scenario, for example, it would probably be a good idea to encapsulate the business logic of

transferring the funds in a separate com object for security, flexibility, and architecture reasons.

when using com objects with transactions, you can either build the transaction logic into the component,

or keep it in the asp. for example, if your com object has a method called transferfunds, then you can

either build in error checking and call setabort in the transferfunds method itself, or keep that code in

the asp script as weve done above (just remember to put it somewhere). this decision is not crucial,

though it is necessary (and will depend on the overall architecture of your web application).

in order for a com object to work transactionally, you must register it with mts. the microsoft

transaction server keeps track of all components on your system that can participate in transactions, and

tells each component how to do so (i.e. required, requires_new, support, or not_supported). if you do not

register your component, it may function normally, but will not be able to work transactionally, and may

also produce errors when asked to do so.

conclusion

by using transactions in asp, you are opening up a whole new world of professional functionality.

transactions make your applications stronger, and can ease development pains. the best part is that they

are simple to use once youve learned the basics.

happy scripting!

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