In the document Oracle Database PL/SQL Language Reference 11g Release 2 (11.2), the "Implicit Rollbacks" section begins with this text:
"Before running an INSERT, UPDATE, DELETE, or MERGE statement, the database marks an implicit savepoint (unavailable to you). If the statement fails, the database rolls back to the savepoint. Usually, just the failed SQL statement is rolled back, not the whole transaction."
So if I run a SQL sentence into an PL/SQL program, and the sentence fails, then the sentence will be automatically rolled-back. That is ok.
But the same section ends with this text:
"If you exit a stored subprogram with an unhandled exception, PL/SQL does not assign values to OUT parameters, and does not do any rollback."
It seems to be contrary to the first text: No rollback is done if my program ends with an untrapped exception. But the first text says that the rollback is done automatically if the SQL sentence fails.
So if my stored program contains a SQL sentence, the sentence fails, the exception is not trapped, and my program ends, then should the SQL sentence be rolled-back or not? Does the document have a contradiction?
Related questions in Stack Overflow:
Update (solved): Thank DrabJay for the example, it is clearer now:
- one thing is the rollback of the SQL statement.
- other thing is the rollback of the program that contains the SQL statement.
The rollback of a SQL statement that fails is ALWAYS done (independently of being into a program or not). The rollback of the program depends of the caller:
- If a rollback is applied to the caller, then a rollback is applied to the program.
- If no rollback is applied to the caller, then no rollback is applied to the program.
If the program is an anonymous block (no caller exists), it is equivalent to being called from a user statement, and a user statement that fails is rolled back automatically, so the anonymous block is rolled back.
I think the document should be clearer, specially on the words "and does not do any rollback":
"If you exit a stored subprogram with an unhandled exception, PL/SQL does not assign values to OUT parameters, and does not do any rollback."