Title: OPS-27: Understanding Record and Table Locking In OpenEdge
1OPS-27 Understanding Record and Table Locking In
OpenEdge SQL Applications
Brian Werne
Sr. Engineering Manager, OpenEdge SQL and
OpenEdge Management
2Goals
- What if you could
- Improve concurrency?
- Avoid a lock table overflow?
- Increase throughput?
- Deal with potential lock contention errors?
3Unlock the facts Combination ? - ? - ?
4Basics of Locking
Grades of Locks
- Schema lock
- Table lock
- Record lock
5Basics of Locking
Lock types
- eXclusive (X)
- Intent eXclusive (IX)
- Share Intent eXclusive (SIX)
- Share (S)
- Intent Share (IS)
- NoLock () dirty read
Lock Type
Lock strength
6How locking conflicts can occur
The Lock Table (simplified)
Active Queued (Waiting) Lock Entries
Hash Table
7Where to monitor locks?
PROMON Option 4 Record Locking Table
8Where to monitor locks?
OpenEdge Management Locks and Latches page
or VST _LockReq
9Unlock the facts Combination ? - ? - ?
- Review of locking model and definitions
- Available SQL features to influence locking
- Examples on lock contention strategies
10Basics of transactions
Locking protocols
- Transaction scope
- - No locks allowed outside a SQL transaction
- Isolation level
- - Implicit lock strength depends on SQL
isolation level setting
11Basics of Transactions
Transaction scope
con.setAutoCommit(true)
con.commit() OR con.rollback()
12Isolation Level Affect on Lock Type
Isolation Level Update Update Fetch Fetch
Isolation Level Table Lock Record Lock Table Lock Record Lock
Read Uncommitted --- --- NoLock NoLock
Read Committed IX Exclusive IS Share
Repeatable Read IX Exclusive IS Share
Serializable SIX Exclusive Share None
13Basics of Transactions
The Basics
- Setting Isolation level to affect
- transaction strength
con.setTransactionIsolation(
Connection.TRANSACTION_READ_COMMITTED)
14ODBC Setting Isolation Level
15Strategies
Beyond The Basics increasing throughput
- Lock Table statement
- - Similar to Serializable
- ( High throughput, low concurrency)
-
LOCK TABLE table_name , table_name , ... IN
SHARE EXCLUSIVE MODE
16Strategies
Beyond The Basics increasing throughput
- Select for Update
- Similar to Repeatable Read
- ( High throughput, better concurrency)
Select id, name from PUB.Standings where balance
lt 100 FOR UPDATE
17Locking Contention or Overflow
- Avoiding lock table overflow error?
18Strategies
The basics of reporting
- Helping the cost-based optimizer in the OpenEdge
SQL engine
UPDATE TABLE STATISTICS AND INDEX STATISTICS AND
ALL COLUMN STATISTICS FOR table_name
- Selecting only the columns you need
- Using good predicates
- Possibly defining more indexes
19Strategies
Measuring and tuning the Basics
select substring(_Description,1,80) from
pub._Sql_Qplan
- Using NoExecute to help query tuning
Select name from pub.customer where countryName
! USA NOEXECUTE
20Beyond the Basics
Avoid lock table overflow or buffer pool flush
- Limit result set size Select TOP n
SELECT TOP 5 Name, Balance FROM pub.Standings
ORDER BY Balance
21Unlock the facts Combination ? - ? - ?
- Review of locking model and definitions
- Available SQL features to influence locking
22Locking Contention
- Still getting lock contention errors?
23Beyond the Basics
Controlling Lock Timeout
- ABL
- Default for ABL 30 minutes
- Control process with -lkwtmo
- SQL
- Default for SQL 5 seconds
- Process control is environment variable
PROSQL_LOCKWAIT_TIMEOUT
24Locking Contention Read Retry
- Isolation level Read Committed
- Encounter lock error -210015
- Try-catch block to capture lock error
- Retry fetching the record
25Locking Contention Read Retry
RECORD_LOCKED error - Read Retry
rs1 stmt.executeQuery("SELECT name FROM
pub.customer") while (read_rows) try
if (rs1.next())
row_num else read_rows
false // no more rows catch
(SQLException e)
26Locking Contention Read Retry
RECORD_LOCKED error - Read Retry (part2)
catch (SQLException e) int error_code
e.getErrorCode() // Throw the exception if
row/table lock // not detected. Otherwise,
retry. if (error_code ! -210015)
throw e retries // end catch if
(retries MAX_FETCH_RETRIES) read_rows
false // end while
27Locking Contention
- Make use of locking hints
SELECT from PUB.Status WITH ()
28Locking Contention Readpast
- Isolation level Read Committed
- Set lock wait timeout value with READPAST locking
hint - Action Skips record if timeout value exceeded
- Warning possible to get incomplete
resultset
29Locking Contention - Readpast
Using READPAST Nowait locking hint
// Skip lock conflict error and the associated
row String select1 "SELECT name FROM
pub.customer" "
WITH (READPAST NOWAIT)" // record read
will return immediately if // there is a lock
conflict rs1 stmt.executeQuery(select1)
while (rs1.next())
System.out.println("Customer Name "
rs1.getString(1))
30Locking Contention - Readpast
Using READPAST WAIT n
String select2 "SELECT name FROM pub.customer"
" WITH (READPAST WAIT
10)" // record read will wait for up to 10
seconds if // there is a lock conflict rs2
stmt.executeQuery(select2) while
(rs2.next()) System.out.println("Cust
omer Name " rs2.getString(1))
31Locking Contention NoLock and Rowid
- Isolation level Read Committed
- Fetch initial record using NoLock locking hint
- Using ROWID re-fetch record later with
appropriate lock
32Locking Contention NoLock and Rowid
Using NoLock locking hint
// Get ROWID for customer at Hurricane Lane
without // locking record String cust_addr
"Hurricane Lane" String cust_addr_qry_nl
"SELECT ROWID FROM pub.customer WHERE
address \'" cust_addr "\' WITH
(NOLOCK)" // Get specific customer and only
lock one record String cust_addr_qry
"SELECT name FROM pub.customer " "WHERE
ROWID ?"
33Locking Contention NoLock and Rowid
Using NoLock lock hint (part 2)
// Execute query to get rowid for a customer
without // any record locks rs1
stmt.executeQuery(cust_addr_qry_nl) // Using
the result set from the FIND NOLOCK query, //
use the rowid as a parameter to the query //
that will lock only one record while
(rs1.next()) rowidInt rs1.getInt(1)
pstmt.setInt(1, rowidInt) rs2
pstmt.executeQuery()
34In Summary Combination ? - ? - ?
35In Summary Unlocked
-
6
4
-
5
36For More Information, go to
- PSDN
- Locking talks many Exchanges
- Developing Performance-Oriented ODBC/JDBC
OpenEdge Applications - OpenEdge SQL Authorization Explained
- Progress eLearning Community
- Using OpenEdge SQL
- Documentation
- 10.1C OpenEdge Data Management SQL Development
- 10.1C OpenEdge Data Management SQL Reference
37?
Questions
38Thank You
39(No Transcript)
40Row Locks
Lock Type Allows Blocks
No-lock read nothing
Share (S) read all updates
Exclusive (X) read, update all access by others
NO-LOCK means access without lock
41Row Lock Compatibility
Lock Request Type
42Table Lock Types
Lock Type Allow Block
Intent Share (IS) Row S Table X
Intent Exclusive (IX) Row X Table X
Shared (S) Row S Row X
Shared with Intent Exclusive (SIX) Row S, X other Row X
Exclusive (X) Row S, X any other access
43Table Lock Compatibility
Lock Request Type
IS IX S SIX X
None OK OK OK OK OK
IS OK OK OK - Q - - Q -
IX OK OK - Q - - Q - - Q -
S OK - Q - OK - Q - - Q -
SIX OK - Q - - Q - - Q - - Q -
X - Q - - Q - - Q - - Q - - Q -
Other Active Locks
44Basics of transactions - ACID
- Atomicity
- Consistency
- Isolation
- Durability