-
Lostall
This article does not introduce complete basic knowledge, please refer to relevant books
This article mainly provides an example of a simple framework of the COM program, and the screenshot of the program is as follows:
1, resource manager RM (Resource Manager)
RM is a software that can span COM transactions and manage persistent system status. RM knows that when the object participating in the transaction changes the resources it managed,
How to cache resources. Then, if the transaction is submitted, RM knows how to release these changes; if the transaction is put
Abandoned, RM knows how to discard these changes.
Most databases offer COM resource manager, including SQL Server, Oracle, DB2, and the like. In addition, special is Microsoft
The holiographic manager MSMQ also provides this function, which combines the message queue with the transaction, or through SetComplete
The method call in the message queue can be sent correctly to the server.
The final submission process of the transaction is done by the RM, and the operation after SetComplete, SETABORT. RM submitting methods by two stages
To complete this process. The first stage is the preparation phase, and the main job of RM is to save all relevant information to the log, the log is a key data.
Structure, different RMs have different implementation methods. If the RM returns successfully from the preparation phase, it means that the customer means that the submission has been successful.
Although the database has no real update, the rest of the work is the responsibility of the RM. The RM assumes all the responsibilities that make changes permanent.
For example, if the database completes the update of the database after successful completion, the RM will ask DTC when the next system is restarted.
Complete the rest of the work. The second phase is the submission phase, and the RM is responsible for completing the final database update operation.
A key interface to be implemented is ICRMCompensator, complete the transaction's submission process through its interface method, the calling process is as follows:
Transaction Submit: Preparation Stage: Beginprepare ()
PrepareRecord ()
Endprepare ()
Submitting Stage: BeginCommit ()
CommitRecord ()
EndCommit ()
Business stop: Beginabort ()
Abortrecord ()
Endabort ()
2, safety
To properly use COM , you must set the correct settings in Component Service, with the following steps:
(1) In the application's property / security setting / authorization, "to enforce the application to check the application"
(2) Attribute / security settings / security levels in the application, "Access to process level and component level"
(3) Select "Forced Components Class Access Check" in the properties / security settings / authorizations of components
(4) Add the appropriate role in the application
(5) The role that is allowed to access in the properties / security settings of the components
These five steps are unable to, and they must be (5). In addition to pay attention to the previous application service program
3, timely activation JIT (Just-in-Time Activation)
JIT features are not necessary to COM components. Transaction components must support JIT, non-transaction components can be used as JIT or not.
Timing of object activation (ACTIVATE):
(1) After calling COCREATEINSTANCE, activate the object when the component is called for the first time.
(2) When the object is in an invalid state, the object is activated when the object is called.
Object is invalid (deActivate) time:
(1) The object will be invalid after calling setcomplete or setabort. The object will be destroyed (if there is an object pool into the pool, if not
It is delete it), but the agent / stub, the RPC channel is unchanged.
(2) The object is invalid after the method call is completed, and its effect is equivalent to the effect of calling SetComplete and Setabort. (3) Calling Release will invalidate the object. The object will be destroyed (if there is an object pool into the pool, if it is not DELETE it),
Destroy the agent / stub and the RPC channel.
The role of JIT works: JIT's role is to save system resources. However, because the object is invalid, it is not released, and the channel is not released.
JIT really played with only more resources that consume more infrastructure than these COM .
4, object pool
The two mechanisms of the object pool and JIT activation are combined with COM to get the maximum speed. JIT guarantees that the object can be released in time, the object pool
Avoiding objects frequently created frequently.
The pool object must meet three conditions:
(1) IOBJECTCONTROL must be implemented
(2) It must be aggregated (otherwise it will be wrong when you create a component). This is because COM adds a packager outside the component, which may be Context Wrapper
(3) Highly recommended thread model is free or neutral, but it is not required. This is because the pool object may be used by customers of different thread models,
Use free or neutral to increase access speed and avoid scheduling between apartments. In addition, in Com , the components are Neutral apartments.
The role of the object pool: The role of the object pool is to save the time created by the component, so as long as you do some initialization work. but
Only when the initialization object is more than the time to create a new object, the object pool has truly role.
5, connection point problem
Traditional connection points are used in the examples given herein to implement component notifications for customers. But this approach has some problems in COM .
(1) In this example, the client first creates an object through CocreateInstance, and then invokes Advise to establish a connection, then call the component method.
In the three functions of the component's Activate, deActivate, and Canbepooled I attempt to return information to the client by connecting points. but
By running the program, it is found that the connection point does not work when the object is created and the object is destroyed, and the connection point works fine at other times. Original
Because, when calling Advise, COM calls Activate first, notifying the component that it has been activated, then call other related functions
To establish a connection, it is impossible to return information by connecting in Activate. The same reason, when the component is destroyed, COM first removes
Connect, then call deActivate and canbepooled, so you cannot return information by connection.
(2) The object is stateful, it contains information of connection. If this connection information is deleted in Deactivate, it means that the client program is
Supreme is to re-Advise after being invalid to establish a connection, sometimes it is very inconvenient. But if there is anything in DEACTIVATE
In this case, there will be interesting situation in this example:
First, each time you live again, reactivate, the connection still exists, apparently because the object still retains the original state.
Second, if multiple clients are running, the results in one program will be displayed in other programs at the same time. This is because this example
Setting up each function will be invalid, so the object is always placed in the pool, that is, there will only be one program calling component (because it is
On a machine, it will not be executed concurrently), so the same object is operated, and the connection point is connected to multiple customers.
There is a new way to implement events in COM , both publishers and subscribers. But this method has some limitations, can replace the traditional connection point
I will explain this question in further learning practice.