int mts_trx_commit (lock_trx_t * lt, int is_commit) { tp_dtrx_t *dtrx = lt->lt_2pc._2pc_info; tp_data_t *tpd = lt->lt_client->cli_tp_data; if (tpd && (CONNECTION_ENLISTED == tpd->cli_tp_enlisted)) { MTS_TRACE (("mts_trx_commit (connection level) %x\n", lt->lt_client)); return 0; } if (dtrx->dtrx_info) { HRESULT hr; MTS_TRACE (("mts_trx_commit (transaction level) %x\n", lt)); ITransaction *trx = ((mts_t *) dtrx->dtrx_info)->mts_trx; hr = is_commit ? trx->Commit (FALSE, 0, 0) : trx->Abort (0, 0, 0); trx->Release (); if (SUCCEEDED (hr)) return LTE_OK; return LTE_DEADLOCK; } return 0; }
int _tmain(int argc, _TCHAR* argv[]) { // Starting up the resource manager // Note: In most of the cases, a "durable" resource manager lives in its own process // Here the resource manager is in the same process with the client for readability DTCResourceManager* resourceManager = NULL; try { resourceManager = new DTCResourceManager(); } catch( std::bad_alloc ) { resourceManager = NULL; } if( NULL == resourceManager ) { std::cout << "Failed to allocate memory. The program will exit." << std::endl; exit(1); } if(!resourceManager->Init()) { std::cout << "The resource manager failed to start. The program will exit." << std::endl; exit(1); } std::cout << "The client starts..." << std::endl; // client starts, creates a transaction, does some work on the resource manager and later calls commit ITransaction* pTransaction = NULL; if( !CreateDTCTransaction(&pTransaction) ) { std::cout << "Failed to create transaction. The program will exit." << std::endl; exit(1); // Replace with specific error handling } byte* txToken = NULL; ULONG txTokenSize = 0; if( !MarshalDTCTransaction(pTransaction, &txToken, &txTokenSize) ) { std::cout << "Failed to marshal the transaction. The program will exit." << std::endl; exit(1); // Replace with specific error handling } std::cout << "The client asks the resource manager to do work as part of the transaction" << std::endl; if( !resourceManager->OpenConnection(txToken, txTokenSize)) { std::cout << "The client failed to open the connection with the resource manager. The program will exit." << std::endl; exit(1); } resourceManager->DoWork(); resourceManager->CloseConnection(); std::cout << "The client commits the transaction" << std::endl; // Commit the transaction. HRESULT hr = S_OK; hr = pTransaction->Commit( FALSE, // [in] BOOL fRetaining, XACTTC_SYNC_PHASEONE, // [in] DWORD grfTC, 0 // [in] DWORD grfRM ); if (FAILED(hr)) { std::cout << "pTransaction->Commit() failed: Error # " << std::hex << hr << std::endl; exit(1); // Replace with specific error handling. } // Release the transaction object. pTransaction->Release(); pTransaction = NULL; delete[] txToken; txToken = NULL; std::cout << std::endl << "The client exits" << std::endl; // Since the resource manager is sharing its lifetime with the client process in this sample, // to avoid "failed to notify" transactions, we will ask for the user to keep this process alive // until the resource manager will complete the transaction std::cout << std::endl << "Press ENTER after you see the Resource Manager completing the transaction." << std::endl; std::cin.get(); delete resourceManager; resourceManager = NULL; return 0; }