Use KTM (kernel Transaction Manager) for file Transaction Processing

Source: Internet
Author: User

In my recent articles about transaction processingArticleFrom the overall concept of transaction processing to the specific C #CodeBasically all the practical operations can meet the daily development needs. Most of the transaction scope operations in this article are limited to databases.. NetBrief Introduction to custom transaction Resource Manager "Although I have implemented a simple Custom Resource Manager, it can also meet basic project requirements and core functions, but we cannot help with file transaction operations.[Wang qingpei has all rights reserved. For more information, please sign it.]

From the database to the Custom Resource Manager, you can all participate in transaction processing and ensure data integrity when necessary. Therefore, we lack a type of resource operation. Of course, you may have long wanted to ask, what should I do with transaction operations on the file system? So there is a mature solution for file transaction operations, which was not feasible in the past few years, but Microsoft has recently released a transaction NTFS system. Understand the advantages and benefits of the NTFS file system. Compared with fat and other HPFs file systems, file transaction processing only supports the NTFS format file system.

Transaction NTFS is also called txf, which is only supported by the latest Windows System (windowsvista \ windows7 \ windowsserver2008 \ windowsserver2008r2), so do not test it on XP.[Wang qingpei has all rights reserved. For more information, please sign it.]

The ltm local Transaction Manager is used in the previous articles, and then multiple persistent resource registration Automatic transactions in the transaction scope class are upgraded to transactions in the DTC type, due to the unmanaged Implementation of DTC, there will be performance loss of data sending in Distributed Transaction operations. msdn also advocates using DTC as little as possible, there are many uncertain factors that are difficult to solve. However, this is still necessary at the key point, and we need to study it.

KTM,DTC,LtmBrief Introduction to the usage relationship between the three elements

The previous ideas and explanations have little to do with KTM. However, due to his appearance, it is necessary for us to return to the origin for a more systematic and in-depth understanding, only understanding;

A large number of msdn documents and system. after reading the transaction namespace carefully, we found that Microsoft has hidden a lot. net transaction implementation details, such as system. transaction. the implementation of specific distributed protocols in the oletx namespace does not refer to any technical documents. You can only decompile the files and read the code on your own.

We combed from ltm. ltm is the local transaction manager, so its existence can only be in the current managed appdomain, and it cannot boast remote processing, once the cross-remote processing is responsible for the propagation of objects, it is necessary to improve the functions of local transactions, including a series of banding elements in WCF and transaction-aware Code, which must all manage transactions, however, most of the Code is provided by the system.[Wang qingpei has all rights reserved. For more information, please sign it.]

Decompilation reads some code, which involves Code such as P \ invoke and com \ InterOP. Based on your understanding, it aims to start the idtctransaction interface, that is, the COM interface. Understanding this is very beneficial for the following KTM operations. To manage DTC, ltm must load com interfaces through ole32.dll, that is, the idtctransaction interface in the. NET class library hosted by us. Let's take a look at the Code:

// Describes a DTC transaction. [GUID ("0fb15084-af41-11ce-bd2b-204c4f4f5020")] [interfacetype (cominterfacetype. interfaceisiunknown)] public interface idtctransaction

 

This interface is the type used when it is made public to com and used for com interoperability. You need to use this interface to improve the usage of DTC. to verify whether it is correct, we will conduct a simple test, we manually use system. transaction. the transactioninterop class is used to obtain the unmanaged idtctransaction interface. See the Code:

Ltm transaction:

 
Public static void startcopy () {using (transactionscope transcope = new transactionscope () {transcope. Complete ();}}

 

This code will not be upgraded to DTC management. We will add a line of code:

Public static void startcopy () {using (transactionscope transcope = new transactionscope () {idtctransaction idtc = transactioninterop. getdtctransaction (transaction. Current); transcope. Complete ();}}

 

First explain the role of the transactioninterop class, from the description of msdn:

"Promotes the interaction between system. Transactions and previously written components used to interact with MSDTC, COM +, or system. enterpriseservices. This class cannot be inherited ."

In fact, this class is mainly used to interoperate early Distributed Transaction technologies, for example, to obtain the COM object related to DTC or to conduct custom transaction propagation, for the complex oletx (Binary communication protocol for Windows) protocol, we do not need to care too much about core things to transmit distributed transactions. remoting may have this requirement here.[Wang qingpei has all rights reserved. For more information, please sign it.]

You can use the transactioninterop. getdtctransaction method to obtain the DTC transaction interface.

Figure 1:

With the transactioninterop class, our subsequent extensions are much more convenient.

Because KTM is an unmanaged implementation, the operating system provides transactional API methods for file operations:

Non-transaction processing API

Transaction Processing API

Createfile

Createfiletransacted

Copyfileex

Copyfiletransacted

Movefilewithprogress

Movefiletransacted

Deletefile

Deletefiletransacted

Createhardlink

Createhardlinktransacted

Createsymboliclink

Createsymboliclinktransacted

Createdirectoryex

Createdirectorytransacted

Removedirectory

Removedirectorytransacted

By encapsulating these methods, you can implement transactional file operations. Currently,. Net does not encapsulate mature class libraries for us. It is estimated that these methods may be provided in later versions.

So how can we use KTM transaction processing? Fortunately, through the msnd connection, we can get the source code compiled by Microsoft's transaction developers, which is:

Http://download.microsoft.com/download/f/2/7/f279e71e-efb0-4155-873d-5554a0608523/TxF2007_07.exe

The source code is encapsulated by the above API, which involves a lot of details about the communication between the internal API and COM. We can look at the code written by foreigners as a complex example.

As we mentioned above, we only need to boast about the current application.ProgramThe transaction processing of the domain is automatically upgraded to the DTC transaction. The API call is already out of the interoperability type. Currently, due to remote calls, the DTC has an interactive implementation with the hosted domain, therefore, we only need to access KTM through DTC. This is also the official explanation of msdn.

Figure 2:

Let's look at a simple example. This example is used to delete files in a transactional manner.

Example 1:

Public static void startdelete () {try {using (transactionscope transcope = new transactionscope () {console. writeline ("Enter the file to be deleted"); string Path = console. readline (); Microsoft. ktmintegration. transactedfile. delete (PATH); console. writeline ("submit transaction processing? "); If (console. readline () = "Y") transcope. complete (); else transaction. current. rollback () ;}} catch (exception ERR) {console. writeline (ERR );}

 

I simply wrote a piece of test code, and the test is OK. KTM can be combined with DTC and ltm for hybrid transaction processing. The problems we introduced above can be solved perfectly now.

Reference: http://msdn.microsoft.com/zh-cn/magazine/cc163388.aspx

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.