Basics
Creating a Document
Head and Body Elements
Formatting Text
Creating Lists
Embedding Images
URLs Explained
Linking to Documents
Creating Tables
Forms Form Basics
Input and Textarea
Select Lists
Frames Framesets
The Frame Element
Nested Framesets
Targetting Frames
Inline Frames
Tips and Tricks Meta Tags
Transition Effects
HTML Generators Create a Document
Create a List
Create a Table
HTML
Basics
Implementing CSS
CSS Syntax
Pseudo Classes/Elements
CSS Classes
CSS Properties Font Properties
Color and Background
Text Properties
Border Properties
Margins and Padding
Size and Position
Tips and Tricks Menu Buttons
Special Effects
CSS
Basics
Running a Script
Variables
Expressions and Operators
Objects.Properties.Methods
The Date Object
Strings
Regular Expressions
Defining RegExp Patterns
Branches and Conditions
Loops
Arrays Array Basics
Array Methods
Sorting Arrays

User-Defined Functions
Cookies
Windows
Frames
Tips and Tricks Image Replacement
Using Includes
Form Validation
Debugging
JavaScript
Basics
Creating a Script
Running a Script
Variables
Expressions and Operators
Strings Strings Basics
Strings and Substrings
Replacing Substrings
Regular Expressions
Branches and Conditions
Loops
Arrays Array Basics
Array Functions
Sorting Arrays
User-Defined Functions
Include and Require
Uploading Files
File Functions
Session Variables
Tips and Tricks Page Templates
Form Reply Scripts
Form Validation
JavaScript to PHP
PHP
Basics
Create and Drop
Show and Describe
Insert, Update and Delete
Querying
Join Queries
Functions
Table Locking
PHP/MySQL Functions Accessing a Database
Querying with PHP
Create and Drop with PHP
Insert and Update with PHP
Frequently Used Functions MySQL
Basics
Layout and Navigation
Page Content Style
Web Page Copy
Graphics and Animation
HTML Forms
Accessibility
Legal Requirements
MySQL
MySQLBasicsCreate and DropShow and DescribeInsert, Update and DeleteQueryingJoin QueriesFunctionsMySQL Table LockingTable LockingPHP/MySQL FunctionsAccessing a DatabaseQuerying with PHPCreate and Drop with PHPInsert & Update with PHPFrequently Used Functions

Home > MySQL > Table Lockingprinter version

MySQL Table Locking

This tutorial assumes that you have access to the MySQL command prompt and uses the people table created in the Create Tutorial and the payroll table referred to in the Join Queries Tutorial.

Concurrency

MySQL provides an environment where multiple users can read and write to and from database tables and this presents potential problems.

For example an application may allow a number of users to update tables based on the results of other tables.

If the first user runs a query on table 1 and delays the update on table 2, a second user meantime may update table 1.

This will lead the first user to update table 2 based on stale information from table 1.

Concurrency isn't a problem if your scripts only insert, update or delete one table row in a single operation.

The problem only occurs if your scripts allow multiple users to insert, update or delete based on results from SELECT statements.

Lock Variables

Each table has a lock variable associated with it. If a user sets the lock variable on a table then other users cannot perform particular operations on that table until the lock has been released.

When a user sets a lock variable for a table they are said to hold the lock for that table.

There are two types of lock.



When a request for locks is made then all the tables requiring locks must be listed in a single request.

When a lock is requested it is placed in either a write-lock queue or read-lock queue.

The write-lock queue always has priority over the read-lock queue - no matter how long the read lock requests have been pending.

If not all the locks can be granted then the request is suspended until they all become available.

After operations have been completed a lock is released with UNLOCK TABLES, which releases the locks held on all the tables.

Example of Table Locking

To demonstrate an example of locking, the people and payroll table that were created in an earlier tutorial can be used. Their contents are reproduced below.

The people and payroll table
mysql> SELECT * FROM people;
+------------+--------+------------+------------+-------+---+
|id|firstname|lastname|address     |county      |country|age|
+------------+--------+------------+------------+-------+---+
|1 |Dave     |Clark   |Main Street |West Lothian|UK     |40 |
|2 |Mary     |Smith   |North Street|Cumbria     |UK     |36 |
|3 |Dave     |Smith   |West Street |Strathclyde |UK     |51 |
+------------+--------+------------+------------+-------+---+
3 rows in set (0.00 sec)

mysql> SELECT * FROM payroll;
+------------+--------+----------+------+
|id|firstname|lastname|job       |salary|
+------------+--------+----------+------+
|1 |Dave     |Clark   |Programmer|20010 |
|2 |Mary     |Smith   |Manager   |25030 |
|3 |Dave     |Smith   |Operator  |12400 |
+------------+--------+----------+------+
3 rows in set (0.00 sec)

mysql>


What Can Happen without Table Locking

Suppose someone in Human Resources wants to update the database because a member of staff has left and been replaced with someone on a different salary.

Both the people and payroll table need to be updated with the details of the new member of staff.

The people table is updated first but before the payroll table can be updated the person doing it is called away to deal with something else.

Meantime the Payroll Department are preparing a report that will show employees details based on information from the people table and the payroll table.

When queries are run on the tables the information from both tables don't match.

What Happens with Table Locking

Before updating the database both the people and payroll table are write locked. This prevents anyone reading from or writing to the tables until the updates have been completed.

Example of Table Locking
mysql> LOCK TABLES people WRITE, payroll WRITE;

UPDATE people SET firstname = 'Frank', lastname = 'Jones', address = 'Brick Lane', county = 'London', age = 34 WHERE id = 3;

UPDATE payroll SET firstname = 'Frank', lastname = 'Jones', salary = 11200 WHERE id = 3;

UNLOCK TABLES;

mysql>


Table Locking for Performance

The other case where locking may be used is to increase performance for an important query.

If a complex query is running slowly then stopping all other users from querying the database will speed it up.

This will inconvenience other users so should only be used where really necessary.

Previous - MySQL Functions Previous - MySQL Functions     Next - Accessing a Database Next - MySQL Accessing a Database


Privacy | Terms | Contact | Links | Sitemap | RSS Feeds RSS and JavaScript Feeds
©2010 www.webdesignworkmate.co.uk all rights reserved 
Design and Production by smallbizonline website design © 2000-2010
Valid HTML 4.01! Level Double-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0Valid CSS!
Recommended Reading
PHP6 and MySQL5

PHP and MySQL Web Development

Build Your Own Database Driven Website using PHP and 

MySQL

How to do Everything with PHP and MySQL