Using COM + to implement transaction control in C #

Source: Internet
Author: User
Tags implement requires
Control. NET technology is the next generation platform technology that Microsoft vigorously promotes, since. NET technology architecture, this technology is gradually becoming mature and stable. According to Microsoft's platform system share, it is not difficult to imagine that in the next year or two. NET technology is bound to mount the mainstream technology platform, and the most important prerequisite for the rapid development of a new technology platform is: He will not completely discard the previous technology, this is for. NET technology refers to the com/com+ technology.

Generally speaking, in the IT technology industry as well as the hardware industry, the speed of technology upgrading is very amazing, and the Convention is that all new technologies will follow the principle of backward compatibility, but. NET technology has not only done this,. NET even realizes the respective call between each other, this is very commendable. That is to say, not only can we be in. NET component, you can also invoke the. NET component normally in a COM component. The benefits of this point are obvious, on the one hand we can maintain the existing technical resources, on the other hand, in the existing resources can be used. NET brings with it all kinds of new technologies.

General database transaction control requires that the operations in the transaction must be in the same database, so that the error can be rolled back (Rllback) to the initial state. There is a problem, in the distributed application, we often need to operate multiple databases at the same time, using the transaction processing of the database itself, it is difficult to meet the requirements of the program for transaction control. In COM +, a complete transaction service is provided, which we can use to complete transaction control in a distributed application.

The specific process is as follows

One: Use Vs.net to generate a class library.

Second: Add the reference to the System.enterpristservices, the specific steps

Menu: (Item-Add reference-in. NET tab select system.enterpristservices-OK)

Three: Build class

1: Source program

Using System;
Using System.EnterpriseServices;
Using System.Data.SqlClient;
Using System.Reflection;

Namespace Complussamples
{
Indicates a need for transactional support
[Transaction (transactionoption.required)]
Declared as a server application, you can also select the library to represent the libraries application
[Assembly:applicationactivation (Activationoption.server)]
Descriptive information
[Assembly:description ("Sample")]

public class Txcfgclass:servicedcomponent
{
private static string init1 = "User id=sa;password=;initial catalog=pubs;data source= (local)";

private static string init2 = "User id=sa;password=;initial catalog=northwind;data source= (local)";

private static string add1 = "INSERT INTO authors (' au_lname ', ' au_fname ') VALUES (' test1 ', ' test2 ')";

private static string add2 = "INSERT into sample values (' Test1 ', 22)";
The error SQL statement
There is not table "sample"

Public Txcfgclass () {}

private void Execsql (String init, String sql)
{
SqlConnection conn = new SqlConnection (init);
SqlCommand cmd = conn. CreateCommand ();
Cmd.commandtext = SQL;
Conn. Open ();
Cmd. ExecuteNonQuery ();
Conn. Close ();
}

Add a record to the database
public void Add ()
{
Try
{
Insert a record in a database

Execsql (init1, ADD1);
Console.WriteLine ("The operation in the same database completely");

Insert two records in a different database
This time, a bad SQL statement was executed.

Execsql (Init2, ADD2);
Console.WriteLine ("The operation in the other database"
Completely ");

Console.WriteLine ("Record (s) added, press Enter ...");
Console.read ();

}
catch (Exception e)
{
Transaction rollback
ContextUtil.SetAbort ();
Console.WriteLine ("Because there are some errors, so Operation Transcation");
Console.WriteLine ("The error is" + e.message);
Console.WriteLine ("Abort successfully");
Console.read ();
}
}
}
}
2: Program Description:

Adds a namespace using System.EnterpriseServices because this program uses the ContextUtil class
[Transaction (transactionoption.required)] indicates that the DLL requires transaction support.

The Txcfgclass class of this program inherits from the ServicedComponent class, which does not affect the class, but adds two additional methods in the class that make it easier to share code.

The SQL Server database used by the program runs on the local computer, and init1 and init2 are two connection strings connected to the database, Init connects to the pubs database, Inin2 connects to the Northwind database, which is the sample database that is brought in from sql2000. Add1 and ADD2 are two SQL statements that add a single record to the table of two databases. Note: ADD2 is a bad statement because there is no sample table at all, which causes an exception at execution time. (This is exactly what we were hoping for)

When executing to the ADD2 statement, because it is wrong, it throws an exception and is executed in the error-handling statement.

ContextUtil.SetAbort (); The statement causes all database operations to be rolled back so that the records inserted by the ADD1 statement will not exist. (To achieve the desired goal)

IV: Add a strengthened name to the program (strong name)

1: Create a pair of keys

The tool used to create the key is a shared tool called Sn.exe. It is usually run by a command prompt that performs various tasks to generate and extract the key. We need to run Sn.exe in the following ways.

Sn–k key.snk

Where key.snk represents the name of the file that will hold the key. Its name can be arbitrary, but it is customary to have a. SNK suffix name.

2: Signature

The signature is usually done at compile time. When signing, the user can use the C # property to inform the compiler that the DLL should be signed with the correct key file. To do this the user needs to open the AssemblyInfo.cs file in the project and make changes.

[Assembly:assemblykeyfile (".. \\.. \\key.snk ")]

Note: key.snk files and project files are in the same folder

V: Compiling into DLLs (concrete steps)

Menu: (Build-build)

If everything works, the DLL file is generated

VI: Use Regsvcs.exe to register DLLs with COM + services

We need to run the Regsvcs.exe in the following ways

RegSvcs DLL file name

If everything works, Regsvcs.exe will enter the DLL into COM + services.

Now that we have generated and registered this class that can be used by other programs, let's write a console program to verify that the class is working correctly.

Seven: Build Client

1: New Console Application project

Menu (File-new-project)

Select the console application and select Add to the solution to determine

2: Add a reference to the System.EnterpriseServices, as in the second step above.

3: Add a reference to the class that you just did well.

Menu (item-Add reference-Browse), select the DLL you just generated, and determine

4: Enter the following program

Using System;
Using Complussamples;
Using System.EnterpriseServices;

public class Client
{
public static void Main ()
{
Txcfgclass cfg = new Txcfgclass ();
Cfg. ADD ();
}
}

5: Set the console program as a startup item, and then compile and run, and you will see the result.

As we hoped, the first record was not inserted into the database


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.