samedi 5 septembre 2020

When to commit during business transaction which spans to multiple system transactions?

I am reading about Concurrency from Fowler's book.

While talking about Business Transaction which spans to multiple system transactions, he noted so:

Atomicity and durability are the ACID properties most easily supported for business transactions. Both are supported by running the commit phase of the business transaction, when the user hits Save, within a system transaction.

As I understood here, we have to commit only during last system transaction. Am I right? Otherwise, our database might be in inconsistent state. For example, let's say our BT consists of 3 STs and we commit after each system transaction. What will happen if we had error while commiting during 3rd system transaction as we can't rollback committed data during 1st and 2nd system transaction?

Seems I am wrong, as author shows some sample code while explaining Pessimistic Offline Lock pattern. All commands are decorated with TransactionalCommand which commits data at the end:

class TransactionalComamnd implements Command...
    public TransactionalCommand(Command impl) {
        this.impl = impl;
    }

    public void process() throws Exception {
        beginSystemTransaction();
        try {
            impl.process();
            commitSystemTransaction();
        } catch (Exception e) {
            rollbackSystemTransaction();
            throw e;
        }
    }

So, as you see I am confused. My understanding from above paragraph is different than my udnerstanding after taking a look to the implementation.

Don't we have to commit during last system transaction execution?

Aucun commentaire:

Enregistrer un commentaire