Current Location: Home> Latest Articles> Guide to Concurrency Control and Lock Mechanisms in PHP with Oracle Database

Guide to Concurrency Control and Lock Mechanisms in PHP with Oracle Database

M66 2025-07-13

Guide to Concurrency Control and Lock Mechanisms in PHP with Oracle Database

When developing web applications, database concurrency control and lock mechanisms are critical. In high-concurrency scenarios, improper handling of concurrent data access can lead to issues with data consistency and integrity. This article introduces how to use Oracle's concurrency control and lock mechanisms in PHP, demonstrating two common concurrency control methods—pessimistic and optimistic concurrency control—and provides related code examples.

Pessimistic Concurrency Control

Pessimistic concurrency control means locking data before performing any operations, preventing other users from modifying the same data simultaneously. In Oracle, this can be achieved using the FOR UPDATE statement.

Here’s a code example for using pessimistic concurrency control in PHP:


<?php
// Connect to Oracle database
$conn = oci_connect('username', 'password', 'database');

// Query the data to update and lock it
$query = "SELECT * FROM my_table WHERE id = :id FOR UPDATE";
$stmt = oci_parse($conn, $query);
$id = 1;
oci_bind_by_name($stmt, ':id', $id);
oci_execute($stmt);

// Update data
$query = "UPDATE my_table SET field = :field WHERE id = :id";
$stmt = oci_parse($conn, $query);
$field = 'new value';
oci_bind_by_name($stmt, ':field', $field);
oci_bind_by_name($stmt, ':id', $id);
oci_execute($stmt);

// Commit the transaction and close the connection
oci_commit($conn);
oci_close($conn);
?>

In the above code, the SELECT ... FOR UPDATE statement locks the rows to be updated, followed by the update operation. Finally, the transaction is committed using oci_commit(), and the database connection is closed with oci_close().

Optimistic Concurrency Control

Unlike pessimistic concurrency control, optimistic concurrency control does not lock data before performing operations. Instead, it checks whether the data has been modified by other users when updating. In Oracle, this can be done using the VERSIONS BETWEEN statement.

Here’s a code example for using optimistic concurrency control in PHP:


<?php
// Connect to Oracle database
$conn = oci_connect('username', 'password', 'database');

// Query the data and get version information
$query = "SELECT * FROM my_table WHERE id = :id";
$stmt = oci_parse($conn, $query);
$id = 1;
oci_bind_by_name($stmt, ':id', $id);
oci_execute($stmt);
$row = oci_fetch_array($stmt, OCI_ASSOC);
$oldVersion = $row['VERSION'];

// Update data
$newVersion = $oldVersion + 1;
$query = "UPDATE my_table SET field = :field, version = :newVersion WHERE id = :id AND version = :oldVersion";
$stmt = oci_parse($conn, $query);
$field = 'new value';
oci_bind_by_name($stmt, ':field', $field);
oci_bind_by_name($stmt, ':newVersion', $newVersion);
oci_bind_by_name($stmt, ':id', $id);
oci_bind_by_name($stmt, ':oldVersion', $oldVersion);
oci_execute($stmt);

// Check the number of updated rows
if (oci_num_rows($stmt) == 0) {
    // Update failed, data has been modified
    oci_rollback($conn);
} else {
    // Update successful
    oci_commit($conn);
}

// Close the connection
oci_close($conn);
?>

In this code example, we first query the data and retrieve the old version information. When updating, the WHERE clause checks if the version number matches the old version. If no rows are updated, it indicates that the data has been modified by other users, and a rollback is performed. Otherwise, the transaction is committed.

Summary

When using Oracle databases with PHP, concurrency control can be implemented using both pessimistic and optimistic strategies. Pessimistic concurrency control ensures data consistency by locking the data before operations, while optimistic concurrency control checks the data version during the update process, making it suitable for scenarios with fewer conflicts. Choosing the right concurrency control strategy based on specific business needs is crucial for ensuring database stability and application efficiency.

This article has introduced how to implement concurrency control and lock mechanisms with Oracle databases in PHP, and we hope this information helps developers manage concurrent access more effectively.