Declaring Lock Characteristics

Declaring Lock Characteristics

As you can see, concurrency control is a complicated subject; determining the level, type, and placement of the lock is difficult. Sometimes, too, the optimum locking strategy depends on which transactions are active and what they are doing. For these and other reasons, database application programs do not generally explicitly issue locks as shown in Figures 9-8 and 9-9. Instead, they mark transaction boundaries and then declare the type of locking behavior they want the DBMS to use. In this way, the DBMS can place and remove locks and even change the level and type of locks dynamically.

Figure 9-10 shows the pencil transaction with transaction boundaries marked with SQL BEGIN TRANSACTION statement, SQL COMMIT TRANSACTION statement, and SQL ROLLBACK TRANSACTION statement. These are the SQL standard commands for controlling transactions. The SQL BEGIN TRANSACTION statement explicitly marks the start of a new transaction, while the SQL COMMIT TRANSACTION statement makes any database changes made by the transaction permanent and marks the end of the transaction. If there is a need to undo the changes made during the transaction due to an error in the process, the SQL ROLLBACK TRANSACTION statement is used to undo all transaction changes, and return the database to the state it was in before the transaction was attempted. Thus, the SQL ROLLBACK TRANSACTION statement also marks the end of the transaction, but with a very differenct outcome.

As usual, each DBMS product implements these SQL statements in a slightly different way. SQL Server does not require the SQL keyword TRANSACTION,

allows the abbreviation TRANS, and also allows the use of the SQL WORK keyword with COMMIT and ROLLBACK. Oracle Database uses SET TRANSACTION with COMMIT and ROLLBACK. MySQL does not use the SQL keyword TRANSACTION, while it allows (but does not require) use of the SQL WORK keyword in its place.

Also note that the SQL BEGIN TRANSACTION statement is not the same as the as the SQL BEGIN statement used in SQL/PSM control-of-flow statements (as discussed in Chapters 7, 10, 10A, and 10B. Thus, you may have to use a different syntax for marking transactions within a trigger or stored procedure. For example, MySQL marks the beginning of transactions in a BEGIN . . . END block with the SQL START TRANSACTION statement. As usual, be sure to consult the documentation for the DBMS product you are using.

Part 4 Multiuser Database Processing

/* *** EXAMPLE CODE - DO NOT RUN *** */ BEGIN TRANSACTION; SELECT

PRODUCT.Name, PRODUCT.Quantity

PRODUCT.Name = Pencil ;

Set NewQuantity = PRODUCT.Quantity – 5; {process transaction – take exception action if NewQuantity < 0, etc.} UPDATE

PRODUCT

SET

PRODUCT.Quantity = NewQuantity

WHERE

PRODUCT.Name = Pencil ;

{continue processing transaction} . . .

IF {transaction has completed normally} THEN COMMIT TRANSACTION; ELSE ROLLBACK TRANSACTION; END IF; Continue processing other actions not part of this transaction . . .

Figure 9-10

Marking Transaction

These boundaries are the essential information that the DBMS needs to enforce

Boundaries

the different locking strategies. If the developer now declares via a system parameter that

he or she wants optimistic locking, the DBMS will implicitly set locks for that locking style. If, however, the developer declares pessimistic locking, the DBMS will set the locks differently.