About. Implementation of various transactions in net programming

Source: Internet
Author: User

Starting from a database transaction

In the early days, we want to implement a transaction that is typically SQL-based database transactions, typically implemented in SQL query language, as shown below, while updating the price of two books:

BEGIN TRANSACTION  UPDATE tb_book set price=122 where Ident_current=1001update Tb_book set price=88 where Ident_curren t=1002if @ @ERROR <> 0   BEGIN     ROLLBACK TRANSACTION  END ELSE    COMMIT TRANSACTION
Ado. NET transactions and distributed transactions

With. NET technology continues to evolve, can be implemented by ADO, so that we can better apply the transaction in the business logic, rather than the database storage, so as to better realize the separation of business and storage, the transaction is controlled by the business logic, the database is focused on data storage, to achieve the role of accountability, in the ADO transaction is:

using (dbtransaction transaction = connection. BeginTransaction ()) {    command. Transaction = Transaction;    Try    {        command. ExecuteNonQuery ();        Transaction.commit ();    }    catch (Exception e)    {        transaction. Rollback ();        throw;    }}

The above code can only be based on the same database connection, and later in order to implement the remote multiple database transactions, the proposed distributed transaction, KUA database server for Transaction association:

using (TransactionScope TransactionScope = new TransactionScope ()) {    //operations database using in database server 1    (SqlConnection Connection = new SqlConnection (connectionString1))    {        SqlCommand command = new SqlCommand (COMMANDTEXT1, connection);        Connection. Open ();        Command. ExecuteNonQuery ();        Connection. Close ();    }    Manipulating database using in database server 2    (SqlConnection connection = new SqlConnection (connectionString2))    {        SqlCommand Command = new SqlCommand (commandText2, connection);        Connection. Open ();        Command. ExecuteNonQuery ();        Connection. Close ();    }    Transactionscope.complete ();}
File system-based transactions (TXF)

Sometimes, we need to manage the operation of the NTFS file system, for example: first save the picture to the hard disk, then the file path to the database, these two steps are to satisfy the transaction (ACID) principle, the database should be synchronized with the file system, either deleted, or all exist, die. But unfortunately Microsoft is not in. NET to provide such API interface for developers to use, zero bloggers holding the attitude of technical excellence to carry on a toss, Finally, a community abroad found a KTM transaction management solution for the operating system kernel (Kernel32.dll) package, the zero blogger has made improvements to its own needs, and C # for this scenario. NET calls in the following ways:

using (TransactionScope TransactionScope = new TransactionScope ()) {    string commandtext = "UPDATE book SET price=88.50 WHERE id=1001 ";    var fileStream = Transactedfile.open (@ "Log.txt", FileMode.OpenOrCreate, FileAccess.Write, fileshare.write);    StreamWriter streamwrite = new StreamWriter (fileStream);    Streamwrite.writeline (String. Concat (DateTime.Now, CommandText));    Streamwrite.flush ();    Streamwrite.close ();    SqlConnection connection = new SqlConnection (connectionString);    SqlCommand command = new SqlCommand (commandtext, connection);    Connection. Open ();    Command. ExecuteNonQuery ();    Connection. Close ();    Transactionscope.complete ();}

The example code above first creates a TransactionScope transactional environment, associating file operations with database operations Chengcheng the same transaction, Transactedfile is the KTM-based local transaction encapsulation mentioned above, and the database transaction itself supports implicit auto-correlation. When the current person and the latter are associated to the TransactionScope transaction context, a completed transaction is formed. The example above first writes the current time to the disk's log.txt and the SQL statement to be executed, then updates the book price through SQL and rolls back the written log (automatically deleting the log) if the update price fails.

In short: The transaction is a complex system, mainly KTM, DTC and LTM transactions, internal implementation and complex, this article is mainly simple to explain the basic usage of transactions, for the study of the original reason please pay attention to the future of Zero blog article, thank you for your reading, I hope that you have some help!

About. Implementation of various transactions in net programming

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.