How to control the transaction between two different databases in Delphi?

Asked

Viewed 659 times

1

I have two databases and need to ensure the persistence of the data in both in a given process.

Connection components are those of the Interbase Express package.

I did so:

database := DataModule.IBDatabase;
databaseDois := TIBDatabase.Create(nil);
try
  try
    transaction := database.DefaultTransaction;
    transaction.StartTransaction;
    databaseDois.DefaultTransaction := transaction;
    databaseDois.LoginPrompt := false;
    databaseDois.Params.Text := database.Params.Text
    databaseDois.DatabaseName := 'databaseDois.gdb';
    query := TIBQuery.Create(nil);
    try
      query.Database := databaseDois;
      query.Transaction := transaction;
      // execução dos processo no segundo banco de dados.
    finally
      query.Free;
    end;
    // execução dos processos no database original
    transaction.Commit;
  except
    on e: exception do
    begin
      transaction.Rollback;
      raise e;
    end;
  end;
finally
  databaseDois.Free;
end;

What I did was create a second connection component and use the same transaction component for both. However, when trying to execute a query with the second connection an error is generated:

invalid transaction Handle (expecting Explicit transaction start)

It is possible to do this kind of control, as is, where I am missing?

Note that the transaction has already started, as shown below:

Transação Iniciada

1 answer

1

All that was left was to open the databaseDois for my example to work.

...
transaction := database.DefaultTransaction;
transaction.StartTransaction;
databaseDois.DefaultTransaction := transaction;
databaseDois.LoginPrompt := false;
databaseDois.Params.Text := database.Params.Text
databaseDois.DatabaseName := 'databaseDois.gdb';
databaseDois.Open; // falta apenas isso para funcionar
query := TIBQuery.Create(nil);
try
...
  • you can accept your answer (the V below the number of her votes) then it will no longer appear as unanswered.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.