Transaction - Manual Affairs (ADO.NET)

zhaozj2021-02-16  58

Transaction type:

1. Manual business: Control the transaction boundary with Explicit instructions that start and end the transaction. The second transaction that is known as nested transactions can be started from a transaction boundary. It is until all subordinate transactions are submitted, and their parent is committed.

2: Automatic transaction: Based on the declaration feature set management transaction boundaries. The transaction automatically flows to the object that is instructing to participate in the transaction and skips the object indicated outside the transaction. Unable to nested the transaction when using an automated transaction model.

Distributed Transaction (TP): Distributed Transaction Processing (TP) system aims to assist transactions in transactions across transactions in a distributed environment.

Manual business and ADO.NET

SQL clients and OLE DB .NET providers support manual transactions in the public language runtime. In ADO.NET, both .NET providers can be used to control transactions.

Local transactions should be started using the ADO.NET connection object Connection.Begintransaction. Use the TRANSACTION property of the Command object to register the commands in the transaction. Use the Transaction object to submit or roll back or roll back to the changes in the data source according to the success or failure of the transaction component.

Registration is automatic by default, and can be disabled. If automatic registration is disabled, you can register manually. Register using Connection.enListDistributeTransaction in an existing distributed transaction.

To perform a transaction, do the following:

Call the Begintractions method of the Connection object to mark the beginning of the transaction. Begintransaction method returns a reference to Transaction. This reference will be assigned to a Command object registered in the transaction. Assign the Transaction object to the transaction property of the Command to be executed. If you execute your Command via an active Transaction object, the Transaction object has not been assigned to the Transaction property of Command, the exception will be triggered. Execute the required command. Call the CommT method of the Transaction object to complete the transaction, or call the Rollback method to cancel the transaction.

MyConnection.open ();

Sqltransaction myTrans = myconnection.begintransaction ();

Sqlcommand mycommand = myconnection.createcommand (); mycommand.transaction = myTrans;

try {myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')"; myCommand.ExecuteNonQuery (); myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description ') "; MyTrand.executenonQuery (); MyTrans.commit (); console.writeline (" Both Records Are Written to Database. ");} Catch (Exception E) {try {myTrans.rollback ();} catch (SQLException EX) {if (MyTrans.Connection! = null) {Console.WriteLine ("An Exception of Type" ex.gettype () "Was Encountered While Attempting to Roll Back The Transaction.");}} distributed transaction:

If the Connection object determines that the transaction is active, it will automatically register in an existing distributed transaction. Automatic transaction registration occurs when the connection is opened or retrieved from the connection pool. This situation, if you want to register your new operation, if you want to register your new operation, you're not possible. At this point, the automatic registration function should be disabled:

SQL Server sets the Enlist field in the connection string for false: enlist = false

OLEDB is OLE DB SERVICES = -7

You can use EnlistDistributeTransAction to register with Connection in the transaction.

SqlConnection nwindconn = new sqlconnection (".......; enlist = false;");

SQLCommand cmd = new sqlcommand ("...", nwindconn);

...

nwindconn.open ();

ITransaction trans = (itransaction) ContextUtil.Transaction; // Get current distribution transactions

NWINDCONN.ENLISTDISTRIBUTEDTRANSACTION (TRANS); // must be nwindconn.open ();

Try {cmd.executenonQuery (); // Execute transaction

Contextutil.setComplete (); // Submit transaction} catch (sqlexception e) {contextUtil.seTabort ();} finally {nwindconn.close ();

转载请注明原文地址:https://www.9cbs.com/read-21131.html

New Post(0)