Skip navigation

Running a COM Component in MTS

Follow these steps to create a component that leverages Microsoft Transaction Server (MTS)

Download the Code iconIn "Using COM Components with MTS," I demonstrated how to build a Vendor COM component. In this article, I’ll show you how to turn it into a component that runs in MTS.

You’ll find the files for this project in the 8677.zip file in the "Download the Code" box at the top of the page. Extract these files into a folder named RawCode. Extract the files from the Project1.zip file and place them into a folder called Project1. Then follow these steps to create the folder structure for the application and add the proper files:

  1. Create a new folder named DBCode for the database files.
  2. Move recordset.bas and password.bas from the RawCode folder into the DBCode folder.
  3. Create a new folder named MTSCode for special MTS-related files.
  4. Move MTSContext.bas into the MTSCode folder.
  5. Create a new folder named VendorMTS.
  6. Add the remaining files from RawCode to this folder.

Next, you need to modify the code in the Vendor component so it will work with MTS. First, add the recordset.bas and password.bas files. These files were part of the DBStuff project, which I demonstrated how to compile into a dll last month. This time, we’ll move these files into the project as code modules. Why do you want to use code modules instead of COM components? MTS can let a component be registered in one package on a given system, which creates a problem for general-purpose modules such as the DB components. If you want to use the DB functionality in another component that runs in a different package, you can execute the methods from a component in a different package but that procedure adds overhead to the process. Moving the functionality into a code module makes it easy for you to incorporate the code into any COM component project. The downside is that without proper controls, developers can modify the code.

Follow these steps to modify the Vendor project to work with MTS.

  1. Open Vendor.vbp.
  2. Add the recordset.bas and password.bas files to the project. Select Add Module from the Project menu, then click the Existing tab. Navigate to the DBCode folder and select the files.
  3. Add the MTSContext.bas module to the project. Select Add Module from the Project menu, then click the Existing tab. Navigate to the MTSCode folder and select the file.
  4. Add a reference to the Microsoft Transaction Server Type library. Select References from the Project menu and select Microsoft Transaction Server Type library from the list, as Figure 1 shows.
  5. Remove the project reference to DBStuff. Select References from the Project menu and deselect DBStuff. Change the compatibility setting to point to the DLL for this project. Open the project properties and click the Component tab. You’ll see the dialog box that Figure 2 shows. Enter the path to the DLL that you’re going to replace in the textbox at the bottom of the dialog box.

Next, you need to modify the code so that it runs by using the code modules (password.bas and recordset.bas) instead of using the DBStuff component. The steps also cover setting properties on the class and integrating the MTS methods into the application:

Follow these steps to modify the code:

  1. Remove the lines that reference DBStuff from each procedure. This code looks like this:
    Dim oDB As DBStuff.RecordsetStuff
  2. Change the lines that use RecordsetStuff methods. This code needs to call the methods as functions because the code from RecordsetStuff is now in a code module. You can delete the lines that instantiate an object from DBStuff. The lines that call the RunSQLWithRS method will look like:
    Set oRS = RunSQLWithRS(sSQL)
  3. Change the instancing property for the Publisher class to 2 – Requires Transaction.
  4. Change all lines that use CreateObject to use the ContextCreateObject function from the MTSContext class.
  5. Open the Publisher class.
  6. Add a call to the ContextSetComplete function just before the Exit Function statement. Repeat this for each method in Publisher.
  7. Add a call to the ContextSetAbortfunction just after the error handler label. Repeat this for each method in Publisher.
  8. Recompile the Vendor component, then select Make from the File menu.

Listing 1 shows the completed code for the Publishers class. Listing 2 shows the updated Recordset.bas code.

Next, follow these steps to create the MTS package and add the component to the package:

  1. Open the MTS Explorer in the Microsoft Management Console.
    MTS Explorer is in the NT Option Pack folder on the Program Files menu.
  2. Open the Computers folder.
  3. Open the Packages Installed folder under My Computer.
  4. Select New Package from the Action menu, or right-click on the Packages Installed folder and select New, Package. Either of these actions will start the Package Wizard.
  5. Select Create an Empty Package. You have two options for creating new packages. You can install an existing package (exported from another system), or you can create a new package and enter its attributes as you go.
  6. Enter a name for the new package Vendor.
  7. Select the user account this package will run in. This option lets you specify the user account the package will run under. You usually create a user account for each package that is executing on the system and assign relevant rights to that user. For now, just accept the default (interactive user) and click Finish.
  8. Open the properties of the new package by right-clicking on the package in the Packages Installed folder and selecting Properties
  9. Browse through the tabs and available options.
  10. Close the Properties window.
  11. Expand the Vendor package folder.
  12. Click the Components folder.
  13. Use NT Explorer to drag Vendor.dll into the Components folder.

Now, follow these steps to test the component:

  1. Create a new folder named Project1.
  2. Copy Form1.frm, Project1.vbp, and Project1.vbw to the Project1 folder.
  3. Switch back to VB.
  4. Select Add Project from the File menu.
  5. Click the Existing tab and select Project1 from the Project1 folder you just created.
  6. In Project Explorer, right-click Project1 and select Set as Startup.
  7. Change the reference to the Vendor component to point to your newly recompiled file. Select References from the Project menu and select Microsoft Transaction Server Type library from the list, as Figure 3 shows.

MTS and VB make a powerful team for building high-performance, manageable client/server or Web applications, and they offer significant benefits in object and connection management.

TAGS: SQL
Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish