Question Locked

.NET TRANSACTION

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Can anyone help know how to implement transaction in asp .net? Just like the database transaction, can we put some lines of code in a transaction ...for example

begin transaction
{
myMethodToSendMail() //sends email

myMethodToWrite() //Writes into an file e.g. xml file

myMethodToDB() //updates database
}
commit

Is there any way to achieve the above situation via asp .net using c#? I dont think the class SqlTransaction is the one to use......can anyone help...thanks in advance

4 replies

by tony hopkinson

which aspect of transaction do you want ?

For instance do you want to lock the xml file while the db completes or not.

What is an uncommitted write to a file, how do you roll it back?

If sendmail works, what are you going to do if the file is locked recall the email?

If all three methods are simply meant to be using the same data, then that's a completely different animal.

by packer.lean

Well thanks Tony for the reply,

I mentioned about the email just for an example. My main requirement is to maintain the atomicity on a section of code. Either all will execute or none...lets forget email sending for the time being, let it be

begin transaction
{
writeToFile() //Write to xml file
sendEmail() //Send an email
}

I came across following class which seem to solve the problem;

System.Transactions.TransactionScope

Read about it at following URL
http://msdn2.microsoft.com/en-us/library/system.transactions.transactionscope.aspx[^]

But the problem is that I am working on .NET 1.1 while it is supported on .NET 2.0 and above. Can you suggest some way out?

by tony hopkinson

won't deal with a specific rollback algorithm as far as I can see.

If the write and send operate on the same data you could lock it, or associate a lock object with it to make the statements atomic

Rolling back a file write?

YOu could save the entire file before you write to it and then put it back together on failure.

Maybe you could just save the node you were writing.

I'm only guessing by your examples, but Queues and adjustment to your xml would appeal to me

....

In write XML rip out any old version content. Find teh node you are going to change,add it to the new version of the node .

Roll back would then simply 'promote' it back.

Save failure simply does queue or revokes the queued associated send mail request.

An idea anyway...

This discussion is closed to new replies.