Suporte a transações distribuídas

Aplica-se a:SQL ServerBase de Dados SQL do AzureInstância Gerida do Azure SQLAzure Synapse Analytics

Baixar driver OLE DB

Os consumidores do OLE DB Driver for SQL Server podem usar o método ITransactionJoin::JoinTransaction para participar numa transação distribuída coordenada pelo Coordenador de Transações Distribuídas da Microsoft (MS DTC).

O MS DTC expõe objetos COM que permitem aos clientes iniciar e participar em transações coordenadas através de múltiplas ligações para uma variedade de repositórios de dados. Para iniciar uma transação, o OLE DB Driver for SQL Driver for SQL Server para consumidores utiliza a interface MS DTC ITransactionDispenser. O membro BeginTransaction do ITransactionDispenser devolve uma referência num objeto de transação distribuído. Esta referência é passada para o OLE DB Driver for SQL Server usando o JoinTransaction.

O MS DTC suporta commit e aborto assíncronos em transações distribuídas. Para notificação sobre o estado da transação assíncrona, o consumidor implementa a interface ITransactionOutcomeEvents e liga a interface a um objeto de transação MS DTC.

Para transações distribuídas, o OLE DB Driver for SQL Driver for SQL Server implementa os parâmetros ITransactionJoin::JoinTransaction da seguinte forma.

Parâmetro Description
punkTransactionCoord Um apontador para um objeto de transação MS DTC.
IsoLevel Ignorado pelo OLE DB Driver for SQL Server. O nível de isolamento para transações coordenadas pelo MS DTC é determinado quando o consumidor adquire um objeto de transação do MS DTC.
IsoFlags Deve ser 0. O OLE DB Driver for SQL Server devolve XACT_E_NOISORETAIN se algum outro valor for especificado pelo consumidor.
POtherOptions Se não for NULL, o Driver OLE DB para SQL Server solicita o objeto options da interface. O OLE DB Driver para SQL Server devolve XACT_E_NOTIMEOUT se o membro ulTimeout do objeto options não for zero. O Driver OLE DB para SQL Server ignora o valor do membro szDescription .

Este exemplo coordena a transação usando o 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.