步骤 1:创建事务组件

目标

在本步骤中,你将了解以下内容:

  • 如何在 Microsoft Visual Basic 中编写事务组件
  • COM+ 服务的默认设置是什么
  • 如何配置 COM+ 服务

说明

UpdateAuthorAddress 组件(要在本部分中创建的组件)会更新 Pubs 数据库中现有作者的地址。 Pubs 数据库是 Microsoft SQL Server 附带的示例数据库。 它包含发布信息,如作者名称、地址和书名。

注意

Pubs 是在整篇入门中使用的数据存储区。

 

由于 UpdateAuthorAddress 会更新数据存储区,因此建议在事务中包含工作,如下图所示,因此当客户端调用组件时,COM+ 会自动启动事务,并在该事务中登记数据库(资源管理器)。 (有关 COM+ 中的事务的详细信息,请参阅 COM+ 事务。)

Diagram that shows a COM+ transaction with UpdateAuthorAddress.

若要将 UpdateAuthorAddress 设为事务组件,需要执行以下步骤:

  1. 必须编写组件。 为了获得额外的保护,会添加子例程,验证 COM+ 是否在事务中创建了对象。 此外,组件中包含基本错误处理,以简化错误恢复。 事务验证和错误处理增强了组件的可靠性。 (有关 UpdateAuthorAddress 组件的完整列表,请参阅步骤 1 示例代码。)

  2. 将组件添加到 COM+ 应用程序并安装应用程序后,必须事务属性必须设置为 Required,这可以保证 COM+ 在事务中创建每个 UpdateAuthorAddress 对象。 有关如何为组件设置事务属性的说明,请参阅设置事务属性

    注意

    在组件上设置事务属性时,将定义 COM+ 如何创建与事务相关的每个对象。 事务属性值包括 IgnoredNot SupportedSupportedRequiredRequires NewRequired 值不是组件的默认属性值之一。

     

COM+ 会使用实时 (JIT) 激活和并发绑定事务服务。 将组件声明为事务组件时,COM+ 还会强制实施 JIT 激活和并发保护(同步)。

示例代码

UpdateAuthorAddress 组件将打开与 Pubs 数据库的连接,从而允许用户修改作者的名称、地址或合同状态。 它还会调用第二个组件,这将在步骤 2:跨多个组件扩展事务中讨论。

若要在 Microsoft Visual Basic 项目中使用以下代码,请打开新的 ActiveX.dll 项目,并添加对 Microsoft ActiveX 数据对象库和 COM+ 服务类型库的引用。

注意

本入门中的示例代码是为了说明目的,对于实际暂存和生产来说可能不是最高效的代码。

 

Option Explicit
'
'  Purpose:   This class is used for updating an author's address.
'
'  Notes:     IMPT:  This component implicitly assumes that it will 
'             always run in a transaction. Undefined results may 
'             otherwise occur.
'

'----------------------------------------------------------
'  VerifyInTxn subroutine
'      Verifies that this component is in a transaction.
'      Throws an error if it is not.
'
Private Sub VerifyInTxn()
  If Not GetObjectContext.IsInTransaction Then
    ' Transactions turned off. 
    Err.Raise 99999, "This component", "I need a transaction!"
  End If
  ' Component is in a transaction.
End Sub

'----------------------------------------------------------
'  UpdateAuthorAddress subroutine
'      Procedure to update an author's address.
'
Public Sub UpdateAuthorAddress( _
                        ByVal strAuthorID As String, _
                        ByVal strPhone As String, _
                       ByVal strAddress As String, _
                        ByVal strCity As String, _
                        ByVal strState As String, _
                        ByVal strZip As String)
  ' Handle any errors.
  On Error GoTo UnexpectedError
  
  ' Verify that component is in a transaction.
  VerifyInTxn
  
  ' Get object context.
  Dim objcontext As COMSVCSLib.ObjectContext
  Set objcontext = GetObjectContext
  
  ' Get the IContextState object.
  Dim contextstate As COMSVCSLib.IContextState
  Set contextstate = objcontext
  
  ' Validate the new address information.
  ' The ValidateAuthorAddress function is described in Step 2.
  Dim oValidateAuthAddr As Object
  Dim bValidAddr As Boolean
  Set oValidateAuthAddr = _
    CreateObject("ComplusPrimer.ValidateAuthorAddress") 
  bValidAddr = oValidateAuthAddr.ValidateAuthorAddress( _
    strAddress, strCity, strState, strZip)
  If Not bValidAddr Then
    Err.Raise 99999, "The UpdateAuthorAddress component", _
      "The address of the author is incorrect!"
  End If
  
  ' Open the connection to the database.
  Dim conn As ADODB.Connection
  Set conn = CreateObject("ADODB.Connection")

  ' Specify the OLE DB provider.
  conn.Provider = "SQLOLEDB"

  ' Connect using Windows Authentication.
  Dim strProv As String
  strProv = "Server=MyDBServer;Database=pubs;Trusted_Connection=yes"

  ' Open the database.
  conn.Open strProv

  ' Execute the query.
  conn.Execute "update authors set phone= '" & strPhone & "'" & _
               " set address= '" & strAddress & "'" & _
               " set city= '" & strCity & "'" & _
               " set state= '" & strState & "'" & _
               " set zip= '" & strZip & "'" & _
               " where au_id = '" & strAuthorID & "'"
               
  ' Close the connection.
  conn.Close
  
  ' Get rid of the connection.
  Set conn = Nothing
                 
  ' Everything works--commit the transaction.
  contextstate.SetMyTransactionVote TxCommit
  contextstate.SetDeactivateOnReturn True
  Exit Sub
  
UnexpectedError:
  ' There's an error.
  contextstate.SetMyTransactionVote TxAbort
  contextstate.SetDeactivateOnReturn True
End Sub

总结

  • COM+ 分配默认属性值。 可以重新配置大多数服务属性。
  • 将组件的事务属性设置为 Required 时,可保证 COM+ 必须在事务中创建该组件的每个实例,但不一定启动新事务。
  • 通过验证事务是否存在,可确认组件的事务属性值不会被无意重置为非事务值,例如 IgnoreNot Supported
  • 处理错误使组件更可靠且更易于故障排除。
  • COM+ 为事务组件强制实施了 JIT 激活并发保护服务。
  • 若在完成后管理数据库连接,则将允许同一事务中的另一个组件重复使用连接池中的连接。 (不能将连接池与对象池混淆。)

步骤 2:跨多个组件扩展事务

步骤 3:重用组件

COM+ 实时激活

COM+ 同步

配置事务

创建 COM+ 应用程序

设置事务属性