440 likes | 894 Views
OPS-27: Understanding Record and Table Locking In OpenEdge ® SQL Applications. Brian Werne. Sr. Engineering Manager, OpenEdge SQL and OpenEdge Management. Goals. What if you could: Improve concurrency? Avoid a lock table overflow? Increase throughput?
E N D
OPS-27: Understanding Record and Table Locking In OpenEdge® SQL Applications Brian Werne Sr. Engineering Manager, OpenEdge SQL and OpenEdge Management
Goals What if you could: • Improve concurrency? • Avoid a lock table overflow? • Increase throughput? • Deal with potential lock contention errors? OPS-27: Record and Table Locking In OpenEdge SQL Apps
Review of locking model and definitions • Available SQL features to influence locking • Examples on lock contention strategies Unlock the facts: Combination ? - ? - ? OPS-27: Record and Table Locking In OpenEdge SQL Apps
Basics of Locking Grades of Locks • Schema lock • Table lock • Record lock OPS-27: Record and Table Locking In OpenEdge SQL Apps
Basics 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 OPS-27: Record and Table Locking In OpenEdge SQL Apps
X S S S SQ S XQ SQ SQ SQ SQ SQ How locking conflicts can occur The Lock Table (simplified) Active & Queued (Waiting) Lock Entries Hash Table OPS-27: Record and Table Locking In OpenEdge SQL Apps
Where to monitor locks? PROMON Option 4: Record Locking Table OPS-27: Record and Table Locking In OpenEdge SQL Apps
Where to monitor locks? OpenEdge Management Locks and Latches page or VST _LockReq OPS-27: Record and Table Locking In OpenEdge SQL Apps
Unlock the facts: Combination ? - ? - ? • Review of locking model and definitions • Available SQL features to influence locking • Examples on lock contention strategies OPS-27: Record and Table Locking In OpenEdge SQL Apps
Basics of transactions Locking protocols • Transaction scope - No locks allowed outside a SQL transaction • Isolation level - Implicit lock strength depends on SQL isolation level setting OPS-27: Record and Table Locking In OpenEdge SQL Apps
Basics of Transactions Transaction scope • Auto-commit - true • con.setAutoCommit(true); • Explicit commit/rollback • con.commit(); OR • con.rollback(); OPS-27: Record and Table Locking In OpenEdge SQL Apps
Isolation Level Affect on Lock Type OPS-27: Record and Table Locking In OpenEdge SQL Apps
Basics of Transactions The Basics • Setting Isolation level to affect “transaction strength” con.setTransactionIsolation( Connection.TRANSACTION_READ_COMMITTED); OPS-27: Record and Table Locking In OpenEdge SQL Apps
ODBC – Setting Isolation Level OPS-27: Record and Table Locking In OpenEdge SQL Apps
Strategies 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 ; OPS-27: Record and Table Locking In OpenEdge SQL Apps
Strategies Beyond The Basics – increasing throughput • Select for Update: Similar to Repeatable Read ( High throughput, ‘better’ concurrency) Select id, name from PUB.Standings where balance < 100 FOR UPDATE; OPS-27: Record and Table Locking In OpenEdge SQL Apps
Locking Contention or Overflow Avoiding lock table overflow error? OPS-27: Record and Table Locking In OpenEdge SQL Apps
Strategies 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 ** OPS-27: Record and Table Locking In OpenEdge SQL Apps
Strategies Measuring and tuning the Basics • Query Plan select substring(“_Description”,1,80) from pub.”_Sql_Qplan”… • Using NoExecute to help query tuning Select name from pub.customer where countryName != ‘USA’ NOEXECUTE OPS-27: Record and Table Locking In OpenEdge SQL Apps
Beyond 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; OPS-27: Record and Table Locking In OpenEdge SQL Apps
Examples on lock contention strategies Unlock the facts: Combination ? - ? - ? • Review of locking model and definitions • Available SQL features to influence locking OPS-27: Record and Table Locking In OpenEdge SQL Apps
Locking Contention Still getting lock contention errors? OPS-27: Record and Table Locking In OpenEdge SQL Apps
Beyond 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 OPS-27: Record and Table Locking In OpenEdge SQL Apps
Locking Contention – Read Retry Isolation level = Read Committed • Encounter lock error: -210015 • Try-catch block to capture lock error • Retry fetching the record OPS-27: Record and Table Locking In OpenEdge SQL Apps
Locking 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) { OPS-27: Record and Table Locking In OpenEdge SQL Apps
Locking 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 OPS-27: Record and Table Locking In OpenEdge SQL Apps
Locking Contention • Make use of locking hints: SELECT * from PUB.Status WITH (…) ; OPS-27: Record and Table Locking In OpenEdge SQL Apps
Locking 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 OPS-27: Record and Table Locking In OpenEdge SQL Apps
Locking 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)); } OPS-27: Record and Table Locking In OpenEdge SQL Apps
Locking 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("Customer Name: " + rs2.getString(1)); } OPS-27: Record and Table Locking In OpenEdge SQL Apps
Locking Contention – NoLock and Rowid Isolation level = Read Committed • Fetch initial record using NoLock locking hint • Using ROWID: re-fetch record later with appropriate lock OPS-27: Record and Table Locking In OpenEdge SQL Apps
Locking 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 = ?"; OPS-27: Record and Table Locking In OpenEdge SQL Apps
Locking 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(); … OPS-27: Record and Table Locking In OpenEdge SQL Apps
- 4 6 - 5 • Definitions • SQL features that affect locking • Examples on handling lock contention In Summary : Combination ? - ? - ? OPS-27: Record and Table Locking In OpenEdge SQL Apps
In Summary : Unlocked - 4 6 - 5 OPS-27: Record and Table Locking In OpenEdge SQL Apps
For 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 OPS-27: Record and Table Locking In OpenEdge SQL Apps
? Questions OPS-27: Record and Table Locking In OpenEdge SQL Apps
Thank You OPS-27: Record and Table Locking In OpenEdge SQL Apps
Row Locks * NO-LOCK means “access without lock” OPS-27: Record and Table Locking In OpenEdge SQL Apps
S X UpgX Other Active Locks S OK - Q - - Q - X - Q - - Q - - Q - QS/QE - Q - - Q - - Q - Row Lock Compatibility Lock Request Type OPS-27: Record and Table Locking In OpenEdge SQL Apps
Table Lock Types OPS-27: Record and Table Locking In OpenEdge SQL Apps
Table Lock Compatibility Lock Request Type Other Active Locks OPS-27: Record and Table Locking In OpenEdge SQL Apps
Basics of transactions - ACID • Atomicity • Consistency • Isolation • Durability OPS-27: Record and Table Locking In OpenEdge SQL Apps