Skip to content

Unit of Work - Savepoint Release support #516

@DmitriiDronov

Description

@DmitriiDronov

Is your feature request related to a problem? Please describe.
There are problems when using the unit of work in classes where multiple callouts are expected within the same transaction, causing this error to appear:
"All active Savepoints must be released before making callouts".

Describe the solution you'd like
It looks like a simple fix; however, some unit tests are failing.
This is something I came up with:

public void commitWork() {
        Savepoint sp = Database.setSavepoint();
        Boolean wasSuccessful = false;
        try {
            doCommitWork();
            wasSuccessful = true;
        } catch (Exception e) {
            Database.rollback(sp);
            throw e;
        } finally {
            if (wasSuccessful && !Test.isRunningTest()) {
                Database.releaseSavepoint(sp);
            }
            doAfterCommitWorkSteps(wasSuccessful);
        }
    }

I added myself the finally if-block:

            if (wasSuccessful && !Test.isRunningTest()) {
                Database.releaseSavepoint(sp);
            }

I consider Test.isRunningTest() in this scenario a bad practice since this is used to bypass unit tests.

Additional context
Release Savepoint Documentation
Make Callouts After Rolling Back DML and Releasing Savepoints

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions