A Developer Introduction to the Kernel Transaction Manager (KTM) and Windows Transactional File System (TxF)

The lack of operating system support for transactions has resulted in some tedious data management solutions that needed to be hand-implemented by developers to minimize the risk of data corruption in the event of application crashes or unexpected system shut-downs such as blue-screens. The typical pattern to reduce the chance of data corruption is to narrow an operation against a non-transactional resource down to the most 'atomic' operation possible such as a file move or rename, and then to use temporary resources that can be swapped into place once a long chain of non-transactional operations have been completed. The need to hand-implement this pattern is tedious, and fails to take into account any other transaction scopes that exists.

To address this problem, Windows Vista shipped with the Kernel Transaction Manager (KTM) for use in C++ applications. The KTM works with the Common Log File System (CLFS), which is another new operating system feature that is available in Windows Server 2003 R2 onwards, to provide basic transaction support to both system and application developers. The client API of the KTM is quite simple - functions to create, commit and rollback transactions exist, and the Windows Distributed Transaction Coordinator (DTC) has also been extended in Windows Vista to support a new interface called IKernelTransaction that allows the KTM to work with other resource managers like a SQL Server database.

The KTM allows any resource manager to enlist its services to implement either volatile or durable transactions. Volatile transactions do not use a log file, while durable transactions use the facilities of the CLFS to provide recovery support in the advent of system failure. Vista ships with two resource managers that implement durable transactions - the Transactional File System (TxF) and Transactional Registry (TxR). Most developers will never actually write a transacted resource manager, but will instead deal with existing resource managers like the TxF. The TxF can operate against non-networked non-encrypted NTFS file systems, and allows multiple operations against a file to be conducted in a transactional manner to guarantee the file's consistency.

The handle-based nature of the Windows API makes the use of the TxF quite easy, a transaction is created either directly through the KTM APIs or as a distributed transaction through IKernelTransaction, and this transaction handle is passed through to CreateFileTransacted, which is a transaction-aware equivalent of CreateFile. The file handle returned by CreateFileTransacted can then be passed through to any normal file API such as WriteFile and ReadFile, minimizing the rework required to retrofit transactions to an application. For file-based APIs that do not take a file handle, a transacted function that takes a transaction handle rather than a file handle has been added - MoveFileTransacted and SetFileAttributesTransacted are good examples of this.

The Transactional Registry (TxR) API works in a very similar manner to the TxF, with RegCreateKeyTransacted and RegOpenKeyTransacted returning a registry key handle that is transaction aware and can be used in standard registry functions like RegCreateKey.

By providing transactions at an operating system level and allowing these transactions to be enlisted with the DTC and take part in transactions that span other resource managers like databases, the KTM is a huge advance in providing the APIs for developers to create robust and reliable applications. Only minimal changes are required to introduce the KTM, the TxF and the TxR in an application, with transaction information stored in Windows handles and passed into existing APIs.



Related Articles

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: April 22, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Database professionals — whether developers or DBAs — can often save valuable time by learning to get the most from their new or existing productivity tools. Whether you're responsible for managing database projects, performing database health checks and reporting, analyzing code, or measuring software engineering metrics, it's likely you're not taking advantage of some of the lesser-known features of Toad from Dell. Attend this live …

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds