PHP - Committing and Rolling Back the Transaction

Committing a transaction in advanced PHP programming is the process of finalizing and applying the changes made within the transaction to the database. Committing a transaction ensures that all the database operations within the transaction are saved permanently. In this explanation, I'll cover how to commit a transaction using PDO (PHP Data Objects) in PHP.

Starting a Transaction:

Before committing a transaction, you need to start it using the beginTransaction() method. This sets up a point where all subsequent database operations are considered part of the transaction.

$dsn = "mysql:host=localhost;dbname=mydatabase";
$username = "your_username";
$password = "your_password";
try {
  $pdo = new PDO($dsn, $username, $password);
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  // Start a transaction
  $pdo->beginTransaction();
  // Perform database operations
  // Commit the transaction
  $pdo->commit();
} catch (PDOException $e) {
  // Roll back the transaction on error
  $pdo->rollBack();
  echo "Transaction failed: " . $e->getMessage();
}
?>

Committing the Transaction:

To commit the transaction, call the commit() method on the PDO instance. This will finalize all changes made within the transaction and make them permanent in the database.

// Commit the transaction
$pdo->commit();

Rolling Back on Error:

It's important to note that if any of the database operations within the transaction encounters an error, you should roll back the transaction to ensure data integrity. Use the rollBack() method within the catch block to undo any changes made during the transaction.

try {
  // Perform database operations
  // ...
  // Commit the transaction
  $pdo->commit();
} catch (PDOException $e) {
  // Roll back the transaction on error
  $pdo->rollBack();
  echo "Transaction failed: " . $e->getMessage();
}

Benefits of Committing Transactions:

Data Integrity: Committing ensures that a group of related operations is applied as a whole, maintaining data consistency and avoiding partial changes.

ACID Properties: Committing a transaction ensures that the ACID properties (Atomicity, Consistency, Isolation, Durability) are maintained.

Use Cases for Transactions:

Transactions are commonly used when you need to perform multiple database operations as a single unit of work. For example:

Transferring funds between bank accounts (withdraw from one, deposit into another).

Making a reservation (reduce available seats, record the reservation).

Updating inventory (reduce quantity of sold items, update stock levels).

Committing transactions is essential for maintaining the integrity of your application's data. By enclosing related operations within transactions and ensuring that they are committed only when all operations succeed, you can ensure accurate and reliable data manipulation.

Rolling Back the Transaction :

Rolling back a transaction in advanced PHP programming is the process of undoing all changes made within the transaction and reverting the database to its original state. Rolling back is typically done when an error occurs during the execution of a transaction, ensuring data integrity and consistency. In this explanation, I'll cover how to roll back a transaction using PDO (PHP Data Objects) in PHP.

Starting a Transaction:

Before rolling back a transaction, you need to start it using the beginTransaction() method. This establishes a point where all subsequent database operations are considered part of the transaction.

$dsn = "mysql:host=localhost;dbname=mydatabase";
$username = "your_username";
$password = "your_password";
try {
  $pdo = new PDO($dsn, $username, $password);
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  // Start a transaction
  $pdo->beginTransaction();
  // Perform database operations
  // Commit or roll back the transaction based on conditions
} catch (PDOException $e) {
  // Roll back the transaction on error
  $pdo->rollBack();
  echo "Transaction failed: " . $e->getMessage();
}
?>

Rolling Back the Transaction:

To roll back the transaction, call the rollBack() method on the PDO instance. This will undo all changes made within the transaction and restore the database to its state before the transaction started.

// Roll back the transaction
$pdo->rollBack();

Handling Errors and Rollback:

When an error occurs during the execution of the transaction, catch the PDOException in the catch block and roll back the transaction to maintain data integrity.

try {
  // Perform database operations
  // ...
  // Commit or roll back the transaction based on conditions
} catch (PDOException $e) {
  // Roll back the transaction on error
  $pdo->rollBack();
  echo "Transaction failed: " . $e->getMessage();
}

Benefits of Rolling Back Transactions:

Data Integrity: Rolling back ensures that incomplete or erroneous changes are not permanently applied to the database, maintaining data consistency.

Error Handling: Rolling back allows you to handle errors gracefully without leaving the database in an inconsistent state.

Use Cases for Rolling Back:

Rolling back transactions is commonly used when an error occurs during a series of related database operations. For example:

An error occurs while updating multiple records in a single transaction.

A validation check fails before committing changes.

An external API call fails after making changes.

Rolling back transactions ensures that you can recover from errors without affecting the integrity of your application's data. By using transactions and handling errors properly, you can create more robust and reliable database interactions in your PHP applications.