Transaction Issues

When you get to the point of utilizing transactions, you must make sure that an action or a series of actions are completed successfully, or you want to make sure that everything gets rolled back if there is a problem during the process. To get started, you’ll look at some of the basics of transactions before you look at how to debug them.

Transaction Models

The .NET Framework uses two basic transaction models:

  • Automatic —Automatically aborts or completes the transaction

  • Manual —Requires you to call SetComplete or SetAbort

The amount of control that you want over the processing of your transaction determines which method you use. In most cases, the automatic model should suffice. For a managed object to participate in an automatic transaction, the managed class must be registered with Windows 2000 Component Services. However, not all transactions are automatic.

Automatic transaction processing is a service provided by COM+ that enables you to configure a class at design time to participate in a transaction at runtime. To use this service, the class must derive directly or indirectly from the System.EnterpriseServices.ServicedComponent class.

Imagine that you are having problems developing a component to implement transactions. Then take a look at a simple example of a class that implements automatic transaction (see Listings 15.5 and 15.6).

Listing 15.5. AutoComplete Transaction (C#)
namespace TradeComponent 
{
      [Transaction(TransactionOption.Required)] 
      public class StockTrade : ServicedComponent 
      {
            [AutoComplete] 
            public bool Buy(int NumShares, string symbol) 
            {
                   try 
                   {
                        //Some code that buys x amount of shares 
                        //The transaction will automatically SetComplete() if the method
 call returns normally. 
                   } 
                   catch(Exception ex) 
                   {
                        //If the method call throws an exception, the transaction will be
 aborted 
                   } 
            } 
      [AutoComplete] 
           public bool Sell(int NumShares, string symbol) 
           {
                     try 
                     {
                            //Code that Sells x amount of shares 
                            //The transaction will automatically SetComplete() if the
 method call returns normally. 
                     } 
                     catch(Exception ex) 
                     {
                           //If the method call throws an exception, the transaction will
 be aborted 
                     } 

       } 
} 

Listing 15.6. AutoComplete Transaction (Visual Basic .NET)
Imports System.EnterpriseServices 

Namespace TradeComponent 

     <Transaction(TransactionOption.Required)> 
    Public Class StockTrade 
        Inherits ServicedComponent 


Public Function <AutoComplete()> Buy(ByVal NumShares As Integer, ByVal symbol As String)
 As Boolean 

                'Some code that buys x amount of shares 
                'The transaction will automatically SetComplete() if the method call
 returns normally. 


                'If the method call throws an exception, the transaction will be aborted 
       End Function 


       Public Function <AutoComplete()> Sell(ByVal NumShares As Integer, ByVal symbol As
 String) As Boolean 
                 'Code that Sells x amount of shares 
                 'The transaction will automatically SetComplete() if the method call
 returns normally. 

                 'If the method call throws an exception, the transaction will be aborted 

        End Function 
    End Class 
End Namespace 

In each of the methods in the class, the AutoComplete attribute has been assigned. This ensures that the management of the transaction will be handled by the system based on whether the function executes successfully.

Also, if you look closely, you will notice that the StockTrade class derives from the ServicedComponent class. Deriving the class from ServicedComponent ensures that the contexts of StockTrade objects are hosted in COM+. Two critical attributes must be applied for this example to work:

  • TransactionAttribute —Applied to the StockTrade class to set the transaction to Required, which is equivalent to using the COM+ Explorer to set the transaction support on a COM+ component.

  • AutoCompleteAttribute —Applied to the Buy and Sell methods. This attribute instructs the runtime to automatically call the SetAbort function on the transaction if an unhandled exception is generated during the execution of the method; otherwise, the runtime calls the SetComplete function.

Various assembly-level attributes are also used to supply COM+ registration information. A serviced component must be strong-named and should be placed in the global assembly cache (GAC) for manual registration.

Assemblies

Assemblies cannot be placed in the global assembly cache and should use dynamic registration.


Now that you have looked at the different attributes of a transaction, let’s take a look at some known issues with transactions.

Known Issues with Microsoft Transaction Server Components

Microsoft has made some changes in the ASP.NET security model. If you have existing Microsoft Transaction Server components that you plan to use with ASP.NET applications, you might need to change the security access permissions.

One of the common exceptions seen when calling an MTS component without the necessary security permissions is,“Permission denied.” If you are running into this, you might want to try the following steps to resolve the problem:

  1. In Component Services, bring up the Properties dialog box for the MTS application.

  2. Select the Identity tab, and change the account under which the component runs to that of a new local machine account, created solely for this purpose.

  3. Run the utility Dcomcnfg.exe.

  4. Select the Default Security tab.

  5. Under Default Access Permissions, click Edit Default and add the user created in Step 2.

  6. Restart IIS to ensure that the changes are recognized.

Going through all these steps should solve your permission problem.

Strong-Named Assemblies

Are you having problems getting your component to work? If so, have you implemented strong-named assemblies? You might have overlooked this requirement. Even though you can build and register your component as a COM object, you might not be able to use it.

To create a strong-named key, use this code:

 [C#] 
sn −k TradeComponent.snk 

After you have generated a key, you can then reference it in your code to create a strong-named assembly.Without a key, you will not be able to generate the strong-named assembly.

Listings 15.7 and 15.8 illustrate how to properly implement the attributes to make your component a strong-named component.

Listing 15.7. Strong-Named Component (C#)
// - Registration details -
// Supply the COM+ application name. 
[assembly: ApplicationName("TradeComponent")] 
// Supply a strong-named assembly. 
[assembly: AssemblyKeyFileAttribute("TradeComponent.snk")] 

namespace TradeComponent 
{
           [Transaction(TransactionOption.Required)] 
           public class StockTrade : ServicedComponent 
      {

             public bool Buy(int NumShares, string symbol) 
                    {
                    try 
                    {
                           //Some code that buys x amount of shares 
                           //The transaction will automatically SetComplete() if the
 method call returns normally. 
                    } 
                    catch(Exception ex) 
                    {
                           //If the method call throws an exception 
                           //you must call SetAbort so the transaction will be aborted 
ContextUtil.SetAbort(); 
                    } 
                    //Everything has completed successfully 
                    ContextUtil.SetComplete(); 

             } 
      [AutoComplete] 
             public bool Sell(int NumShares, string symbol) 
             {
                    try 
                    {
                          //Code that Sells x amount of shares 
                          //The transaction will automatically SetComplete() if the method
 call returns normally. 
                    } 
                    catch(Exception ex) 
                    {
                          //If the method call throws an exception, the transaction will
 be aborted 
                    } 

             } 
      } 
} 

Listing 15.8. Strong Named Component(Visual Basic .NET)
Imports System.EnterpriseServices 

Namespace TradeComponent 

    <Transaction(TransactionOption.Required)> Public Class StockTrade 
         Inherits ServicedComponent 


         Public Function Buy(ByVal NumShares As Integer, ByVal symbol As String) As Boolean 

             Try 

                 ' Some code that buys x amount of shares 
                 ' The transaction will automatically SetComplete() if the method call
 returns normally. 

             Catch ex As Exception 

                 ' If the method call throws an exception, the transaction will be aborted 

                     ContextUtil.SetAbort() 

             End Try 

ContextUtil.SetComplete() 


         End Function 


         Public Function Sell(ByVal NumShares As Integer, ByVal symbol As String) As Boolean 

            Try 

                 ' Code that Sells x amount of shares 
                 ' The transaction will automatically SetComplete() if the method call
 returns normally. 
             Catch ex As Exception 

                 'If the method call throws an exception, the transaction will be aborted 

             ContextUtil.SetAbort() 
            End Try 

       ContextUtil.SetComplete() 


        End Function 
    End Class 
End Namespace 

If you look at the method definitions in these listings, you will notice that the AutoComplete attribute is missing but that the Transaction attribute is still on the Class definition.

Now if you are trying to call the SetComplete or SetAbort methods on the transaction object, the examples above (Listings 15.7 and 15.8) shows how this can be accomplished.

After you have built your component, you probably will want to make sure that it is being executed as you intended it to. To accomplish this, you will need some sort of monitoring program to monitor transactions. The next section looks at how to monitor transactions.

Monitoring Your Transactions

When you start working with transactions , you will probably want to see how many are being processed by the system and see if there are any problems with any of them. Microsoft has provided a very simple, yet informative, tool for you to use to monitor transactions on the system. This can be found under the Distributed Transaction Coordinator. To access this tool, you will need to drill down into the Component Services console. The nice thing about the Distributed Transaction Coordinator is that, if you have permission on a remote server where transactional components are running, you can look at the statistics (see Figure 15.6).

Figure 15.6. Distributed Transaction Coordinator.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset