DB2UDB_the_Basics Day 4

22
IBM Software Group © 2005 IBM Corporation DB2 UDB Intermediate Day4

Transcript of DB2UDB_the_Basics Day 4

IBM Software Group

© 2005 IBM Corporation

DB2 UDB Intermediate

Day4

IBM Software Group

© 2005 IBM Corporation

Topics

Locks Deadlocks Snapshots Transaction Logs

2

IBM Software Group

© 2005 IBM Corporation

Locks

We need to establish or define some rules and locking semantics to guarantee data integrity in case more than one user tries to update the same row of data in a multi-user database environment. DB2 uses four isolation levels to support different levels of concurrency.

• Uncommitted Read

• Cursor Stability

• Read Stability

• Repeatable Read

3

IBM Software Group

© 2005 IBM Corporation

Uncommitted Read

4

IBM Software Group

© 2005 IBM Corporation

Cursor Stability (Default Isolation Level)

5

IBM Software Group

© 2005 IBM Corporation

Read Stability Isolation Level

6

IBM Software Group

© 2005 IBM Corporation

Repeatable Read Isolation Level

7

IBM Software Group

© 2005 IBM Corporation

Changing Isolation Level

>>-SET--+---CURRENT------+--ISOLATION--+---+--+-UR----+---------------><

+-CS----+ +-RR----+ +-RS----+ '-RESET-‘

VALUES CURRENT ISOLATION

8

IBM Software Group

© 2005 IBM Corporation

Important Points

It is important to understand that changes to this DB2 register affect only the current session.

Subsequent dynamic SQL statements executed in this session will use this isolation level. The change only applies for dynamic SQL statements. For static SQL statements or packages, you can control the isolation level through the DB2 bind command

9

IBM Software Group

© 2005 IBM Corporation

Using the DB2PRECOMPILE and BIND command

To execute an SQL statement, it must be compiled into an executable form that DB2 understands. This executable form of the statement is known as the data access plan. Data access plans are stored in database objects called packages.

precompile appfile.sqc isolation RR

bind bindfilename.bnd isolation RR

SELECT pkgschema, pkgname, isolation FROM syscat.packages

10

IBM Software Group

© 2005 IBM Corporation

Isolation at statement level

SELECT empno, lastname, firstnme FROM employee WHERE deptno=’A01’ WITH UR USE AND KEEP EXCLUSIVE LOCKS

'-WITH--+-RR--+---------------------+-+-‘ | '-lock-request-clause-' | +-RS--+---------------------+-+ | '-lock-request-clause-' | +-CS--------------------------+ '-UR--------------------------‘

lock-request-clause: >>-USE AND KEEP--+-UPDATE----+--LOCKS-------------------------->< '-EXCLUSIVE-'

11

IBM Software Group

© 2005 IBM Corporation

DB2 Locking

DB2 uses various levels of locking to provide concurrent data access and at the same time protect the data. Depending on the operations requested, the database manager can acquire locks on table rows, table blocks, tables, table spaces, buffer pools, and databases. Locks are acquired implicitly by DB2 according to the semantics defined by the isolation level.

12

IBM Software Group

© 2005 IBM Corporation

Lock Modes

During normal data manipulation processing, DB2 uses row-level locking by default. You can override this rule to acquire table-level locking instead.

ALTER TABLE employee LOCKSIZE TABLE

ALTER TABLE employee LOCKSIZE ROW

QUIESCE TABLESPACES FOR TABLE employee EXCLUSIVE

LOCK TABLE--+-table-name-+--IN--+-SHARE-----+--MODE---------><

'-nickname---' '-EXCLUSIVE-'

13

IBM Software Group

© 2005 IBM Corporation

Lock Waits

It is generally not possible to totally avoid lock wait as concurrency increases. After all, DB2 relies on the locking mechanism to keep data integrity. However, you should minimize lock waits and each wait length as much as possible. They put a hold on processing the statements, hence, they affect performance.

Db2 get db cfg | grep –I locktimeout LOCKTIMEOUT = -1 Values CURRENT LOCK TIMEOUT If an application reaches the LOCKTIMEOUT value, it receives the

following message:

14

IBM Software Group

© 2005 IBM Corporation

SQL0911N The current transaction has been rolled back because of a deadlock or timeout. Reason code "68". Locktimeout

SQL0911N The current transaction has been rolled back because of a deadlock or timeout. Reason code "2". Deadlock

15

IBM Software Group

© 2005 IBM Corporation

DeadLock

16

IBM Software Group

© 2005 IBM Corporation

Lock Escalation

As the number of locks being held by all applications connected to the database increases, it is possible that the locklist will get full. When this happens, DB2 attempts to free memory by allocating a table lock and releasing the row locks. This internal operation is called lock escalation.

THIS is being tuned by two dbcfg parameters.

1. LockList: indicate the maximum storage allowed for locks in each database.

2. MAXLOCKS: : defines the percentage of the total locklist permitted to be allocated to a single application.

17

IBM Software Group

© 2005 IBM Corporation

Snapshot Monitor

You can use the Snapshot Monitor to capture information about a database and any connected applications at a specific time. Snapshot monitoring provides the majority of the useful information for dealing with lock issues.

db2 update monitor switches using bufferpool on lock on sort on statement on table on timestamp on uow on

db2 get snapshot for all on database_name

18

IBM Software Group

© 2005 IBM Corporation

Database snapshot* Buffer pool snapshot Dynamic SQL snapshot Application snapshot* Table space snapshot Database lock snapshot* Table snapshot

19

IBM Software Group

© 2005 IBM Corporation

Logging

DB2 supports 2 Logging methods.

• Circular Logging(Default)

• Archival Logging

20

IBM Software Group

© 2005 IBM Corporation

Circular Logging

As the name suggests, in this method the logs are reused in a circular mode. For example, if you have three primary logs, DB2 uses them in this order: Log #1, Log #2, Log #3, Log #1, Log #2….

Note that in the above sequence Log #1 and Log #2 are reused. When a log file is reused, its previous contents are completely overwritten. Therefore, a log can be reused if and only if the transactions it contains have already been committed or rolled back and externalized to the disk.

Logprimary, Logsecond db2 get db cfg |grep –i logprimary Db2 get db cfg | grep –I logsecond

21

IBM Software Group

© 2005 IBM Corporation

Archival Logging

Archival logging keeps the log files even after they contain committed and externalized data. To enable archival logging, you can change the value of the LOGARCHMETH1 database configuration parameter.

db2 get db cfg | grep –I LOGARCHMETH1 db2 update db cfg using LOGARCHMETH1

disk:/home/db2inst1/archlog

22