2

I am doing some testing on Salesforce Transaction controls and noticed something odd. I was wondering if I am missing something or if I am just not following 'best practices'.

System.Savepoint sp = Database.setSavepoint();
try{
SomeClass.someMethod(string jsonString) //Method parses a large JSON string and inserts/updates and deletes data
}catch(Exception ex){
Database.rollback(sp);
}

I have updated that 'someMethod' above to throw different SOQL, DML, CPU errors and they after modifying data and it does not successfully rollback. However simple divide by 0 and explicit exceptions rollback successfully.

Overall my question is when using Salesforce's Database savepoints and rollbacks and a limit error is encountered, does the transaction successfully rollback or does the transaction stop and leave the DB where it stands?

2
  • 1
    When an exception is thrown, it "should" rollback the entire transaction, not just to the rollback point. However that could depend on the type of exception as it's possible the exception could prevent recovery.
    – crmprogdev
    Dec 15, 2016 at 13:13
  • I opened a case with Salesforce and based on what I have heard so far it sounds like Governor Limit exceptions are not catchable so you cannot handle the rollback. Their suggestion is the same as yours where you have to monitor your limits and throw and terminate/rollback when you are getting close. Dec 22, 2016 at 18:13

1 Answer 1

2

From the Apex Docs under Transaction Control:

Each rollback counts against the governor limit for DML statements. You will receive a runtime error if you try to rollback the database additional times.

I would take this to mean that the rollback will fail if you reach a DML limit exception.

2
  • That makes sense for DML limits however if I induce a SOQL limit (101 Queries) this also doesn't roll back successfully. Since I still have some DML bandwidth in this method I would assume that it would still rollback, but it doesn't appear to do so. Dec 15, 2016 at 14:01
  • There's are limits class methods that I recommend you implement in your code before trying to use rollbacks. That's something one normally does when using any feature on the platform that can fail because of exceeding limits. If query limits are causing issues for you, then you clearly need to be using getlimitQueries() and getQueries() in your code.
    – crmprogdev
    Dec 15, 2016 at 14:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .