Three transaction processes are often encountered in data processing. The methods that are often used in often have the following three summary finishing: Method 1: Direct write to SQL in the stored procedure using Begin Trans, COMMIT TRANS, ROLLBACK TRANS achieve begin transdeclare @orderDetailsError int, @ procuntError intdelete from [order details] where productid = 42select @orderDetailsError = @@ errordelete from products where productid = 42select @procuntError = @@ errorif (@orderDetailsError = 0 and @ procuntError = 0) Commit TranselSerollback Trans Some: All transaction logic is included in a separate call with the best performance running a transaction independently of the application restriction: Transaction context exists only in database call code and database system related to database system 2: Using ADO.NET Implementing using ADO.NET SqlConnection and OLEDBConnection objects have a begintransaction method that returns a SQLTRANSACTION or OLEDBTRANSACTION object.
Moreover, this object has Commit and Rollback methods to manage transactions 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 (); Database connection transactions are executed on the database connection layer, so you need to maintain a database to connect to the ADO.NET distribution transaction during the transaction can also span multiple databases, but one of the SQL Server databases, by using SQL Serv The ER connection server is connected to another database, but if it is between DB2 and ORCAL. The above two transactions are often used transaction processing methods. Method 3 Com transaction (distributed transaction) .NET Framework relies on MTS / COM service to support automatic transactions. COM uses Microsoft Distributed Transaction Coordinator (DTC) to run a transaction in a distributed environment as a transaction manager and transaction coordinator. This allows the .NET application to run across multiple resources (for example, inserting a SQL Server database, writing the message to the Microsoft Message Queuing (MSMQ) queue, and from the Oracle database). Class Com transaction processing must inherit System.EnterpriseServices.ServicedComponent, in fact, Web Service is inheriting System.EnterpriseServices.ServicedComponent, so Web Service also supports COM transactions.
Define a class COM transaction [Transaction (TransactionOption.Required)] public class DataAccess: System.EnterpriseServices.ServicedComponent {} TransactionOption enumeration type values supported COM 5 (Disabled, NotSupported, Required, RequiresNew, Supported) Disabled ignoring the current context Any business in it. NotSupported creates components in context using non-controlled transactions. Required If the transaction is shared, create a new transaction if necessary. RequiresNew uses a new transaction to create components and is independent of the status of the current context. Supported If the transaction exists, shared this transaction. Generally, the components in COM require a Required or Supported. RequiresNew is useful when the component is used for recording or checking, as components should leave with other transaction processing in the activity or rollback. Detective classes can overreload any properties of the base class. If DataAccess is selected with Required, the derived class can still be overloaded and specifying the RequiresNew or other values. The COM transaction has manual processing and automatic processing, and automatic processing is to add [AutoComplete] before the method you need, add [AutoComplete] according to the normal or throwing exceptions based on the method. Manual processing is to call 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 ! "