Click here to Skip to main content
Licence 
First Posted 12 Aug 2005
Views 27,607
Bookmarked 9 times

.NET COM+ Transactions

By | 12 Aug 2005 | Article
Implementation of .NET COM+ Transactions

Introduction

      

                        .NET COM+ Transactions

 

Introduction:

 

Most of the components are used to access or retrieve data from the database. These components are best done using the transaction in order to get the advantages of the Transaction built in the .Net. As we have learned from the DBMS concepts that the advantages of Transaction is summarized as ACID. A-Atomicity. C-Consistency. I-Isolation. D-Durability. Now, we don’t have to worry about the ACID. .Net Takes care of it.

 

Let us see the steps used to create the .Net COM+ Transaction:

 

  1. Use the built in .Net Transaction objects.

 

  1. Use ApplicationName Atribute that will hold the assembly Components.

 

  1. Use ApplicationActivation.ActivationOption attribute specifies where assembly components are loaded on activation.

 

4.   Use the AssemblyKeyFile specifies the name of the strong key that       will be used to sign the assembly.

 

  1. Use Transaction.TransactionOption attribute to specify that the class requires the transaction or not.

 

  1. Write your code for the class. (The class name has to inherit from ServicedComponent Class.

 

  1. Use Serializable attribute to support serialization of the exception as it goes from COM+ to the client.

 

  1. Build the Component.

 

9. Access the Component from the Interface.

 

 

 

And now we are going to the real thing:

 

  1. Use the built in .Net Transaction objects.

 

using System;

using System.Data;

using System.Data.OleDb;

using System.Reflection;

using System.EnterpriseServices;  //This is must when you want to use component.

using System.Runtime.InteropServices; //This is for component interoperability.

using System.Runtime.Serialization;//This is for serialization.

 

  1. Use ApplicationName Atribute that will hold the assembly Components.

 

[assembly: ApplicationName("SRS")]

 

 

 

  1. Use ApplicationActivation.ActivationOption attribute specifies where assembly components are loaded on activation.

 

            [assembly: ApplicationActivation(ActivationOption.Library)]

 

 

Use the AssemblyKeyFile specifies the name of the strong key that will be used to sign the assembly.

 

(The .snk file was generated with sn.exe)

Note: use it in .net command prompt example in the same Com+ project folder:

C:\SRS>sn –k SRS.snk

 

#if BuildBat  // Used when building from the command line

                        [assembly: AssemblyKeyFile("SRS.snk")]

#else        // Used when building from within Visual Studio .NET

[assembly: AssemblyKeyFile("SRS.snk")]

#endif

 

           

            Here you actually have the namespace. When you create a project the .Net creates a namespace for you with the same name of your project. Example we create a SRS. (Student Registration System), we will have namespace like this.

 

namespace SRS

{

 

}

 

 

Note: Inside the namespace we write all our code which is in step no. 5 & 6

 

5.   Use Transaction.TransactionOption attribute to specify that the class requires the transaction or not.

 

[Transaction(TransactionOption.Required)]

 

 

6.   Write your code for the class. (The class name has to inherit from ServicedComponent Class.

 

 

public class Term : ServicedComponent

            {

// This method returns the List of Advising Terms in the Dataset

                        protected string ConnString;

                        protected OleDbConnection connection;

                        // First the SQLConnection needs to be setup.

                        public string ConnectionString                

                        {

                                    get

                                    {

                                                return ConnString;

                                    }

                                    set

                                    {

                                                ConnString = value;

                                    }

                        }

                        public OleDbConnection Connection                   

                        {

                                    get

                                    {

                                                if(ConnString==null)

                                                {

                                                            throw new ConnectionStringException();

                                                }

                                                if (connection== null)

                                                {

                                                            connection = new OleDbConnection(ConnString);

                                                }

                                                return connection;

                                    }

                        }

                        public void CloseDatabaseOnError()

                        {

                                    try

                                    {

                                                Connection.Close();

                                    }

                                    catch

                                    {

                                    }

                        }

public DataSet GetAdvisingTerms()

                        {

                                    DataSet termDS = new DataSet();

                                    try

                                    {                                  

                                                Connection.Open();

                                                OleDbCommand oledbCommand = new OleDbCommand();

                                                oledbCommand.Connection = Connection;

                                                oledbCommand.CommandType = CommandType.StoredProcedure;

                                                oledbCommand.CommandText = "Ssp_SRS_GetAdvisingTerms";

                                                OleDbDataAdapter termDA = new OleDbDataAdapter();

                                                termDA.SelectCommand = oledbCommand;

           

                                                termDA.Fill(termDS, "Ssp_SRS_GetAdvisingTerms");

                                                Connection.Close();

                                               

                                    }

                                    catch(Exception e)

                                    {

                                                CloseDatabaseOnError();

                                                throw new AdvisingTermNotReadException(e);

                                    }

                                    return termDS ;

                        }

 

 

}

 

Note: The Class has to be declared as public so that you can call it anywhere you want to use it.

 

7. Use Serializable attribute to support serialization of the exception as it goes from COM+ to the client.

 

// AdvisingTermNotReadException occurs when there is an error //while reading the

            // advising terms from the database.

            [Serializable]

            public class AdvisingTermNotReadException : Exception

            {

                        public AdvisingTermNotReadException(Exception ex) :

                                    base("Unable to get the advising term from the database. "+ex.Message , ex)

                        {

                        }

 

                        public AdvisingTermNotReadException(SerializationInfo info,

                                    StreamingContext context) : base(info, context)

                        {

                        }

            }

 

  1. Build the component.

Create a build.bat file and put it in the project folder.

The build.bat will contain the following:

 

csc /target:library %DEBUGSAMPLE% /d:BuildBat /out:SRS.dll /r:System.EnterpriseServices.dll  /r:System.dll /r:System.Data.dll *.cs

 

regsvcs SRS.dll

gacutil -i SRS.dll

 

@echo.

@echo To uninstall the Transactions Sample, run "build -u"

@echo.

 

@goto exit

 

:clean

gacutil /u SRS,PublicKeyToken=b7073b6138b34cac

regsvcs /u SRS.dll

 

:exit

 

Move to Programs > Microsoft Visual Studio .Net> Microsoft Studio .Net Tools > Visual Studio .NET Command Prompt.

 

                  Move to where your project is located, then,

use Build command to build the component. Example:

c:\SRS>build

 

 Now you are ready to use the component.

 

  1. Access the Component from the Interface. Example from a Button or Page_Load() function etc. This is usually another project accessing the component.

 

      Note: Do not forget to Reference the dll component.

      To Do this: right click references > browse > c:\SRS\SRS.dll

      The SRS.dll will loaded in your project.

      You also do the same for the System.EnterpriseServices

 

            Using(SRS.Term _Term = new SIS.Term())

{

            DataSet DSTerm = new DataSet();

            DSTerm = _Term.GetAdvisingTerms()

 

                        //To retrieve the data

                        int i;

                        i = DSTerm.Tables.Rows.Count;

                        while(i > 0)

                        {

Response.Write(DSTerm.Tables.Rows[i-1][“Term_Desc”].ToString());

                        }

}

 

 

Code used with this Article consists of 3 files:

 

   

1.   Term.cs

 

using System;

using System.Data;

using System.Data.OleDb;

using System.Reflection;

using System.EnterpriseServices;  //This is must when you want to use component.

using System.Runtime.InteropServices; //This is for component interoperability.

using System.Runtime.Serialization;//This is for serialization.

 

[assembly: ApplicationName("SRS")]

[assembly: ApplicationActivation(ActivationOption.Library)]

 

#if BuildBat  // Used when building from the command line

[assembly: AssemblyKeyFile("SRS.snk")]

#else        // Used when building from within Visual Studio .NET

[assembly: AssemblyKeyFile("SRS.snk")]

#endif

 

namespace SRS

{

 

[Transaction(TransactionOption.Required)]

 

public class Term : ServicedComponent

     {

// This method returns the List of Advising Terms in the Dataset

           protected string ConnString;

           protected OleDbConnection connection;

           // First the SQLConnection needs to be setup.

           public string ConnectionString      

           {

                get

                {

                     return ConnString;

                }

                set

                {

                     ConnString = value;

                }

           }

           public OleDbConnection Connection         

           {

                get

                {

                     if(ConnString==null)

                     {

                           throw new ConnectionStringException();

                     }

                     if (connection== null)

                     {

                           connection = new OleDbConnection(ConnString);

                     }

                     return connection;

                }

           }

           public void CloseDatabaseOnError()

           {

                try

                {

                     Connection.Close();

                }

                catch

                {

                }

           }

public DataSet GetAdvisingTerms()

           {

                DataSet termDS = new DataSet();

                try

                {              

                     Connection.Open();

                     OleDbCommand oledbCommand = new OleDbCommand();

                     oledbCommand.Connection = Connection;

                     oledbCommand.CommandType = CommandType.StoredProcedure;

                     oledbCommand.CommandText = "Ssp_SRS_GetAdvisingTerms";

                     OleDbDataAdapter termDA = new OleDbDataAdapter();

                     termDA.SelectCommand = oledbCommand;

    

                     termDA.Fill(termDS, "Ssp_SRS_GetAdvisingTerms");

                     Connection.Close();

                    

                }

                catch(Exception e)

                {

                     CloseDatabaseOnError();

                     throw new AdvisingTermNotReadException(e);

                }

                return termDS ;

           }

 

 

}

 

 

// AdvisingTermNotReadException occurs when there is an error //while reading the

     // advising terms from the database.

     [Serializable]

     public class AdvisingTermNotReadException : Exception

     {

           public AdvisingTermNotReadException(Exception ex) :

           base("Unable to get the advising term from the database. "+ex.Message , ex)

           {

           }

 

           public AdvisingTermNotReadException(SerializationInfo info,

                StreamingContext context) : base(info, context)

           {

           }

     }

}

 

 

 

2.               buid.bat

 

csc /target:library %DEBUGSAMPLE% /d:BuildBat /out:SRS.dll /r:System.EnterpriseServices.dll  /r:System.dll /r:System.Data.dll *.cs

 

regsvcs SRS.dll

gacutil -i SRS.dll

 

@echo.

@echo To uninstall the Transactions Sample, run "build -u"

@echo.

 

@goto exit

 

:clean

gacutil /u SRS,PublicKeyToken=b7073b6138b34cac

regsvcs /u SRS.dll

 

:exit

 

 

3. frmTerm.cs use the Component

 

 

private void Button_Click(

object sender, System.EventArgs e

)

{

     Using(SRS.Term _Term = new SIS.Term())

{

     DataSet DSTerm = new DataSet();

     DSTerm = _Term.GetAdvisingTerms()

 

           //To retrieve the data

           int i;

           i = DSTerm.Tables.Rows.Count;

           while(i > 0)

           {

Response.Write(DSTerm.Tables.Rows[i-1][“Term_Desc”].ToString());

           }

}

  }

 

Reference: Microsoft .Net Help

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

hadhmy

Web Developer

Oman Oman

Member

My name is Hamood Awadh Al-Hadhrami. I am working for the Modern College of Business and Science in Muscat, Oman. I have been working here about 2 and half years as Programmer/ Database administrator. I have worked in c# and sql server thoroghly. I love programming and database and reading books especially in my proffession and human development.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralDefault behaviour of transaction PinmemberThe Geek1:49 24 Aug '07  
GeneralBesides... Pinmemberleppie9:24 13 Aug '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 13 Aug 2005
Article Copyright 2005 by hadhmy
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid