Use statemachine with transaction

Not tested yet.