Automatic Transactions and .NET Framework Classes

原创 2004年10月28日 15:02:00

Instances of a .NET Framework class can participate in an automatic transaction, as long as you prepare the class to do so. Each resource accessed by a class instance, or object, enlists in the transaction. For example, if an object uses ADO.NET to post money on an account in a database, the resource manager for the database determines whether the object should execute in a transaction. If so, it automatically enlists the database in the transaction.

Use the following process to prepare a class to participate in an automatic transaction:

  1. Apply the TransactionAttribute to your class.
  2. Derive your class from the ServicedComponent Class.
  3. Sign the assembly with a strong name.

    To sign the assembly using attributes, create a key pair using Sn.exe:

    sn -k TestApp.snk

    Add the AssemblyKeyFileAttribute or AssemblyKeyNameAttribute assembly attribute specifying the name of the file containing the key pair to sign the assembly with a strong name.

    [assembly: AssemblyKeyFileAttribute("TestApp.snk")]

    For details see Signing an Assembly with a Strong Name.

  4. Register the assembly that contains your class with the COM+ catalog.

    If the client calling instances of your class is managed by the common language runtime, the registration is performed for you. However, if you anticipate that an unmanaged caller might create and call instances of your class, use the .NET Services Installation Tool (Regsvcs.exe) to perform the registration manually.

The following example shows how to apply the TransactionAttribute attribute to a class derived from the ServicedComponent class.

public class SampleClass(): ServicedComponent
  //. . .

When applying the transaction attribute, you can use Transaction, transaction, TransactionAttribute, and transactionattribute interchangeably. For example, you can use either Transaction or transactionattribute to produce identical results.

The following table lists and describes each constructor variation.

Attribute value Description
Disabled Eliminates the control of automatic transactions on the object. An object with this attribute value applied can engage the Distributed Transaction Coordinator (DTC) directly for transactional support.

NotSupported Indicates that the object does not run within the scope of transactions. When a request is processed, its object context is created without a transaction, regardless of whether there is a transaction active.

Supported Indicates that the object runs in the context of an existing transaction, if one exists. If no transaction exists, the object runs without a transaction.



Indicates that the object requires a transaction. It runs in the scope of an existing transaction, if one exists. If no transaction exists, the object starts one.

RequiresNew Indicates that the object requires a transaction and a new transaction is started for each request.

Sample Class

The following code example demonstrates several elements of an automatic transaction. In this example, both the transactional class and the client that calls the class are managed by the runtime.

// -----------------------------------------------------------------
// TestApp.cs
// Generate a Strong name: 
//    sn -k TestApp.snk
// Compile the code:
//    csc /target:exe /r:System.EnterpriseServices.dll TestApp.cs
// Run TestApp:
//    start TestApp.exe
// -----------------------------------------------------------------
using System;
using System.Runtime.CompilerServices;
using System.EnterpriseServices;
using System.Reflection;

//Registration details.
//COM+ application name as it appears in the COM+ catalog.
[assembly: ApplicationName("TestApp")]
//Strong name for assembly.
[assembly: AssemblyKeyFileAttribute("TestApp.snk")]

public class Account : ServicedComponent
  //Provides SetComplete behavior in the absence of exceptions.
  public void Debit(int amount)
     // Do some database work. Any exception thrown here aborts the 
     // transaction; otherwise, transaction commits.

public class client
  public static int Main() 
    Account accountX = new Account();
    return 0;

Garbage Collection: Automatic Memory Management in the Microsoft .NET Framework

Garbage Collection: Automatic Memory Management in the Microsoft...
  • bright60
  • bright60
  • 2007年12月02日 13:29
  • 730


简介最近要用到C++语言,需要用到指针数组,当练习时,有点懵逼,于是写篇博客。指针数组指针数组就是该数组的元素都是指针,即它的值是一个地址,用于指向某处。int * p[5]; // p[i]值为地址...
  • wl1710582732
  • wl1710582732
  • 2017年12月16日 16:58
  • 323

org.hibernate.TransactionException: nested transactions not supported

public PlUser findByUsernameAndPassword(String username, String password) { Session sx = this.getH...
  • u010102162
  • u010102162
  • 2015年01月08日 15:14
  • 2056

.NET Framework详解之内存机制

.NET Framework详解之内存机制:在.NET Framework中,内存中的资源(即所有二进制信息的集合)分为"托管资源"和"非托管资源".托管资源必须接受.NET Framework的CL...
  • xiaoran1985
  • xiaoran1985
  • 2014年03月11日 11:22
  • 663

.Net Framework学习的10个建议

  • abigfrog
  • abigfrog
  • 2009年10月23日 14:40
  • 5316

net framework体系结构

术语解释 CIL(common intermediate language):公共中间语言。.net框架下各种种类、版本的编程语言在经过编译后生成的中间语言(后缀为.il),与平台无关、与语言无关,...
  • u011872945
  • u011872945
  • 2017年06月07日 11:21
  • 459

netframework 卸载工具

.netframework 下载页面 : 如果该工具不能完全卸载,到    控制面板>>程序>>卸载...
  • u013066730
  • u013066730
  • 2016年12月29日 12:31
  • 794

.NET Framework4.5 & .NET Framework4.0

楔子 IDE:VS2013,.NET Framework 描述:在已经引用命名空间System.Configuration的前提下,却无法使用该命名空间下的AppSettings函数。...
  • sky15732625340
  • sky15732625340
  • 2016年06月04日 11:49
  • 1188

[~ Tue, 26 July 2016] Deep Learning in arxiv

  • sunbaigui
  • sunbaigui
  • 2016年07月26日 15:30
  • 2197

Win10无法安装.NET Framework 3.5

最近又要学习SQL Sever了,于是又要给电脑安装SQL Sever。在安装过程中根据教程安装一步一步来到最后一步,结果出现一个错误,错误的原因就是没有安装.net framework 3.5 ...
  • n950814abc
  • n950814abc
  • 2017年05月04日 19:12
  • 14677
您举报文章:Automatic Transactions and .NET Framework Classes