begin cServer:='127.0.0.1:9000'; cDatabase:='test'; cUserId:='1'; cUserPwd:='1';
client := TSQLDBWinHTTPConnectionProperties.Create(cServer, cDatabase,cUserId,cUserPwd); if not client.MainConnection.Connected then client.MainConnection.Connect; client.MainConnection.StartTransaction; try client.ExecuteNoResult( 'INSERT INTO syslog(frmName,UserID,EventTim e,Notes) '+ ' VALUES(?,?,?,?)',['test','test',now,'test']); client.MainConnection.Commit; except on E: Exception do begin client.MainConnection.Rollback; end; end;
只要启动超过 1 个线程,数据库启动事务就会失败。提示是: if session_ID=0 then raise ESQLDBRemote.Create('Remote transaction expects authentication/session'); if connection.Properties.InheritsFrom(TSQLDBConnectionPropertiesThreadSafe) and (TSQLDBConnectionPropertiesThreadSafe(connection.Properties).ThreadingMode=tmThreadPool) then raise ESQLDBRemote.CreateUTF8('Remote transaction expects %.ThreadingMode<>tmThreadPool: '+ 'commit/execute/rollback should be in the same thread/connection',[connection.Properties]);
with hcProp.NewConnection do try Connect; StartTransaction; try with NewStatementPrepared('sql', true, true) do begin Bind(['test', 'test', Now, 'test']); ExecutePrepared; end; Commit; except on e: Exception do Rollback; end; finally Free; end;
----------------------------------------------
-
//打开HTTP服务器 fServer := TSQLDBServerHttpApi.Create(fProps, cDatabase, '9000', '1', '1',false,2,nil,tmMainConnection); //这里用tmMainConnection 是可以的.不会报错.但是一但改成:tmpThreadPool . 又提示: raise ESQLDBRemote.CreateUTF8('Remote transaction expects %.ThreadingMode<>tmThreadPool: '+ 'commit/execute/rollback should be in the same thread/connection',[connection.Properties]);