Realize database transactions with Microsoft.Net (3)

zhaozj2021-02-16  59

Enterprise service

Enterprise-level services are the only transaction mechanism that is born two-stage. Before you start coding, it is important to understand the working mechanism of enterprise services. If you have used VB programming, and you are familiar with COM programming, you will understand the functions provided by enterprise services. Creating a COM program with VB and creating a business-level service with CLR is that you don't have to limit the single-tier suit (STA), before introducing enterprise-level services, you should go to clear the single-line suit (STA) And the difference between multi-threaded suites (MTA). Using enterprise services, your .NET assembly will reside in the COM application to get services such as DTC and object pools. This article focuses on COM transaction services, but will also mention other services.

To achieve enterprise-class service components, you must introduce System.EnterpriseService namespaces in your class file, each class needs to inherit ServicesDComponet, which needs to be public and need to provide a public default. The constructor, one you extend the ServicesDComponet class, and develop enterprise-level services only need to configure properties. You can also get COM metadata through CLR, so all COM applications can be set in the program, using Visual Studio.net, these properties can be defined in a file called AssemblyInfo.cs or defined In the .cs file, you must assemble the properties:

[assmbly: ApplicationName ("YourapplicationNameHere"]]]]

[assmbly: ApplicationActivation (ActivationOption.Library)]

[assmbly: assemblykeyKeyfile ("..//../ keyfilename.snk")]

l ApplicationName Set the name of the program displayed in the COM directory.

l ApplicationActivation defines whether it is made as library applications or service applications. It is often set as a service application in the development to monitor the activities of each component, and in the test and becoming the product to obtain the best performance.

l AssemblyKeyFile provides a key file to the compiler, used to assemble a strong name, you need to provide a relative path relative to the assembly path. Typically /bin/release/myassembly.dll.

Enterprise-level service programs running in the CLR require a strong name to compile your assembly, you need to create a key file. The created command line statement is: SN -K KeyFileName, which generates a keyfilename.snk file in the current directory, which must be referenced in the AssemblyKeyFile property.

Now we can set the property, we first increase the class properties, in order to create a transaction component, to add attributes before class definition [Transaction (TransactionOption.Required)] Every method in the class will run in a transaction In, the easiest way to control the submission or rollback of each method is to increase attribute [AutoComplete (True)] before the method, so if there is no abnormality when the method is executed, the default commit, if there is an abnormality, this method will roll back .

premise

l need strong names

Advantage

l Execute a distributed transaction

l Get COM services, such as object build and object pool

limit

l Sacrifice some performance L COM 1.0 requires the isolation level of each transaction to serializable

example:

AskEMBLYINFO.CS File

[Assembly: ApplicationName ("MyEnterpriseApplication")]]]]

[assmbly: ApplicationActivation (ActivationOption.server)]

[assembly: assemblykeyKeyfile ("..//// myentapp.snk")]]

Component.cs file

Using system;

Using system.enterprises;

Using system.reflection;

Using system.Runtime.InteropServices;

Using system.data;

Using system.data.sqlclient;

Using system.text;

Namespace PETSHOP.ComponentStx

{

// COM 1.5 (WIN XP) Supports setting Transaction isolation Level

// TO Use with Windows 2000, Remove Isolation Attribute

Transaction (TransactionOption.Required, ISOLATION =

TransactionissionLevel.Readcommitted]

Public Class Ordertx: ServicesDComponent

{

// Must Supply Default Construction

Public orderTX ()

{

}

// set automplete to allow auto commit or rollback

[AutoComplete (TRUE)]

Public Int PurchaseItem (int Customerid, Int itemid, int itemqty)

{

SqlConnection Con = NULL;

Int ORDERID = 0;

Try

{

Con = New SqlConnection ("Data Source = localhost; user

ID = SA; password =; initial catalog = trans_db; ");

C.Open ();

String Updatesqltext = "Update Inventory Set QtyInstock = QtyInstock -"

itemqty.toString () "where inventory.productId =

itemid.toString ();

Sqlcommand cmd = new sqlcommand (updatesqltext, con);

cmd.executenonquery ();

String insertsqltext = "Insert Into Orders VALUES"

Customerid.toString () ",

" itemid.tostring ()", " "

Itemqty.toString () ",

GetDate ()); select @@ identity ";

CMD.comMandtext = INSERTSQLTEXT; ORDERID = Convert.Toint32 (cmd.executescalar ());

cmd.dispose ();

}

Catch (Exception EX)

{

ORDERID = 0;

Throw (ex);

}

Finally

{

C. close ();

}

Return OrderId;

}

}

Test Results

We put different transaction versions of pet stores to test their performance in the laboratory.

A load generating tool is used to record the interaction of users and programs, and the user access to the pet store is:

1. Go to the login page

2. log in

3. Add four random products to the shopping cart

4. Remove the last product from your cart

5. Buy products in shopping carts

The test script uses the same account at all times, and the product is added to the shopping cart, which is a direct URL method, such as:

http://localhost/petshop/cart.aspx? Action = PurchaseItem & itemid = EST-123

Test random generating statements (ranges from EST-1 to EST-50000) to the server.

Load Generation Tool Simulation Different users log in to the system, each user assumes that there is no opportunity to add an additional system load.

Test configuration

Intermediate layer: 2 x 550 MHz Intel Pentium III

500 MB RAM

4GB SCSI Hard Drive

Windows 2000 Advanced Server (SP2)

.NET Framework SDK RTM

Data layer: 4 x 550 MHz Intel Pentium III

3 GB RAM

8 GB SCSI Hard Drive (OS)

Disk array (Database Files)

Windows 2000 Advanced Server (SP2)

Data layer (DTC): 4 x 550 MHz Intel Pentium III

3 GB RAM

8 GB SCSI Hard Drive (OS)

Disk array (Database Files)

Windows 2000 Advanced Server (SP2)

Figure 8: Transaction performance test results

Summary and conclusion

This article discusses some .NET platforms for enterprise-level development technology, such as distributed transactions, and combined with the pet store system for some performance supplements.

To know that several transaction mechanisms have several transaction mechanisms to be used well, all of this does not decrease in performance and scalable.

转载请注明原文地址:https://www.9cbs.com/read-25347.html

New Post(0)