I’m hoping someone can provoke some ideas or provide references to material for creating a “transaction coordinator” implementation in PHP 5. The driver is based on a multi-vendor single business process integration. Due to potential failure points I’d like to avoid spaghetti code with tons of if’s, switches, try..catches and have a coordinator that can intercept method calls, track responses to subsequent calls and interrogate a methods attribute to determine what compensation function to use to reverse the transaction. The intent is to create an “ACID” like processing. The closing thing I can reference would be the TransactionScope object in .Net.
A sample scenario:
ServiceA
ServiceB
ServiceC
ServiceA->DoSomething() - passes
ServiceB->DoSomething() - passes
ServiceC->DoSomething() - fails
at this point A & B must employ some type of compensation strategy to call functionality that will return state be it a database, third party integration or otherwise back to its original state.
My initial thoughts are to use some type of AOP to hold a reference to the compensator and the state requirements to complete the reversal activity. Than a coordinator would need to keep track of executions, as well as returned state of executions to allow for the call to the compensator.
Dont try ACID, because you cannot do it. Trying to fake it will at some point leak, so better not try in the first place. As you wrote yourself, using Service A to do one step, later seeing that Service C failed, puts you in need of telling Service A to undo the one step taken. If there is no way to undo the single step, the workaround might be to restart all over with Service A, doing a reset and all previously done steps without the last one inside your transaction. This does not sound ACID at all. Pretending it is will fail at some point.
As you are modeling a process, use a finite state machine for it. It is independent from what your services can or cannot do, but you get the ability to track which steps need to be taken, probably in which order, what to do if a step fails, and it is relatively easy to extend at any point.
Regarding your comment that you might end up in a mess of if, switch and try/catch: That is entirely up to you, but has nothing to do with implementing a process.
If you are looking for an implementation of a state machine, there are not that many for PHP. PEAR has one very PHP4-style old one, which might still work and do the job (have a look: http://pear.php.net/package/FSM ), but the better implementation would be https://github.com/Metabor/Statemachine – this guy has written “state machine” all over, and I see some very good features there, for example handing over the code that does the work via callbacks to the transitions, and then change transitions via events. This has the advantage that your state machine is in full control of what can and cannot be done, and if it can be done, it will be done via the callback.
A neat feature is that you can get hold of the defined states and transitions as an image.