tornado
Keywords: COM transaction
In three or multi-layered architecture applications, Com 's transaction function is more useful.
The function of COM transaction is very powerful. COM uses Microsoft Distributed Transaction Coordinator (DTC) to run a transaction in a distributed environment as a transaction manager and transaction coordinator. The powerful function of COM transaction performance can be manipulated in multiple databases, processing message queues, etc., and processes these operations as an atomic operation. He is a transaction based on operating system level.
We use an example to explain the Com 's transaction.
first step:
First open SQLServer2000, build database TEST
The SQL script of the table is as follows
Create Table [DBO]. [TEST1] (
[A] [int] Not null,
[b] [int] NULL
) On [primary]
Go
Create Table [DBO]. [TEST2] (
[A] [INT] NOT NULL, primary key
[b] [int] NULL
) On [primary]
Go
The only difference between the two tables is that Test2 has a primary key, and Test1 is not
Initialization has no data.
Step 2:
Then open VB6, create a new ActiveX DLL project. The engineering name is modified to Testcomtrans, and the class name is modified to Class1
Modify the attribute of Class1 MTSTRANSACTIONMODE to 2-RequireTransAction, this step is important.
You can also use the default settings, set the transaction in the COM container.
Quote "Microsoft ActiveX Data Object 2.7 Library" object library and "COM Services Type Library"
code show as below:
Option expedition
Public Sub Add ()
ON Error Goto Errnumber:
DIM Objctx as ObjectContext
Set Objctx = getObjectContext ()
DIM
Conn
As adodb.connection
Set
Conn
= New adodb.connection
Conn.open "provider = SQLOLEDB.1; PERSIST security info = false; user ID = sa; initial catalog = test; data source = localhost"
'Handling data here
Conn.execute "INSERT INTO TEST1 (A, B) VALUES (1, 1)"
Conn.execute "Update Test1 Set B = B 1"
Conn.execute "INSERT INTO TEST2 (A, B) VALUES (1, 1)"
Conn.execute "Update Test2 Set B = B 1"
Objctx.setComplete 'transaction successfully submitted
IF
NOT
Conn
Is nothing then
IF conve.state = 1 THEN
Conn.close
END IF
Set
Conn
= Nothing
END IF
EXIT SUB
Errnumber:
IF
NOT
Conn
Is nothing then
IF conve.state = 1 THEN
Conn.close
END IF
Set
Conn
= Nothing
END IF
Objctx.setabort 'transaction failed rollback
Err.raise Err.Number, "The transaction failed, the reason:", Err.DescriptionEND SUB
Compiled into a TestComtrans.dll file, the system will automatically register.
Otherwise manually register RegSVR
32 f
: / Test / Testcomtrans.dll
Step 3: Deploy
If you find a management tool in the control panel, you can see the component service after you open.
Create a name of the COM application first, then create a new component, use the component installation wizard, use the installation new component. If the MTSTRANSACTIONMODE is set to 2-RequireTransAction in our component, it will automatically set the component's Transaction property to Required (required) when the component is placed in the COM manager. This default is only available. The setting will take effect.
If you do not set a transaction property in the component, you can also set in the COM manager at this time.
Step 3: Test
We open VB to create a standard EXE project and add a button.
Test with later binding
code show as below:
Option expedition
Private submmand1_click ()
DIM OBJ AS Object
ON Error Goto Errhander
Set obj = creteObject ("testcomtrans.class1)
Call Obj.add
Msgbox "successful transaction"
EXIT SUB
Errhander:
MsgBox Err.Source Err.Description, Vbokonly, Err.Number
End Sub
Then we run, when you click the button, you will prompt the transaction to succeed, look at the database, and a record in each table is 1 and 2. When the button is clicked, since there is a primary key in Test2, the repetitive data causes the primary key conflict, the transaction will roll, which is the data in the TEST1 table is also automatically rolled. This ensures the consistency of the data.
Step 4: Verify
We can take a look at the process of transaction, open component management, and select the transaction statistics node below the distributed transaction coordinator node. It can be seen that the submission of the transaction success and the amount of submission and the summary of the summary.
Common mistake
1. If this code is executed in the class module or the VB reference is executed after the registration.
Objctx.Setabort is incorrect: Real-time error 91, no object variable or with block variable is set.
The correct way:
This component must be registered into the component server, then the code can be executed.
Incidentally explain
Configuring type components and non-configuration components
If a component is installed into the COM component server, we call this component as a configuration component. Such components can make full use of COM services. When the component is installed, the system assigns a configuration file called COM Registration Database (RegDb) in a system catalog. The catalog is saved as the component and the COM application. Attribute settings.
Non-configuration components are not interrelated with COM properties. Non-configuration components are not installed in the COM application, and in a way compatible with COM, it can be considered as registration in Regsvr32. Non-configuration components cannot take advantage of COM services.
2. You must select or require newly built on the options for the components in the Component Manager, otherwise the transaction will not perform success.