목표
이 단계에서는 다음을 알아봅니다.
- Microsoft Visual Basic에서 트랜잭션 구성 요소를 작성하는 방법
- COM+ 서비스의 기본 설정은 무엇인가?
- COM+ 서비스를 구성하는 방법
묘사
이 섹션에서 만들 구성 요소인 UpdateAuthorAddress 구성 요소는 Pubs 데이터베이스에 있는 기존 작성자의 주소를 업데이트합니다. Pubs 데이터베이스는 Microsoft SQL Server와 함께 제공되는 샘플 데이터베이스입니다. 여기에는 작성자 이름, 주소 및 책 제목과 같은 게시 정보가 포함됩니다.
메모
Pubs는 이 입문서 전체에서 사용되는 데이터 저장소입니다.
UpdateAuthorAddress는 데이터 저장소를 업데이트하므로 다음 그림과 같이 트랜잭션에 작업을 포함하는 것이 좋습니다. 따라서 클라이언트가 구성 요소를 호출할 때 COM+는 자동으로 트랜잭션을 시작하고 해당 트랜잭션에 데이터베이스(리소스 관리자)를 등록합니다. (COM+의 트랜잭션에 대한 자세한 내용은 COM+ 트랜잭션참조하세요.)
UpdateAuthorAddress를 사용하는 COM+ 트랜잭션을 보여 주는 
UpdateAuthorAddress를 트랜잭션 구성 요소로 만들려면 다음 단계가 필요합니다.
구성 요소를 작성해야 합니다. 추가 보호를 위해 COM+가 트랜잭션에서 개체를 생성했는지 확인하는 서브루틴이 추가됩니다. 또한 오류 복구를 간소화하기 위해 기본 오류 처리가 구성 요소에 포함됩니다. 트랜잭션 확인 및 오류 처리는 구성 요소의 안정성을 향상시킵니다. UpdateAuthorAddress 구성 요소의 전체 목록은 1단계 샘플 코드를 참조하세요.
COM+ 애플리케이션에 구성 요소를 추가하고 애플리케이션을 설치한 후에는 트랜잭션 특성을 필수설정해야 합니다. 그러면 COM+에서 트랜잭션에 각 UpdateAuthorAddress 개체를 만들 수 있습니다. 구성 요소에 대한 트랜잭션 특성을 설정하는 방법에 대한 지침은 트랜잭션 특성설정을 참조하세요.
메모
구성 요소에서 트랜잭션 특성을 설정하면 COM+에서 트랜잭션과 관련하여 각 개체를 만드는 방법이 정의됩니다. 트랜잭션 특성 값은 무시됨, 지원되지 않음, 지원됨, 필수및 새로 요구됨입니다. 필수 값은 구성 요소의 기본 특성 값 중 하나가 아닙니다.
COM+는 JIT(Just-In-Time) 활성화 및 동시성을 사용하여 트랜잭션 서비스를 바인딩합니다. 구성 요소를 트랜잭션으로 선언하면 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+는 기본 특성 값을 할당합니다. 대부분의 서비스 특성을 다시 구성할 수 있습니다.
- 구성 요소의 트랜잭션 특성을 필수 설정하면 COM+에서 해당 구성 요소의 각 인스턴스를 트랜잭션에 만들어야 하지만 반드시 새 트랜잭션을 시작할 필요는 없습니다.
- 트랜잭션이 있는지 확인하면 구성 요소의 트랜잭션 특성 값이 실수로 무시 또는 지원되지 않는같은 비 트랜잭션 값으로 다시 설정되지 않았는지 확인합니다.
- 오류를 처리하면 구성 요소를 보다 안정적이고 쉽게 해결할 수 있습니다.
- COM+는 트랜잭션 구성 요소에 대한 JIT 활성화 및 동시성 보호 서비스를 적용합니다.
- 데이터베이스 연결을 완료하면 동일한 트랜잭션의 다른 구성 요소가 연결 풀에서 연결을 다시 사용할 수 있습니다. (연결 풀링을 개체 풀링혼동해서는 안 됩니다.)
관련 항목
-
트랜잭션 특성 설정