Transaction of ASP.NET data operations at three levels

Source: Internet
Author: User
Tags commit config empty insert modify rollback
asp.net| data
Many database operations need to be transacted, and there are roughly 3 levels of transactions under ASP.net:
(1) Transaction at the level of the stored procedure
(2) Ado.net-level transactions
(3) asp.net transaction at the page level
Here are some examples:
First create Trantest table, field ID (int), test (char)
Set primary key for ID (use primary key is not allow duplicate attribute for transaction test)

(1)
CREATE PROCEDURE Tran1
As
BEGIN Tran
Insert into Trantest (id,test) VALUES (1, ' Test ')
Insert into Trantest (id,test) VALUES (1, ' Test ')
if (@ @error =0)
Commit Tran
Else
Rollback Tran
Go
Run this stored procedure to find that there is no record, it is true that the rollback
Empty the database record and modify the stored procedure
CREATE PROCEDURE Tran1
As
BEGIN Tran
Insert into Trantest (id,test) VALUES (1, ' Test ')
Insert into Trantest (id,test) VALUES (2, ' test ')
if (@ @error =0)
Commit Tran
Else
Rollback Tran
Go
Running this stored procedure can find that 2 records have been added, stating that it is true that a transaction was committed
Empty a record of a database

(2)
SqlConnection conn=new SqlConnection (system.configuration.configurationsettings.appsettings["Conn"));
SqlCommand cmd1=new SqlCommand ("Insert into Trantest (id,test) VALUES (1, ' Test ')", conn);
SqlCommand cmd2=new SqlCommand ("Insert into Trantest (id,test) VALUES (1, ' Test ')", conn);
Conn. Open ();
SqlTransaction Tran=conn. BeginTransaction ();
Cmd1. Transaction=tran;
Cmd2. Transaction=tran;
Try
{
Cmd1. ExecuteNonQuery ();
Cmd2. ExecuteNonQuery ();
Tran.commit ();
}
catch (SqlException except)
{
Tran. Rollback ();
Response.Write (except. message);
}
Finally
{
Conn. Close ();
}
The same run results in what records are not added, emptied and then modified to
SqlCommand cmd1=new SqlCommand ("Insert into Trantest (id,test) VALUES (1, ' Test ')", conn);
SqlCommand cmd2=new SqlCommand ("Insert into Trantest (id,test) VALUES (2, ' Test ')", conn);
There are 2 records in the database after running

(3)
Add Reference System.EnterpriseServices.dll
Using System.EnterpriseServices;

ServiceConfig config = new ServiceConfig ();
Config. Transaction = transactionoption.required;
Servicedomain.enter (config);
Try
{
Work1 ();
Work2 ();
ContextUtil.SetComplete ();
}
catch (System.Exception except)
{
ContextUtil.SetAbort ();
Response.Write (except. message);
}
Finally
{
Servicedomain.leave ();
}

Then add 2 actions to the page to simulate an operation that is not in the same class in the logical layer
private void Work1 ()
{
SqlConnection conn=new SqlConnection (system.configuration.configurationsettings.appsettings["Conn"));
SqlCommand cmd1=new SqlCommand ("Insert into Trantest (id,test) VALUES (1, ' Test ')", conn);
Conn. Open ();
Cmd1. ExecuteNonQuery ();
Conn. Close ();
}

private void Work2 ()
{
SqlConnection conn=new SqlConnection (system.configuration.configurationsettings.appsettings["Conn"));
SqlCommand cmd2=new SqlCommand ("Insert into Trantest (id,test) VALUES (1, ' Test ')", conn);
Conn. Open ();
Cmd2. ExecuteNonQuery ();
Conn. Close ();
}

After emptying the database, it was found that no records were written, the database was emptied again, and the WORK2 () was modified.
SqlCommand cmd2=new SqlCommand ("Insert into Trantest (id,test) VALUES (2, ' Test ')", conn); After the run, 2 records were added.

Description 2 point:
1,
ServiceConfig config = new ServiceConfig ();
Config. Transaction = transactionoption.required;
Servicedomain.enter (config);
To the final servicedomain.leave (); Said
In this section of the transaction, if you want to simply for the entire page transactions, modify the following
Try
{
Work1 ();
Work2 ();
ContextUtil.SetComplete ();

}
catch (System.Exception except)
{
ContextUtil.SetAbort ();
Response.Write (except. message);
}
It's okay, and don't forget to add it to the front page.
transaction= "Required"
2. Do not add try{}catch{} code block in Work1 (), WORK2 ()
3, because my machine is XP SP2, did not notice this problem, by Shi eldest brother reminder, suddenly dawned, add, have platform limit, can only be used in windos2003 or XP XP2, otherwise will give "the current platform does not support" ServiceConfig "exception information, When you have the opportunity to test yourself again. To use in XP SP1 can download the patch, refer to:
Http://www.alexthissen.nl/Weblog/PermaLink.aspx?Guid=f6d61461-d336-40b0-9f4d-51eab6650f27
Http://www.rm.com/Support/GeneralDownload.asp?cref=DWN222592&nav=0


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.