(a) Introduction
The main feature of a transaction is that the task is either completed or not completed.
(ii) overview
Transactions are managed and coordinated by the transaction manager. Each resource that affects transactional results is managed by an resource manager. The transaction manager communicates with the resource manager to define the results of the transaction.
1. Transaction processing phase
Activation phase: Creates a transaction at this stage.
Preparation phase: At this stage, each resource manager can define the results of the transaction.
Commit phase: When all the resource managers are ready, start this phase.
2. ACID Properties
the characteristics of a transaction can be used in terms ACID to define:
Atomicity(atomicity): Represents a unit of work.
Consistency(consistency): The state must be valid before the transaction starts and after the transaction has completed.
Isolation(Isolation): Indicates that concurrent transactions are independent of state, and state may change during transaction processing.
Durability(persistent): After a transaction is complete, it must be stored in a durable manner.
Ps: Not all transactions require these 4 attributes (for example: Memory transactions do not require persistence).
(iii) Traditional business
1. ADO Business
Public AsyncTask Addcourseasync (stringConnectionStr,stringSQL) { varConnection =NewSqlConnection (CONNECTIONSTR); SqlCommand Coursecommand=connection. CreateCommand (); Coursecommand.commandtext=SQL; awaitconnection. OpenAsync (); SqlTransaction TX=connection. BeginTransaction (); Try{coursecommand.transaction=TX; awaitCoursecommand.executenonqueryasync (); Tx.commit (); } Catch(Exception ex) {Console.WriteLine ("Error:"+Ex. Message); Tx. Rollback (); Throw; } finally{connection. Close (); }}
2, System.EnterpriseServices
through System.EnterpriseServices the advantage of using transactions is that there is no need to explicitly transact, the runtime automatically creates transactions, and only adds the [Transaction] attribute to the class that has the transaction requirements. The [AutoComplete] attribute marks a method to automatically set the state bit of a transaction: If the method succeeds, the success bit is set and the transaction can be committed. If an exception occurs, the transaction is terminated.
[Transaction (transactionoption.required)] Public classcoursedata:servicedcomponent{[AutoComplete] Public AsyncTask Addcourseasync (stringConnectionStr,stringSQL) { varConnection =NewSqlConnection (CONNECTIONSTR); SqlCommand Coursecommand=connection. CreateCommand (); Coursecommand.commandtext=SQL; Connection. Open (); Try{coursecommand.executenonquery (); } finally{connection. Close (); } }}
with System.EnterpriseServices One of the great advantages of creating transactions is that multiple objects can easily be run in the same transaction, and transactions can be automatically enlisted. The disadvantage is that it requires the COM + host model, and the class using this technique must derive from the base class ServicedComponent.
Transaction reference:http://www.cnblogs.com/leslies2/archive/2012/01/05/2289106.html
"Reading notes" C # Advanced Programming Chapter 25th Transaction Processing