OCILIB Tutorials v5.0.0
Beginner

Transactions — commit, rollback, autocommit

Objectives — by the end of this page you will be able to:

Concepts

Every DML your connection runs lives inside its transaction — invisible to other sessions until you decide. The connection carries the whole state; three verbs control it:

C function C++ method Effect
OCI_ConnectionCommit Commit() make pending changes permanent
OCI_ConnectionRollback Rollback() discard pending changes
OCI_ConnectionSetAutoCommit SetAutoCommit auto-commit after each execute
OCI_ConnectionGetAutoCommit GetAutoCommit() current flag
OCI_StatementExecute(st, OTEXT("update employees set comm = 0 where deptno = 30"));
OCI_ConnectionCommit(cn);        /* permanent   */
/* or OCI_ConnectionRollback(cn); → undone      */

Default behavior

Connections start with autocommit off — nothing persists until you commit.

That's the safe default for applications; turn it on only for scratch/repl-style tools — every execute then commits itself.

Transaction boundaries that work

if (!OCI_StatementExecute(st, OTEXT("update accounts set bal = bal - :amt where id = :src"))
 || !OCI_StatementExecute(st, OTEXT("update accounts set bal = bal + :amt where id = :dst")))
{
    OCI_ConnectionRollback(cn);          /* either both or neither */
}
else
{
    OCI_ConnectionCommit(cn);
}

DDL commits anyway

create/alter/drop implicitly commit in Oracle — a rollback can't undo them.

Putting it together — the typical sequence

Transaction control is two calls and a mental model:

  1. Work — any DML between Connect and Commit/Rollback is in one implicit transaction; OCILIB doesn't auto-commit.
  2. Commit — OCI_ConnectionCommit makes it permanent.
  3. Or rollback — OCI_ConnectionRollback undoes everything since the last commit.
  4. Set the mode — SetAutoCommit when you want per-statement commits (rare in real apps; it kills batching).

Hints: OCILIB defaults to manual commit — forgetting Commit loses work silently on disconnect; AutoCommit on is the demo convenience that becomes a production footgun — one DML per commit is fine for a config table, a disaster for a bulk load; and Rollback undoes everything pending — you can't partially commit mid-transaction.

Try it yourself

  1. Insert a row, open a second session and select — nothing. Commit, re-select — now it's there.
  2. Turn autocommit on and repeat: appears immediately.
  3. Run the all-or-nothing transfer with a deliberate failure on the second update.

Common pitfalls

Recap

Next: explicit OCI_Transaction objects for finer control.