[Reading Notes] C # advanced programming Chapter 1 transaction processing,

Source: Internet
Author: User

[Reading Notes] C # advanced programming Chapter 1 transaction processing,

(1) Introduction

The main feature of a transaction is that the task is either completely completed or not completed.

 

 

(2) Overview

Transactions are managed and coordinated by the Transaction Manager. Each resource that affects the transaction result is managed by a resource manager. The transaction manager communicates with the resource manager to define the transaction results.

 

1. Transaction Processing Stage

Activation phase: Create a transaction in this phase.

Preparation stage: At this stage, each resource manager can define the transaction results.

Submission phase: This phase starts when all resource managers are ready.

 

2. ACID properties

The features of transactions can be defined using ACID as the term:

Atomicity (Atomicity): represents a unit of work.

Consistency: The status before the start of the transaction and after the completion of the transaction must be valid.

Isolation (Isolation): indicates that concurrent transactions are independent of the State, and the State may change during transaction processing.

Durability: after a transaction is completed, it must be stored in a persistent manner.

Ps: Not every transaction requires these four attributes (for example, memory transactions do not require persistence ).

 

 

(3) traditional transactions

1. ADO. NET transaction

public async Task AddCourseAsync(string connectionStr, string sql){    var connection = new SqlConnection(connectionStr);    SqlCommand courseCommand = connection.CreateCommand();    courseCommand.CommandText = sql;    await connection.OpenAsync();    SqlTransaction tx = connection.BeginTransaction();    try    {        courseCommand.Transaction = tx;        await courseCommand.ExecuteNonQueryAsync();        tx.Commit();    }    catch (Exception ex)    {        Console.WriteLine("Error:" + ex.Message);        tx.Rollback();        throw;    }    finally    {        connection.Close();    }}

 

 

2. System. kerberiseservices

Using System. enterpriseServices the advantage of using transactions is that you do not need to perform explicit Transaction processing. The runtime automatically creates transactions. You only need to add the [Transaction] feature to classes that have Transaction processing requirements. The [AutoComplete] feature marks the method as the automatic setting of the Transaction Status bit: if the method is successful, it sets the successful bit, so you can submit the transaction. If an exception occurs, terminate the transaction.

[Transaction(TransactionOption.Required)]public class CourseData: ServicedComponent{    [AutoComplete]    public async Task AddCourseAsync(string connectionStr, string sql)    {        var connection = new SqlConnection(connectionStr);        SqlCommand courseCommand = connection.CreateCommand();        courseCommand.CommandText = sql;        connection.Open();        try        {            courseCommand.ExecuteNonQuery();        }        finally        {            connection.Close();        }    }}

One advantage of using System. EnterpriseServices to create a transaction is that multiple objects can be easily run in the same transaction, and the transaction can be automatically registered. The disadvantage is that it requires the COM + host model. The class using this technology must be derived from the base class ServicedComponent.

 

 

Transaction reference: http://www.cnblogs.com/leslies2/archive/2012/01/05/2289106.html

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.