Dino Geek, try to help you

How to handle optimistic and pessimistic locking in MariaDB?


Optimistic and pessimistic locking are two types of locking mechanisms that help prevent simultaneous access to a specific record in a database by multiple users. These mechanisms are valuable when considering concurrency control, as it maintains consistency and integrity in a database.

1. Optimistic Locking: Optimistic locking allows multiple transactions to access the same record for editing. It ensures data integrity by checking whether the original data was changed by some other transactions during the time of updating. When a change is detected, the transaction is rolled back. MariaDB provides a feature called MVCC (Multi-Version Concurrency Control) to handle optimistic concurrency.

To manage optimistic locking, you might use versioning. Here’s an example using an additional column `version`: \`\`\`sql CREATE TABLE products ( id INT PRIMARY KEY, name VARCHAR, price DECIMAL, version INT ); INSERT INTO products VALUES (1, ‘Product 1’, 100.0, 1); \`\`\` When you do the update, you increase the version count: \`\`\`sql UPDATE products SET name = ‘New Product’, version = version + 1 WHERE id = 1 and version = 1; \`\`\` If the version hasn’t been increased by another transaction – the row will be updated, otherwise no changes will be made.

1. Pessimistic Locking: With pessimistic locking, when a user is reading a record with the intent of updating or deleting it, a lock is applied so no other transactions can modify the data. After the transaction with the lock is complete, the record is released for other transactions.

MariaDB has `FOR UPDATE` and `LOCK IN SHARE MODE` which can be used to implement pessimistic locks by locking selected rows from `SELECT`: Example with `FOR UPDATE`: \`\`\`sql START TRANSACTION; SELECT \* FROM products WHERE id = 1 FOR UPDATE; UPDATE products SET price = 150.00 WHERE id = 1; COMMIT; \`\`\` Example with `LOCK IN SHARE MODE` which allows read but not update: \`\`\`sql START TRANSACTION; SELECT \* FROM products WHERE id = 1 LOCK IN SHARE MODE; SELECT price FROM products WHERE id = 1; COMMIT; \`\`\`

Remember that unnecessary usage of locks can decrease the performance of your database, as other transactions will wait for the lock to be released. The decision about when and how to use these locks must be cautiously made according to the application requirements and traffic flow.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use