Tài liệu Module 4: Managing Transactions and State - Pdf 90

Contents
Overview 1
COM+ Context 2
The IObjectContext Interface 6
Lab 4.1 Using Context Object Services 15
Just In Time Activation 18
Managing Transactions 25
Programming COM+ Transactions 38
Lab 4.2 Managing Transactions 48
Managing State 51
Using the Shared Property Manager 59
Lab 4.3 Storing State in the Middle Tier 69
Best Practices 74
Review 76

Module 4: Managing
Transactions and State Information in this document is subject to change without notice. The names of companies,
products, people, characters, and/or data mentioned herein are fictitious and are in no way intended
to represent any real individual, company, product, or event, unless otherwise noted. Complying
with all applicable copyright laws is the responsibility of the user. No part of this document may
be reproduced or transmitted in any form or by any means, electronic or mechanical, for any
purpose, without the express written permission of Microsoft Corporation. If, however, your only

efficiency.
After completing this module, students will be able to:
!
Use the context object to retrieve information about a COM+ component.
!
Add transaction support for COM+ components by using the Component
Services tool.
!
Enable JIT Activation for COM+ components.
!
Create COM+ components that support and manage distributed transactions.
!
Use the SPM to manage the application data referred to as state.
!
Describe some of the best practices when managing transactions and state.

In the first two labs, students will learn how to use the context object services to
retrieve information about the context object and to manage transactions. In the
third lab, students will use the Shared Property Manager to store state in the
middle tier of the Purchase Order Online application.
Materials and Preparation
This section provides you with the required materials and preparation tasks that
are needed to teach this module.
Required Materials
To teach this module, you need the following materials:
!
Microsoft PowerPoint
®
file 1907A_04.ppt
!

created with an associated context. For components that do not use COM+
services, the context is largely ignored. However, for components running
inside COM+ applications, contexts are the foundation on which COM+
services are provided. The context object information is exposed through
several interfaces on the ObjectContext object.
!
Just In Time Activation
Discuss how JIT Activation is used to create an object when a COM+
component is instantiated. JIT Activation helps make COM+ solutions
scalable by activating objects as they are required and by preventing idle
objects from consuming valuable resources on the server.
!
Managing Transactions
Discuss how transactions can be managed automatically by using COM+
services. Describe the purpose of transactions in an enterprise solution.
Transactions are required to maintain data integrity and to synchronize
updates to data in multiple data sources.
!
Programming COM+ Transactions
Explain that sometimes developers need to control transactions
programmatically even though they can be managed declaratively through
COM+ services. The main reason for programming COM+ transactions is to
gain a greater degree of control over the process. For example, a component
may enforce a business rule that causes a transaction to abort if certain
conditions arise, such as a balance transfer exceeding a specified maximum
amount. Discuss the context object interfaces and how they are used to
program COM+ transactions.
!
Managing State
Discuss that state refers to the data used within the business processes of an

ActivityID of the COM+ activity.

!
Run the test client
1. Run the NwindClient.exe client application from the <install
folder>\Democode\Mod04\NorthwindClient folder.
2. Enter a number below 100 and click Discontinue. A message will confirm
the discontinued product.
3. Enter a number over 200 and click Discontinue. An error message will be
displayed.

!
Examine the database
1. Start Enterprise Manager from the Microsoft SQL Server 7.0 program
group.
2. Expand the nodes in the Tree pane to view the tables in the Northwind
database.
3. View the contents of the EventLog table and note the name of the user
recorded there by the Traders component.
4. View the contents of the ErrorLog table and note the ActivityID recorded
there by the Traders component.

!
Examine the component code
1. Open Northwind.vbp in the <install
folder>\Democode\Mod04\NorthwindComponent folder.
2. Display the code for the Traders class and examine the
DiscontinueProduct method.
3. Under the comment Declare Object Context variable, add the following
variable declaration:

3. Enter a number over 200 and click Discontinue. An error message will be
displayed.

!
Examine the database
1. Start Enterprise Manager from the SQL Server 7.0 program group.
2. Expand the nodes in the Tree pane to view the tables in the Northwind
database.
3. View the contents of the EventLog table and note the name of the user
recorded there by the Traders component.
4. View the contents of the ErrorLog table and note the ActivityID recorded
there by the Traders component.

Managing Transactions
!
To prepare for the demonstration
!
Ensure the SetupDemos.cmd file has been executed as previously described.

!
Run the test client
1. Run the NwindClient.exe client application from the <install
folder>\Democode\Mod04\NorthwindClient folder.
2. Enter a number below 100 and click Discontinue. A message will confirm
the discontinued product.
3. Enter a number over 200 and click Discontinue. An error message will be
displayed.
Module 4: Managing Transactions and State vii
objCtx.SetAbort

5. Save and recompile the project and close Visual Basic.

!
Run the test client
1. Run the NwindClient.exe client application from the <install
folder>\Democode\Mod04\NorthwindClient folder.
2. Enter a number below 100 and click Discontinue. A message will confirm
the discontinued product.
3. Enter a number over 200 and click Discontinue. An error message will be
displayed.

!
Examine the database
1. Start Enterprise Manager from the SQL Server 7.0 program group.
2. Expand the nodes in the Tree pane to view the tables in the Northwind
database.
3. View the contents of the EventLog table and note that events have not been
recorded for product updates that did not take place. Explain that the reason
is that the business process is not an atomic transaction.

Module 4: Managing Transactions and State 1 #
##
#

Overview

!
Use the context object to retrieve information about a COM+ component.
!
Add transaction support for COM+ components by using the Component
Services tool.
!
Enable JIT Activation for COM+ components.
!
Create COM+ components that support and manage distributed transactions.
!
Use the Shared Property Manager to manage the application data referred to
as state.

2 Module 4: Managing Transactions and State #
##
#

COM+ Context
!
Overview of the Context Object
!
Interfaces for the Context Object
!
The IObjectContext Interface
!
The IContextInfo Inteface
!

Client
COM+ Component
ContextThe data that COM+ uses to keep track of what each object is doing is referred
to as an object's context. Each context must be associated with a particular
context object that monitors its properties. The context object, which is named
ObjectContext, maintains information about the object and its current status,
including:
!
Whether the object is involved in a transaction.
!
What the object is currently doing.
!
The security level of the component.
!
The identity of the object’s caller.
!
Security role membership of the object’s caller.
!
A Done flag to control the object’s activation and lifetime.
!
A Consistency flag to control transactional behavior.

Shared Context
When an object is instantiated, it is either associated with an existing context or
a new context is created for it. If a client program creates an instance of a
COM+ component, a new context will be created for the component. If the
COM+ component then instantiates another component with a compatible

Dim objSt As COMSVCSLib.IContextState
Set objCtx = COMSVCSLib.GetObjectContext()
Set objInfo = objCtx.ContextInfo
Set objSec = objCtx.Security
Set objSt = objCtx
ObjectContext
IObjectContext
IContextState
ContextInfo
Security
IUnknownThe context object provides a number of interfaces that can be used to retrieve
context information and control the object’s behavior.
You can retrieve most context information that you will need by using the
IObjectContext interface. This interface provides a wide range of methods and
properties that you can use to create an instance of a component, retrieve
security information, and retrieve and manage transactions. This interface can
be obtained programmatically by using the GetObjectContext function, which
is provided by COM+.
The IObjectContext interface contains two properties that are themselves
objects. These objects are the ContextInfo and Security objects and they
implement the IContextInfo and ISecurityProperty interfaces, respectively.
The IContextInfo interface provides methods and properties relating to the
status of the context, including the Context ID, the GUID identifying the
activity to which the object instance belongs and other useful logging
information.
The ISecurityProperty interface provides information about the caller’s
identity and is also useful for logging. You should note however that the

Methods of the IObjectContext Interface
Dim objCtx As COMSVCLib.ObjectContext
Set objCtx = GetObjectContext
If objCtx.IsInTransaction Then
‘Do Something
End IfThe IObjectContext interface provides the methods described in the following
table.
Method Description

CreateInstance Creates an instance of another COM+ component (method
required for backward compatibility with code written for
Microsoft Transaction Server only).
DisableCommit Prevents a transaction from committing temporarily.
EnableCommit Allows a transaction to commit.
IsCallerInRole Checks the role membership of the caller.
IsInTransaction Determines whether the object is in a transaction.
IsSecurityEnabled Determines if security checking is enabled for the
component.
SetAbort Declares that a transaction must be aborted.
SetComplete Declares a component as ready to commit the transaction.

The IObjectContext interface provides the properties described in the
following table.
Property Description

ContextInfo Provides an object with further methods and properties relating to
the context.

8 Module 4: Managing Transactions and State The IContextInfo Interface
!
Methods of the IContextInfo Interface
$
IsInTransaction
$
GetTransaction
$
GetTransactionID
$
GetActivityID
$
GetContextID
Dim objCtx As COMSVCSLib.ObjectContext
Dim strActID As String
Set objCtx = GetObjectContext
strActID = objCtx.ContextInfo.GetActivityIDThe IContextInfo interface contains status information about the context
object. The ContextInfo object is a property of the ObjectContext object that
implements the IContextInfo interface.
Methods of the IContextInfo Interface
The IContextInfo interface provides the following methods.
Method Description

IsInTransaction Determines whether the object is in a transaction. (The same

Set objCtxInfo = objCtx.ContextInfo
strActID = objCtxInfo.GetActivityID 10 Module 4: Managing Transactions and State The ISecurityProperty Interface
!
Methods of the ISecurityProperty Interface
$
GetDirectCallerName
$
GetDirectCreatorName
$
GetOriginalCallerName
$
GetOriginalCreatorName
Dim objCtx as COMSVCSLib.ObjectContext
Dim strUsrName as string
Set objCtx = GetObjectContext
strUserName = objCtx.Security.GetOriginalCallerNameYou can use the ISecurityProperty interface to retrieve information about the
caller of a component. The Security property of the ObjectContext is an object
that implements the ISecurityProperty interface.
Methods of the ISecurityProperty Interface
The ISecurityProperty interface provides the following methods.
Method Description

Set objCtx = GetObjectContext
Set objCtxScy = objCtx.Security
strUsrName = objCtxScy.GetOriginalCallerName You can also access security methods and properties through the
ISecurityCallContext interface. This interface provides greater functionality
than the ISecurityProperty interface and should usually be used to manage
programmatic security. For more information about the ISecurityCallContext
interface, see Module 8: Making Applications Secure.

Note
12 Module 4: Managing Transactions and State The IContextState Interface
!
Methods of the IContextState interface
$
SetDeactivateOnReturn
$
SetMyTransactionVote
$
GetDeactivateOnReturn
$
GetMyTransactionVote
!
Used to control activation and transactions
instantiated ObjectContext object, as shown in the following code.
Dim objCx As COMSVCSLib.ObjectContext
Dim objCxSt As COMSVCSLib.IContextState
Set objCx = GetObjectContext
Set objCxSt = objCx

14 Module 4: Managing Transactions and State Demonstration: The Context Object In this demonstration, you will be shown how to access the IObjectContext,
ISecurityProperty, and IContextInfo interfaces by using the ObjectContext
object.
Delivery Tip
The step-by-step
instructions for this
demonstration are in the
instructor notes for this
module.
Module 4: Managing Transactions and State 15 Lab 4.1 Using Context Object Services In this lab, you will use the services of the context object in the
POBusiness.bus_Order component. The context object provides information
about the context of the current object instance. You will use some of this
Exercise 1:
Using the Security Property
In this exercise, you will use the Security property of the context object to
identify the user who is calling the component.
!
Set a reference to the COM+ Services Type Library
1. Open the POBusiness.vbp project that you saved in Lab 3.2. If you did not
complete Lab 3.2, open the POBusiness.vbp project in <install
folder>\Labs\Lab04\POBusiness.
2. On the Project menu, click References.
3. Select the COM+ Services Type Library check box.
4. Click OK.

!
Declare a variable for the security property
In this procedure, you will display the code window for the bus_Order class
module, and in the Raise method, declare the following variables:
1. Declare a variable objCtx of type COMSVCSLib.ObjectContext.
2. Declare a variable objSec of type COMSVCSLib.SecurityProperty

!
Use the security object
In this procedure, you will replace the code strUser = “Student” with code to
do the following:
1. Instantiate objCtx by using the GetObjectContext function.
2. Instantiate objSec by assigning it to the Security property of objCtx.
3. Use the GetOriginalCallerName method of objSec to assign a value to
strUser.


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status