How to control transaction between two distinct database in Delphi?

1

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

The 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 type of control, how and where am I going wrong?

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

    
asked by anonymous 27.02.2015 / 18:47

1 answer

1

I just needed to open 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
...
    
27.02.2015 / 21:47