27Apr
By: Steven Feuerstein On: April 27, 2022 In: APEX Developer Solutions, Feuertips Comments: 0

PL/SQL is a database programming language, which means that whatever else you use it for, you definitely should be using it to manage the data in your database. And when you change data in a table, you must commit those changes for them to be saved, for others to see them.

It’s not hard to commit changes. You just use the commit; statement.

But when and where should you commit? Here are some thoughts on committing…

  • Generally, commits should not be inside your procedures and functions. Leave it to the user to decide when to save their changes. Of course, this doesn’t apply to batch processing, in which case you will likely want to commit at the end.
  • If you feel strongly enough about the above general principle, you can actually turn off the ability to execute a commit inside a stored program unit using the “alter session disable commit in procedure” command.
  • When you execute a DDL statement (e.g., create table), Oracle database performs a commit before and after you execute the statement. More info here.
  • If the DDL statement fails with a syntax error (e.g., table name is a reserved word), there is no commit. If it fails with a semantic error (e.g., table with that name already exists), then there is a commit. Demonstration of this effect here.
  • Committing changes inside a stored program unit can make testing harder (have to rebuild contents of tables). Disabling the commit at the session level could help with this, but you also might want to consider encapsulating the call to commit to give you an API-based approach to turning off commits while testing. I built a my_commit package on LiveSQL in case you want to try this out.
  • Exceptions generally do not cause a rollback of outstanding changes in your session. Check out this script.
  • You can use PL/Scope to find commit and rollback statements in your code. A demo is available at LiveSQL!
  • Use the autonmous_transaction pragma to restrict committing of changes to only those made in the specific procedure or function. We’ve got a script for that!
  • Sometimes developers feel the need to incrementally commit (say, every 5,000 rows inserted). That’s fine, as long as the application logic (definition of a transaction) will support it. You can even do incremental committing with forall, though it’s not naturally or declaratively inclined that way.
  • When running code in APEX, remember that even though you may define individual processes/steps to execute (let’s say, on submit), they are all part of a single transaction – unless your code executes a commit somewhere along the line. I’ll demonstrate this during the Feuertip session here.

Feuertips

Wednesdays at 11 am ET, we go live on YouTube and Facebook to provide you with a not-to-be-missed PL/SQL tip and fun conversation. Sure, we record it so you can always watch later, but live is so much more rewarding!

A participant is selected to choose from 3 organizations dedicated to the preservation of our planet and its biodiversity. Insum will then make a donation of $25 to that group in your name.

What could be better than leveling up your PL/SQL tips and helping a worthy organization? Join us Wednesdays at 11!

Share this:
Share

Leave reply:

Your email address will not be published. Required fields are marked *