TCL : Commit, Roleback, SavePoint
COMMIT
Permanently saves all changes made in the current transaction, e.g. COMMIT; — after this, the changes cannot be undone with ROLLBACK.
ROLLBACK
Undoes changes made in the current transaction that have not yet been committed, e.g. ROLLBACK; — extremely useful in testing to reset data after a destructive test.
SAVEPOINT
Marks a point within a transaction that you can roll back to without undoing the entire transaction, e.g. SAVEPOINT before_update; ... ROLLBACK TO before_update;
Why Testers Use TCL
Wrapping test data changes in transactions with savepoints lets testers safely experiment with data changes and cleanly revert them afterward, keeping the test environment consistent.
A Worked Transaction: Testing a Money Transfer
Transactions are easiest to understand with a transfer between two bank accounts. Both updates must succeed together, or neither should happen. Here is how a tester can walk through it in MySQL:
START TRANSACTION;
UPDATE accounts SET balance = balance - 500 WHERE id = 1; -- debit
SAVEPOINT after_debit;
UPDATE accounts SET balance = balance + 500 WHERE id = 2; -- credit
SELECT id, balance FROM accounts WHERE id IN (1, 2); -- inspect
ROLLBACK TO SAVEPOINT after_debit; -- undo only the credit
SELECT id, balance FROM accounts WHERE id IN (1, 2); -- debit still applied
ROLLBACK; -- undo everything, back to the start
Step by step, the first update takes money from account 1 and the savepoint marks that moment. The second update adds money to account 2. Rolling back to the savepoint removes only the credit, and the transaction stays open. The final ROLLBACK cancels the debit too, so both balances return to their starting values. If the outcome were correct, COMMIT would make it permanent instead.
This is a good way to check what other sessions can see. Open a second connection while the transaction is still open: under the default isolation level, it should not see the uncommitted balances.
Syntax Differences Between Databases
The ideas are the same everywhere, but keywords and default behaviour vary, so check which database your application uses.
| Database | Start | Savepoint | Roll back to it | Notes |
|---|---|---|---|---|
| MySQL (InnoDB) | START TRANSACTION | SAVEPOINT s1 | ROLLBACK TO SAVEPOINT s1 | Autocommit is on by default; DDL statements commit implicitly |
| PostgreSQL | BEGIN | SAVEPOINT s1 | ROLLBACK TO SAVEPOINT s1 | Most DDL can be rolled back inside a transaction |
| SQL Server | BEGIN TRANSACTION | SAVE TRANSACTION s1 | ROLLBACK TRANSACTION s1 | Autocommit unless a transaction is started |
| Oracle | Starts automatically with the first change | SAVEPOINT s1 | ROLLBACK TO s1 | DDL statements commit implicitly |
In MySQL, transactions only work on storage engines that support them, such as InnoDB. Changes to an older MyISAM table are saved immediately and cannot be rolled back.
Common Mistakes and a Tester's Checklist
- Autocommit in your SQL client. Many tools commit each statement as soon as it runs. If you did not explicitly start a transaction, a later
ROLLBACKhas nothing to undo. - Leaving a transaction open. Uncommitted changes can hold row locks, making the application under test hang or time out. Always finish with
COMMITorROLLBACK. - Running DDL in the middle. In MySQL and Oracle, a
CREATEorALTERcommits everything before it, so earlier test changes become permanent. - Assuming a savepoint ends the transaction. Rolling back to a savepoint keeps the transaction open; you still need a final commit or rollback.
What to verify when testing transactional features
- A failure halfway through, such as an invalid account number in the second step, leaves no partial changes behind.
- Two users acting on the same record at once do not corrupt the data.
- Application error messages appear when the database rolls back, instead of a false success screen.
- Test data created during a run is rolled back or cleaned up, so the next run starts from a known state.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.