DB transaction handling

I have two different DB aliases for which I need to manage transaction. What i am currently doing have a main service from which i invoke the following steps

  1. start a transaction on the first DB say db1
  2. start a transaction on the first DB say db2
  3. Invoke a service A that inserts a record into a table of db1
  4. Invoke a service B that inserts a record into a table of db2, inside this service, on error rollback db2
  5. Commit on db1
  6. Commit on db2
    On error
  7. Rollbacl db1

This sequence works perfectly for db2 but doesnt not rollback for db1. I need to rollback db1 if there was error in service that inserts record into db2.

Any help is appreciated.

Make sure that inside the onerror sequence, you still have the value for the db1. This very much looks like an issue where the connections are mixed up.
Thanks and best of luck!

These are somethings that may work for you:

a) Try giving different names (instead of $dbConnection) for database connection objects to db1 and db2. Drop the $dbConnection as and when you are done mapping it to these new connection objects.

b) It looks like you should have an EXIT from FLOW and signal FAILURE in the error block in serviceB

c) Why dont you have the rollback-db1 immediately after the rollback-db2? Looks like you dont need to seperate these two rollbacks?

d) The database connection objects may not be there in the pipeline sometimes (after you do pub.flow:getLastError)when errors happen. You may have to retrieve it from the lastErrorInfo.pipeline.