How to use nested transaction in Delphi dbExpress component with MySQL

ε祈祈猫儿з 提交于 2019-12-13 04:22:40

问题


I'm trying to do a nested transaction using dbExpress in Delphi XE3 that is connected to MySQL 5.6.13.

Here is my example code to do a Nested transaction:


...

  dbxTransaction := _connection.BeginTransaction(TDBXIsolations.ReadCommitted);


  // just to check if nested is suported
  supportsNestedBol := dbxTransaction.Connection.DatabaseMetaData.SupportsNestedTransactions;


  try
    _sqlQuery.Close;
    _sqlQuery.SQL.Clear;
    _sqlQuery.SQL.Add('INSERT INTO test(cod, name) VALUES(:cod, :name)');
    _sqlQuery.ParamByName('cod').AsInteger := Test.Cod;
    _sqlQuery.ParamByName('name').AsAnsiString := Test.Name;
    _sqlQuery.ExecSQL(False);


    //calls a nested function that has another transaction
    Employee.Save();


    _connection.CommitFreeAndNil(dbxTransaction);
    Result := True;
  except
    on Exc:Exception do
    begin
      _connection.RollBackFreeAndNil(dbxTransaction);
      raise Exc;
      Result := False;
    end;
  end;

...

function Employee.Save():Boolean;
begin
...
  dbxTransaction02 := _connection.BeginTransaction(TDBXIsolations.ReadCommitted);

  try
    _sqlQuery.Close;
    _sqlQuery.SQL.Clear;
    _sqlQuery.SQL.Add('INSERT INTO employee(cod, name) VALUES(:cod, :name)');
    _sqlQuery.ParamByName('cod').AsInteger := Employee.Cod;
    _sqlQuery.ParamByName('name').AsAnsiString := Employee.Name;
    _sqlQuery.ExecSQL(False);
    _connection.CommitFreeAndNil(dbxTransaction02);
    Result := True;
  except
    on Exc:Exception do
    begin
      _connection.RollBackFreeAndNil(dbxTransaction02);
      raise Exc;
      Result := False;
    end;
  end;
end;
...

If I put a break point and check the variable supportsNestedBol the value is False. So, I'm not sure if is the connector "libmysql.dll" that I'm using that not support nested transaction or if is the way that I'm trying to do it.

Some help?

来源:https://stackoverflow.com/questions/22543259/how-to-use-nested-transaction-in-delphi-dbexpress-component-with-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!