asp.net three kinds of transaction processing

Source: Internet
Author: User
Tags commit manual require rollback msmq oracle database
asp.net| Transaction Processing

Three kinds of transaction processing
Transaction processing is often encountered in the processing of problems, often used methods have the following 3 kinds of summary collation as follows:
Method 1: Write directly to SQL
Use the BEGIN TRANS in the stored procedure, COMMIT TRANS, ROLLBACK TRANS Implement
Begin Trans
declare @orderDetailsError int, @procuntError int
Delete FROM [order Details] where productid=42
Select @orderDetailsError =@ @error
Delete from the products where productid=42
Select @procuntError =@ @error
if (@orderDetailsError =0 and @procuntError =0)
COMMIT TRANS
Else
ROLLBACK TRANS
Advantages:
All transaction logic is contained in a separate call
Have the best performance for running a transaction
Independent of application
Limit:
Transaction context exists only in database calls
Database code is related to the database system
Method 2: Use Ado.net to implement
Using the Ado.net implementation, the advantage of this approach is that you can manage transactions in the middle tier, and of course you can choose to implement them at the data layer.
The SqlConnection and OleDbConnection objects have a BeginTransaction method that can return sqltransaction
or OleDbTransaction object. And this object has a Commit and Rollback method to manage the transaction.
SqlConnection SqlConnection = new SqlConnection ("Workstation Id=weixiaoping;packet size=4096;user id=sa;initial Catalog=northwind;persist security Info=false ");
Sqlconnection.open ();
SqlTransaction Mytrans = SqlConnection.BeginTransaction ();
SqlCommand Sqlinsertcommand = new SqlCommand ();
Sqlinsertcommand.connection = SqlConnection
Sqlinsertcommand.transaction=mytrans;
try{
sqlinsertcommand.commandtext= "INSERT into Tbtree (Context,parentid) VALUES (' Beijing ', 1)";
Sqlinsertcommand.executenonquery ();
sqlinsertcommand.commandtext= "INSERT into Tbtree (Context,parentid) VALUES (' Shanghai ', 1)";
Sqlinsertcommand.executenonquery ();
Mytrans.commit ();
}catch (Exception ex)
{
Mytrans.rollback ();
}
Finally
{
Sqlconnection.close ();
}
Advantages:
of simplicity
It's almost as fast as a data transaction.
Proprietary code for different databases is hidden, independent of the database
Disadvantages:
Transactions cannot be connected across multiple databases
Transaction execution is on the database connection layer, so a database connection needs to be maintained during the transaction
Ado. NET distributed transactions can also span multiple databases, but one of the SQL Server databases is connected to another database by using a SQL Server connection server, but it is not possible between DB2 and orcal.
Both of these transactions are frequently used transaction processing methods.
Method 3 COM + transactions (distributed transactions)
The. NET Framework relies on the mts/com+ service to support automatic transactions. COM + uses Microsoft distributed Transaction Coordinator (DTC) as the transaction manager and transaction Coordinator to run transactions in a distributed environment.
This enables a. NET application to run a combination of different operations across multiple resources (for example, inserting an order into a SQL Server database, writing a message to a Microsoft Message Queuing (MSMQ) queue, and retrieving data from an Oracle database)
of the transaction.
COM + transaction classes must inherit System.EnterpriseServices.ServicedComponent, but the web Service is to inherit System.EnterpriseServices.ServicedComponent, so Web service also supports
COM + transactions.
Define a class for COM + transactions
[Transaction (transactionoption.required)]
public class DataAccess:System.EnterpriseServices.ServicedComponent
{

}
TransactionOption enumeration type supports 5 COM + values (disabled,notsupported,required,requiresnew,supported)
Disabled ignores any transactions in the current context.
NotSupported creates a component in the context using a non-controlled transaction.
Required If a transaction exists, the transaction is shared and a new transaction is created if necessary.
RequiresNew creates a component with a new transaction, regardless of the state of the current context.
Supported if a transaction exists, the transaction is shared.
Generally speaking, components in COM + require required or supported. RequiresNew is useful when the component is used for recording or checking, because the component should be isolated from the commit or rollback of other transactions in the activity.
Derived classes can overload any property of the base class. Derived classes can still overload and specify RequiresNew or other values, as DataAccess chooses required.

COM + transactions have manual processing and automatic processing, which is to add [AutoComplete] before the method that is required to be processed automatically, and to commit or rollback depending on the normal or thrown exception of the method.
Manual processing is to invoke the Enablecommit,setcomplete,setabort method in the ContextUtil class.
public string testtransaction ()
{
Try
{
Contextutil.enablecommit ();
InsertARecord1 ();
InsertARecord2 ();
ContextUtil.SetComplete ();
return "succeed!";
}
catch (Exception ex)
{
ContextUtil.SetAbort ();
return "failed!";
}

}
public void InsertARecord1 ()
{

String strconn= "Workstation Id=weixiaoping;packet size=4096;user id=sa;initial catalog=northwind;persist Security Info=false ";
SqlConnection conn=new SqlConnection (strconn);
Conn. Open ();
SqlCommand command=new SqlCommand ("INSERT into Tbtree (Context,parentid) VALUES (' Beijing ', 1)", conn);
Command. ExecuteNonQuery ();
Conn. Close ();

}
public void InsertARecord2 ()
{

String strconn= "Workstation Id=weixiaoping;packet size=4096;user id=sa;initial catalog=northwind;persist Security Info=false ";
SqlConnection conn=new SqlConnection (strconn);
Conn. Open ();
SqlCommand command=new SqlCommand ("INSERT into Tbtree (Context,parentid) VALUES (' Shanghai ', 1)", conn);
Command. ExecuteNonQuery ();
Conn. Close ();
}
In systems that require transactions to run across MSMQ and other identifiable transactions (for example, a SQL Server database), only DTC or COM + transactions can be used, and there is no alternative. DTC coordinates all the resource managers involved in a distributed transaction,
Also manages transaction-related operations.
The disadvantage of this approach is that performance is reduced due to the existence of DTC and COM interoperability overhead.
The classes for COM + transactions must be strongly named.



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.