关闭

調用MSDTC控制分散式Transaction

标签: integerfunctioninterfacesql serversessionwindows
1498人阅读 评论(0) 收藏 举报
分类:

MSDN上多方查找資料,找到了調用MSDTC處理Transaction的函數,經過測試后整理如下:

unit DTCTransaction;

 

interface

uses

  SysUtils,OleDB,COmObj,DCConst,Windows;

const

  CLSID_MSDASQL  :TGUID='{C8B522CB-5CF3-11CE-ADE5-00AA0044773D}'; //oledb for ODBC

  CLSID_SQLOLEDB :TGUID='{0C7FF16C-38E3-11D0-97AB-00C04FC2AD98}'; //oledb for sql server

 

  function  DTCTransactionStart:ITransaction;

  procedure DTCTransactionCommit(ATransaction:ITransaction);

  procedure DTCTransactionRollback(ATransaction:ITransaction);

 

implementation

 

  function DtcGetTransactionManager(

    hostName:PChar;

    tmName:PChar;

    iid:Pointer;//pointer of TGUID;

    dwReserved1:DWord;

    wReserved2:Word;

    pvReserved:Pointer;

    out txnDispenser:ITransactionDispenser):HResult;cdecl;external 'xolehlp.dll';

 

function DTCTransactionStart:ITransaction;

var

  ID:ITransactionDispenser;

begin

  Result:=nil;

  OLECheck(DtcGetTransactionManager(

    nil,

    nil,

    @IID_ITransactionDispenser,

    0,0,

    nil,

    ID));

 

  OLECheck(ID.BeginTransaction(nil,

    TransactionISOLATIONLEVEL,

    ISOFLAG_RETAIN_DONTCARE,nil,Result));

end;

 

procedure DTCTransactionCommit(ATransaction:ITransaction);

begin

  OLECheck(ATransaction.Commit(false,0,0));

end;

 

procedure DTCTransactionRollback(ATransaction:ITransaction);

var

  B:BOID;

begin

  OLECheck(ATransaction.Abort(@B,false,false));

end;

 

end.

ADO加入分散式Transaction的方法:

unit ADOTransaction;

 

interface

uses

  ADODB,ADOInt,OLEDB,ComObj,DCConst;

 

  procedure JoinSessionIntoTransaction(AConnectionArray:array of TADOConnection;

            ATransaction:ITransaction);

  procedure UnJoinSession(AConnectionArray:array of TADOConnection);

 

implementation

 

procedure JoinSessionIntoTransaction(AConnectionArray:array of TADOConnection;

  ATransaction:ITransaction);

var

  ICC:ADOConnectionConstruction;

  ISP:IInterface;

  ITJ:ITransactionJoin;

  i:integer;

begin

  for i:=Low(AConnectionArray) to High(AConnectionArray) do

  begin

    if not AConnectionArray[i].Connected then AConnectionArray[i].Open;

    OLECheck(AConnectionArray[i].ConnectionObject.QueryInterface(

      IID_ADOConnectionConstruction,ICC));

    ISP:=ICC.Get_Session;

    OLECheck(ISP.QueryInterface(IID_ITransactionJoin,ITJ));

    OLECheck(ITJ.JoinTransaction(ATransaction,TransactionISOLATIONLEVEL,0,nil));

  end;

end;

 

procedure UnJoinSession(AConnectionArray:array of TADOConnection);

var

  ICC:ADOConnectionConstruction;

  ISP:IInterface;

  ITJ:ITransactionJoin;

  i:integer;

begin

  for i:=Low(AConnectionArray) to High(AConnectionArray) do

  begin

    if AConnectionArray[i].Connected then

    begin

      OLECheck(AConnectionArray[i].ConnectionObject.QueryInterface(

        IID_ADOConnectionConstruction,ICC));

      ISP:=ICC.Get_Session;

      OLECheck(ISP.QueryInterface(IID_ITransactionJoin,ITJ));

      OLECheck(ITJ.JoinTransaction(nil,0,0,nil));

    end;

  end;

end;

 

end.

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:326001次
    • 积分:4878
    • 等级:
    • 排名:第5814名
    • 原创:148篇
    • 转载:46篇
    • 译文:0篇
    • 评论:76条
    最新评论