Use SAVEPOINT and ROLLBACK TO to undo only a portion of an active transaction.
The objective is to understand the behavior and operational limits, not only the method name.
What It Does
Use SAVEPOINT and ROLLBACK TO to undo only a portion of an active transaction.
Prerequisites
Use a dedicated development environment and sample data. Where a snippet uses an injected connection, repository, request value or mailer, supply it from your own application setup.
Example 1: Basic Use
A focused working pattern for this topic. Names such as $pdo, $authContext and $repo represent dependencies provided by the application unless initialized in the snippet.
<?php
$pdo->beginTransaction();
try {
$pdo->exec('UPDATE accounts SET balance = balance - 20 WHERE id = 1');
$pdo->exec('SAVEPOINT optional_step');
try { $pdo->exec("INSERT INTO audit_events(message) VALUES ('charged')"); }
catch (PDOException $e) { $pdo->exec('ROLLBACK TO SAVEPOINT optional_step'); }
$pdo->commit();
} catch (Throwable $e) { if ($pdo->inTransaction()) $pdo->rollBack(); throw $e; }
Example 2: A Second Scenario
A second use case that adds a different operation or data flow specific to this topic.
<?php
$pdo->beginTransaction();
try {
$pdo->exec('SAVEPOINT before_optional_audit');
try { $pdo->prepare('INSERT INTO audit_items (code) VALUES (?)')->execute([$code]); }
catch (PDOException $e) { $pdo->exec('ROLLBACK TO SAVEPOINT before_optional_audit'); }
$pdo->commit();
} catch (Throwable $e) { if ($pdo->inTransaction()) $pdo->rollBack(); throw $e; }
Example 3: Practical Checks
A practical validation, failure or deployment concern specific to this topic.
<?php
$pdo->beginTransaction();
try { $pdo->exec('SAVEPOINT editable_step'); $pdo->prepare('UPDATE items SET note = ? WHERE id = ?')->execute([$note,$id]); $pdo->exec('RELEASE SAVEPOINT editable_step'); $pdo->commit(); }
catch (Throwable $e) { if ($pdo->inTransaction()) $pdo->rollBack(); throw $e; }
Important Behavior and Edge Cases
Savepoint syntax and nesting behavior differ by driver; never call beginTransaction as a nested savepoint.
For production use, also account for error handling, data validation, resource limits and environment-specific driver behavior.
Comparison with Related Approaches
| Approach | Important distinction |
|---|---|
| Using Savepoints with PDO | Use SAVEPOINT and ROLLBACK TO to undo only a portion of an active transaction. |
| Related API / pattern | Use this workflow as a focused starting point rather than a substitute for application-level tests. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Assuming the happy path covers edge cases | Savepoint syntax and nesting behavior differ by driver; never call beginTransaction as a nested savepoint. |
| Using a related API interchangeably | Use this workflow as a focused starting point rather than a substitute for application-level tests. |
Key Takeaways
- Use SAVEPOINT and ROLLBACK TO to undo only a portion of an active transaction.
- Savepoint syntax and nesting behavior differ by driver; never call beginTransaction as a nested savepoint.
- Use this workflow as a focused starting point rather than a substitute for application-level tests.
Choose this API or pattern when its exact semantics match your task, and verify the result against the database driver and runtime used by your application.