步驟 3:重複使用元件
目標
在此步驟中,您將瞭解下列各項:
- 可重複使用的元件。
- 如何規劃重複使用性。
描述
此 COM+ 服務入門的前兩個部分步驟 1:建立交易元件 和 步驟 2:跨多個元件 擴充交易會示範如何撰寫一個元件來呼叫第二個元件,以協助完成某些工作,更新 Microsoft SQL Server Pubs 資料庫中的作者資訊;所有工作都會受到單一交易的保護。 範例元件著重於更新作者數據和驗證作者位址的工作,以及 COM+ 提供的事務處理、 JIT 啟用和 並行保護。
此步驟示範如何重複使用步驟 1 和 2 中建立的元件,並查看這些元件的設計的意義。 如下圖所示,這表示建立新的元件 , AddNewAuthor
藉由呼叫 UpdateAuthorAddress
,將新的作者新增至資料庫。
除了重複使用現有的元件功能之外, AddNewAuthor
還呼叫另 ValidateAuthorName
一個名為 的新元件。 如上圖所示, ValidateAuthorName
為非交易式。 此元件的交易屬性值會保留在其預設設定 (不支援) 以從交易中排除其工作。 如步驟 3 範例程式代碼所示, ValidateAuthorName
對資料庫執行只讀查詢,而且這個次要工作的失敗不應該有中止交易的可能性。 不過,元件的異動屬性值 AddNewAuthor
會設定為 [必要]。
AddNewAuthor
、 UpdateAuthorAddress
和 ValidateAuthorAddress
元件都會在交易中投票。 在此交易中, AddNewAuthor
是根物件。 COM+ 一律會在交易中建立第一個物件做為根物件。
在此範例中,重複使用 UpdateAuthorAddress
元件很簡單—COM+ 會自動傳遞預期的服務。 不過,如果元件的交易屬性值 UpdateAuthorAddress
一開始設定 為 [需要新增 ],而不是 [必要],則結果會有所不同。 在介面上,這兩個設定看起來都類似;兩者都保證交易。 不過,需要 New 一律會啟動新的交易,而 Required 只有在物件的呼叫端為非交易時才會啟動新的交易。 您可以從中看出,要 UpdateAuthorAddress
謹慎且深思熟慮地進行設定是多麼重要。 否則,COM+ 可能會以不同的方式解譯服務要求,併產生兩個不相關的交易,如下圖所示,而不是一個。
注意
當您重複使用元件時,請確定服務已設定為支援您所需的結果。
範例指令碼
AddNewAuthor 元件會藉由允許物件保持作用中,以執行新作者的批次新增作業,直到客戶端釋放對對象的參考為止。
Option Explicit
'
' Purpose: This class is used for adding a new author.
'
' Notes: IMPT: This component implicitly assumes that it will
' always run in a transaction. Undefined results may
' otherwise occur.
'
' AddNewAuthor
'
Public Sub AddNewAuthor( _
ByVal strAuthorFirstName As String, _
ByVal strAuthorLastName As String, _
ByVal strPhone As String, _
ByVal strAddress As String, _
ByVal strCity As String, _
ByVal strState As String, _
ByVal strZip As String, _
ByVal boolContracted As Boolean)
' Handle any errors.
On Error GoTo UnexpectedError
' Verify component is in a transaction.
' The VerifyInTxn subroutine is described in Step 1.
VerifyInTxn
' Get our object context.
Dim objcontext As COMSVCSLib.ObjectContext
Set objcontext = GetObjectContext
' Get the IContextState object.
Dim contextstate As COMSVCSLib.IContextState
Set contextstate = objcontext
' Validate that the author is OK.
' The ValidateAuthorName function is described after this function.
Dim oValidateAuthName As Object
Dim bValidAuthor As Boolean
Set oValidateAuthName = CreateObject("ComplusPrimer.ValidateAuthorName")
bValidAuthor = oValidateAuthName.ValidateAuthorName( _
strAuthorFirstName, strAuthorLastName)
If Not bValidAuthor Then
Err.Raise 999999, "The AddNewAuthor component", _
"You tried to add an author on the banned list!"
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
' Tell the database to actually add the author; use empty strings
' for this part and rely on the UpdateAuthorAddress
' component to validate the address/phone/etc data.
' Default Contract flag is off.
Dim strUpdateString As String
strUpdateString = "insert into authors values(_
'789-65-1234'," & _
strAuthorLastName & ", " & _
strAuthorFirstName & ", " & _
"'(555) 555-5555', ', ', ', '98765', "
If boolContracted Then
strUpdateString = strUpdateString + "1)"
Else
strUpdateString = strUpdateString + "0)"
End If
conn.Execute strUpdateString
' Close the connection; this potentially allows
' another component in the same transaction to
' reuse the connection from the connection pool.
conn.Close
Set conn = Nothing
' Create the UpdateAuthorAddress component.
Dim oUpdateAuthAddr As Object
Set oUpdateAuthAddr = CreateObject("ComplusPrimer.UpdateAuthorAddress")
' The component throws an error if anything goes wrong.
oUpdateAuthAddr.UpdateAuthorAddress "", strPhone, _
strAddress, strCity, strState, strZip
Set oUpdateAuthAddr = Nothing
' Everything works--commit the transaction.
contextstate.SetMyTransactionVote TxCommit
' Design issue: Allow batch additions of new
' authors in one transaction, or should each new author be added
' in a single new transaction?
contextstate.SetDeactivateOnReturn False
Exit Sub
UnexpectedError:
' There's an error.
contextstate.SetMyTransactionVote TxAbort
contextstate.SetDeactivateOnReturn True
End Sub
元件會在 ValidateAuthorName
將名稱新增至資料庫之前 AddNewAuthor
,先驗證作者名稱。 如果發生非預期的情況,此元件會將錯誤擲回給其呼叫端。
Option Explicit
'
' Purpose: This class is used for validating authors before
' adding them to the database.
'
' Notes: This component doesn't need to be in a transaction because
' it is performing read-only queries on the database,
' especially since these queries are not overlapping with
' any updates of end-user data. If an unexpected error
' happens, let the error go back to the caller who
' needs to handle it.
'
Public Function ValidateAuthorName( _
ByVal strAuthorFirstName As String, _
ByVal strAuthorLastName As String _
) As Boolean
ValidateAuthorName = False
' 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
' Suppose another hypothetical table has been added to the Pubs
' database, one that contains a list of banned authors.
Dim rs As ADODB.Recordset
Set rs = conn.Execute("select * from banned_authors")
' Loop through the banned-author list looking for the specified
' author.
While Not rs.EOF
If rs.Fields("FirstName") = strAuthorFirstName And _
rs.Fields("LastName") = strAuthorLastName Then
' This is a banned author.
conn.Close
Set conn = Nothing
Set rs = Nothing
Exit Function
End If
rs.MoveNext
Wend
' None of the added authors found in the banned list.
ValidateAuthorName = True
conn.Close
Set conn = Nothing
Set rs = Nothing
End Function
摘要
- 有時候,您不希望元件在交易中投票。
- COM+ 不會在非交易元件上強制執行 JIT 啟用或並行保護。 您可以個別設定這些服務。
- 呼叫元件的元件的元件會影響 COM+ 如何解譯所呼叫元件的服務要求。
相關主題