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.
Parts
» This page intentionally left blank
» Reporting and Data Mining Database Applications
» Database Applications and SQL
» Reading Specified Columns and Rows from a Single Table
» “Does Not Work with Microsoft Access ANSI-89 SQL”
» Processing SQL Statements in Microsoft Access 2010
» Using SQL in Microsoft SQL Server 2008 R2
» Using SQL in Oracle Database 11g
» Using SQL in Oracle MySQL 5.5
» Wildcards in SQL WHERE Clauses
» Using SQL Built-in Functions
» SQL Expressions in SQL SELECT Statements
» Querying Multiple Tables with Subqueries
» Querying Multiple Tables with Joins
» Comparing Subqueries and Joins
» Finding Functional Dependencies
» Eliminating Anomalies from Multivalued Dependencies
» The Multivalue, Multicolumn Problem
» The General-Purpose Remarks Column
» R Diagrams Using the IE Crow’s Foot Model
» The Multivalued Attribute Pattern
» The Archetype/Instance Pattern
» The Student Acceptance Letter
» X This is a warning, no further action is required.
» 1:1 Relationships Between Strong Entities
» M Relationships Between Strong Entities
» Relationships in Mixed Entity Designs
» Representing Ternary and Higher-Order Relationships
» Relational Representation of the Highline University Data Model
» Surrogate Key Database Design
» Column Properties for the View Ridge Database Design Tables
» Variations in SQL Data Types
» Implementing Data Constraints
» Populating the View Ridge Database Tables
» Using Triggers to Provide Default Values
» The WORK_AddWorkTransaction Stored Procedure
» • If a PROJECT row is deleted, then the project has been canceled, and it is unneces-
» Reducing Cardinalities (with Data Loss)
» Optimistic Versus Pessimistic Locking
» Declaring Lock Characteristics
» Processing Rights and Responsibilities
» Recovery via Rollback/Rollforward
» Maintaining the Data Repository
» Types of Distributed Databases
» • Express Edition. This free, feature-limited version is available for download. It
» SQL Server 2008 R2 SQL Statements and SQL Scripts
» Creating the View Ridge Database Table Structure
» Populating the VRG Tables with Data
» The Stored Procedure InsertCustomerAndInterests
» The Stored Procedure InsertCustomerWithTransaction
» A Trigger for Setting Default Values
» A Trigger for Enforcing a Data Constraint
» A Trigger for Enforcing a Required Child Constraint
» Creating an ODBC Data Source Name
» Materializing XML Documents with XSLT
» Using the SQL SELECT . . . FOR XML Statement
» Multitable SELECT with FOR XML
» A Schema with Two Multivalued Paths
» Problems with Operational Data
» Using SQL for Market Basket Analysis
Show more