Obsługa transakcji rozproszonych

SQL ServerZa pomocą macierzystego klienta OLE DB dostawca konsumentów ITransactionJoin::JoinTransaction metoda uczestniczyć w transakcja rozproszona koordynowane przez Microsoft transakcja rozproszona Coordinator (MS DTC).

Usługi MS DTC uwidacznia obiektów COM, które umożliwiają klientom do inicjowania i uczestniczenie w transakcjach koordynowane przez wiele połączeń w różnych magazynach danych.Aby zainicjować transakcję, SQL Server macierzystego klienta OLE DB dostawca konsumenta używa usługi MS DTC ITransactionDispenser interfejs.BeginTransaction element członkowski ITransactionDispenser zwraca odwołanie do obiektu transakcja rozproszona.To odwołanie jest przekazywana do SQL Server macierzystego klienta dostawca OLE DB przy użyciu JoinTransaction.

Usługi MS DTC obsługuje asynchroniczny zatwierdzanie i przerwanie zatwierdzanie rozproszonych.powiadomienie w stan asynchronicznego transakcji implementuje konsumenta ITransactionOutcomeEvents interfejs i łączy interfejsu obiekt transakcji MS DTC.

Dla transakcji rozproszonych SQL Server macierzystego klienta dostawca OLE DB implementuje ITransactionJoin::JoinTransaction parametrów w następujący sposób.

Parametr

Opis

punkTransactionCoord

Wskaźnik na obiekt transakcji MS DTC.

IsoLevel

Ignorowane przez SQL Server macierzystego klienta OLE DB dostawca.Poziom izolacji transakcji usługi MS DTC koordynowany jest określana, gdy konsument nabywa obiekt transakcji MS DTC.

IsoFlags

Musi być 0. SQL Server Macierzystego klienta dostawca OLE DB zwraca XACT_E_NOISORETAIN, jeśli żadna inna wartość jest określona przez konsumenta.

POtherOptions

Jeśli nie jest NULL, SQL Server macierzystego klienta dostawca OLE DB żąda obiektu opcje z interfejs. SQL Server XACT_E_NOTIMEOUT zwraca macierzystym klienta dostawca OLE DB, jeśli obiekt opcje ulTimeout element członkowski nie jest równa zero. SQL Server Macierzystego klienta dostawca OLE DB ignoruje wartość szDescription element członkowski.

W tym przykładzie koordynuje transakcji za pomocą usługi MS DTC.

// Interfaces used in the example.
IDBCreateSession*       pIDBCreateSession   = NULL;
ITransactionJoin*       pITransactionJoin   = NULL;
IDBCreateCommand*       pIDBCreateCommand   = NULL;
IRowset*                pIRowset            = NULL;

// Transaction dispenser and transaction from MS DTC.
ITransactionDispenser*  pITransactionDispenser = NULL;
ITransaction*           pITransaction       = NULL;

    HRESULT             hr;

// Get the command creation interface for the session.
if (FAILED(hr = pIDBCreateSession->CreateSession(NULL,
     IID_IDBCreateCommand, (IUnknown**) &pIDBCreateCommand)))
    {
    // Process error from session creation. Release any references and
    // return.
    }

// Get a transaction dispenser object from MS DTC and
// start a transaction.
if (FAILED(hr = DtcGetTransactionManager(NULL, NULL,
    IID_ITransactionDispenser, 0, 0, NULL,
    (void**) &pITransactionDispenser)))
    {
    // Process error message from MS DTC, release any references,
    // and then return.
    }
if (FAILED(hr = pITransactionDispenser->BeginTransaction(
    NULL, ISOLATIONLEVEL_READCOMMITTED, ISOFLAG_RETAIN_DONTCARE,
    NULL, &pITransaction)))
    {
    // Process error message from MS DTC, release any references,
    // and then return.
    }

// Join the transaction.
if (FAILED(pIDBCreateCommand->QueryInterface(IID_ITransactionJoin,
    (void**) &pITransactionJoin)))
    {
    // Process failure to get an interface, release any references, and
    // then return.
    }
if (FAILED(pITransactionJoin->JoinTransaction(
    (IUnknown*) pITransaction, 0, 0, NULL)))
    {
    // Process join failure, release any references, and then return.
    }

// Get data into a rowset, then update the data. Functions are not
// illustrated in this example.
if (FAILED(hr = ExecuteCommand(pIDBCreateCommand, &pIRowset)))
    {
    // Release any references and return.
    }

// If rowset data update fails, then terminate the transaction, else
// commit. The example doesn't retain the rowset.
if (FAILED(hr = UpdateDataInRowset(pIRowset, bDelayedUpdate)))
    {
    // Get error from update, then abort.
    pITransaction->Abort(NULL, FALSE, FALSE);
    }
else
    {
    if (FAILED(hr = pITransaction->Commit(FALSE, 0, 0)))
        {
        // Get error from failed commit.
        //
        // If a distributed commit fails, application logic could
        // analyze failure and retry. In this example, terminate. The 
        // consumer must resolve this somehow.
        pITransaction->Abort(NULL, FALSE, FALSE);
        }
    }

if (FAILED(hr))
    {
    // Update of data or commit failed. Release any references and
    // return.
    }

// Un-enlist from the distributed transaction by setting 
// the transaction object pointer to NULL.
if (FAILED(pITransactionJoin->JoinTransaction(
    (IUnknown*) NULL, 0, 0, NULL)))
    {
    // Process failure, and then return.
    }

// Release any references and continue.

Zobacz także

Koncepcje