oca 09 - managing data and concurrency

Upload: muhammad-asghar-khan

Post on 04-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    1/34

    09 - Managing Data and Concurrency

    By Muhammad Asghar Khan

    Reference: OCA Oracle Database 11g - Admin I Exam Guide by John Watson

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    2/34

    Agenda

    http://asghars.blogspot.com2

    Manage Data Using DML Atomicity

    Consistency

    Isolation

    Durable Execution of DML Statement

    Transaction Control

    Rollback

    Commit EXERCISE 09-1: Manage Data Using DML

    Identify and Administer PL/SQL Objects

    Procedures and Functions

    1/2

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    3/34

    Agenda

    http://asghars.blogspot.com3

    Packages Database Triggers

    Monitor and Resolve Locking Conflicts

    Exclusive Lock

    Shared Lock

    EXERCISE 09-2: Detect and Resolve Lock Contention

    2/2

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    4/34

    Manage Data Using DML

    http://asghars.blogspot.com4

    DML commands change data in tables, they willalso change data in indexes, but this is automatic

    RDBMS group one or more DML statements intotransactions

    Any RDBMS must be able to pass the ACID test: itmust guarantee atomicity, consistency, isolation,and durability

    Atomicity

    Principle of atomicity states that either all parts oftransaction must complete, or none of themcomplete

    1/11

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    5/34

    Manage Data Using DML

    http://asghars.blogspot.com5

    The rollback of an incomplete transaction is thereversal process

    Oracle guarantees atomicity through the use of undo

    segments

    Consistency

    The principle of consistency states that the results of

    a query must be consistent with the state of the

    database at the time the query started

    Through the use of undo segments Oracle guarantees

    that if a query succeeds, the result will be consistent

    2/11

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    6/34

    Manage Data Using DML

    http://asghars.blogspot.com6

    If your undo segments are incorrectly configured, thequery may not succeed: there is a famous Oracleerror, ORA-1555 snapshot too old, that is raised

    Isolation

    The principle of isolation states that an incomplete(that is, uncommitted) transaction must be invisibleto the rest of the world

    Transaction isolation requires that the database must

    conceal transactions in progress from other users Oracle guarantees transaction isolation through use

    of undo segments

    3/11

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    7/34

    Manage Data Using DML

    http://asghars.blogspot.com7

    Durable

    The principle of durability states that once a transactioncompletes, it must be impossible for the database to loseit

    Oracle fulfils this requirement through the use of log files(online and archived redo log)

    Execution of DML Statement

    For any DML operation, it is necessary to work on bothdata blocks and undo blocks, and also to generate redo:

    the A, C, and I of the ACID test require generation ofundo; the D requires generation of redo

    Redo protects all block changes while undo segment isjust another segment

    4/11

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    8/34

    Manage Data Using DML

    http://asghars.blogspot.com8

    Execution of DML statements involve the followingsteps:

    1. An empty block or required blocks are checked inthe database buffer cache, or copied into thedatabase buffer cache from the datafiles

    2. An empty or expired block of an undo segment isselected

    1. For INSERT & UPDATE only a rowid is written to theundo block

    2. For a DELETE, the whole row (which might be severalkilobytes) must be written to the undo block

    3. Locks are placed on rows and associated index keysthat are going to be affected by the operation

    5/11

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    9/34

    Manage Data Using DML

    http://asghars.blogspot.com9

    4. Then the redo is generated

    The server process writes to the log buffer the changevectors that are going to be applied to the data blocks

    Generation of redo is applied both to table blockchanges and to undo block changes

    5. At last the DML statement is carried out in thedatabase buffer cache

    Transaction Control

    Rollback If anything goes wrong, rollback of transactions in

    progress is completely automatic and is carried out bybackground processes

    6/11

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    10/34

    Manage Data Using DML

    http://asghars.blogspot.com10

    A manual rollback requires the user to issue the

    ROLLBACK command

    In the case of an UPDATE, the pre-update versions of the

    columns, as stored in the block of undo segment, are used

    to construct another UPDATE command that will set the

    columns of the row in the table block back to their originalvalues

    In case of INSERT Oracle retrieves the rowid of the inserted

    row from the undo block and uses it as the key for a DELETE

    statement on the table In case of DELETE, Oracle constructs a complete INSERT

    statement from the data in the undo block

    Then Oracle will issue a COMMIT that will commit both the

    original change and the rollback change, as one transaction

    7/11

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    11/34

    Manage Data Using DML

    http://asghars.blogspot.com11

    A rollback will itself generate more redo as it executes,

    perhaps rather more than the original statement

    Commit

    A COMMIT involves nothing more than flushing the log

    buffer to disk, and flagging the transaction as complete DBWn does absolutely nothing during the commit

    When you say COMMIT, LGWR actually does write in real

    time: your session will hang until the write is complete

    The change vectors written to the redo log are all thechange vectors: those applied to data blocks (tables and

    indexes) and those applied to undo segments

    8/11

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    12/34

    Manage Data Using DML

    http://asghars.blogspot.com12

    DBWn writes only a few dirty buffers to disk; when acheckpoint is signaled, it writes all dirty buffers todisk

    So in principle, your database on disk is corrupted:the datafiles may well be storing uncommitted work,

    and be missing committed changes

    The COMMIT and ROLLBACK statements does notapply to DDL

    When you create a table, you are in fact doing atransaction against data dictionary tables: likeSYS.TAB$ and SYS.COL$ and then the commandconcludes with a COMMIT

    9/11

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    13/34

    Manage Data Using DML

    http://asghars.blogspot.com13

    Therefore; executing one or more DML commandsfollowed by a DDL command will commit the wholelot: the DML statements as well as the DDLstatement, this is called auto-commit or implicit-commit

    Another situation when auto-commit is happened,On Windows when you exit (EXIT) from a userprocess such as SQL*Plus

    This is because built into the SQL*Plus EXITcommand there is a COMMIT statement

    If you click in the top-right corner of the SQL*Pluswindow, the transaction will be rollback

    10/11

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    14/34

    Manage Data Using DML

    http://asghars.blogspot.com14

    The behavior of SQL*Plus on other platforms maywell be different; the only way to be sure is to test it

    There is a SQL*Plus command SET AUTOCOMMIT

    ON

    11/11

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    15/34

    EXERCISE 09-1: Manage Data Using DML

    http://asghars.blogspot.com15

    Open two SQL*Plus sessions and connect asSYSTEM

    Step 1st Session 2nd Session

    1

    2

    Results are the same

    1/4

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    16/34

    EXERCISE 09-1: Manage Data Using DML

    http://asghars.blogspot.com16

    Step 1st Session 2nd Session

    3

    4

    Results differ because transaction isolation conceals the changes

    5

    2/4

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    17/34

    EXERCISE 09-1: Manage Data Using DML

    http://asghars.blogspot.com17

    Step 1st Session 2nd Session

    6

    Results are the same in both sessions

    7

    8

    3/4

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    18/34

    EXERCISE 09-1: Manage Data Using DML

    http://asghars.blogspot.com18

    Step 1st Session 2nd Session

    9

    10

    11

    12

    Oh The DDL statement committed the DELETE, so it cant be rolled back.

    4/4

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    19/34

    Identify and Administer PL/SQL Objects

    http://asghars.blogspot.com19

    PL/SQL is Oracles proprietary 3GL that runs withinthe database

    You can use it to retrieve and manipulate data withSQL, while using procedural constructs such as

    IF...THEN...ELSE or FOR or WHILE The PL/SQL code can be stored on a client machine

    and sent to the server for execution, or it can bestored within the database as a named block of

    code Code stored remotely, or ad hoc code issued at the

    SQL*Plus prompt, is anonymous PL/SQL

    1/7

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    20/34

    Identify and Administer PL/SQL Objects

    http://asghars.blogspot.com20

    Anonymous PL/SQL is less efficient than storedPL/SQL and also causes problems with source code

    management, as the code may be distributed

    across many machines

    Some of the commonly used PL/SQL objects are:

    Procedures and Functions

    A procedure is a block of code that carries out some

    action. It can, optionally, be defined with a number ofarguments

    2/7

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    21/34

    Identify and Administer PL/SQL Objects

    http://asghars.blogspot.com21

    The arguments can be IN arguments, meaning thatthey are used to pass data into the procedure, or OUTarguments, meaning that they are modified by theprocedure and after execution the new values arepassed out of the procedure

    Arguments can also be IN-OUT, where the onevariable serves both purposes

    To run a procedure, either call it from within a PL/SQLblock or use the interactive EXECUTE command

    A function is similar in concept to a procedure, but itdoes not have OUT arguments and cannot be invokedwith EXECUTE. It returns a single value, with theRETURN statement

    3/7

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    22/34

    Identify and Administer PL/SQL Objects

    http://asghars.blogspot.com22

    Functions are generally used for relatively simpleoperations

    Following code creates and invoke a procedure

    4/7

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    23/34

    Identify and Administer PL/SQL Objects

    http://asghars.blogspot.com23

    Packages Packages group related procedures and functions

    together

    A package consists of two objects: a specification and

    a body

    A package specification lists the functions and

    procedures in the package. It can also define variables

    and constants accessible to all the procedures and

    functions in the package

    The package body contains the PL/SQL code that

    implements the package

    5/7

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    24/34

    Identify and Administer PL/SQL Objects

    http://asghars.blogspot.com24

    To create a package specification, use the CREATEPACKAGE command

    To invoke a packaged procedure, you must prefix theprocedure name with the package name

    Database Triggers

    Database triggers are a special category of PL/SQL objectwhich runs (or fires) automatically, when a particularaction is carried out, or a certain situation arises

    6/7

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    25/34

    Identify and Administer PL/SQL Objects

    http://asghars.blogspot.com25

    Some of the common triggers are; DML triggers, DDL

    triggers, Database operation (SERVERERROR,LOGON,

    LOGOFF,STARTUP,SHUTDOWN,SUSPEND)

    There are numerous uses for triggers:

    Auditing users actions Executing complex edits

    Security

    Enforcing complex constraints

    It is impossible to run a trigger by any means other

    than its triggering event

    7/7

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    26/34

    Monitor and Resolve Locking Conflicts

    http://asghars.blogspot.com26

    Serialization of concurrent access is accomplishedby record and table locking mechanisms

    Locking in an Oracle database is completelyautomatic

    Problems only arise if software tries to interferewith the automatic locking mechanism with poorlywritten code

    Exclusive Lock

    In exclusive lock the first session to request the lockon the row/table gets it, and any other sessionsrequesting write access must wait

    1/5

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    27/34

    Monitor and Resolve Locking Conflicts

    http://asghars.blogspot.com27

    The lock is held until the transaction completes

    Shared Lock

    Shared lock can be taken on the same object by many

    sessions

    Shared locks are taken on whole tables, it would not

    make any sense to take a shared lock on one row

    The purpose of taking a shared lock on a table is to

    prevent another session acquiring an exclusive lockon the table

    Exclusive locks on tables are only required to execute

    DDL statements

    2/5

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    28/34

    Monitor and Resolve Locking Conflicts

    http://asghars.blogspot.com28

    To execute DML on rows, a session must acquire

    exclusive locks on the rows to be changed, and shared

    locks on the tables containing the rows

    The exclusive lock prevents another session from

    interfering with the row, and the shared lockprevents another session from changing the table

    definition with a DDL statement

    Requests for locks are queued Lock contention arises when a session requests a

    lock on a row or object and cannot get it

    3/5

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    29/34

    Monitor and Resolve Locking Conflicts

    http://asghars.blogspot.com29

    The causes of lock contention may be the nature ofthe business, a user updates a row and then doesnot commit the change

    To detecting and resolve lock contention, Database

    home pagePerformance tabAdditionalMonitoring Links sectionInstance Locks link

    Blocking locks are the locks that are causingsessions to hang

    To terminate a session, either use Database Control,or theALTER SYSTEM KILL SESSIONcommand

    4/5

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    30/34

    Monitor and Resolve Locking Conflicts

    http://asghars.blogspot.com30

    A special case of lock contention is the deadlock Deadlock is a position where two sessions block

    each other in such a fashion that both will hang,

    each waiting for the other to release its lock

    Deadlocks are not the DBAs problem; they are

    caused by bad program design and resolved

    automatically by the database itself

    Information regarding deadlocks is written out tothe alert log, with full details in a trace file

    5/5

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    31/34

    EXERCISE 09-2: Detect and Resolve Lock

    Contention

    http://asghars.blogspot.com31

    1.

    Using SQL*Plus, connect to database in twosessions as user SYSTEM

    2. Create a table to be used for this exercise

    3. In your first session, lock all the rows in the

    INTEGERS table

    1/4

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    32/34

    EXERCISE 09-2: Detect and Resolve Lock

    Contention

    http://asghars.blogspot.com32

    4.

    In your second session, attempt to update a row.The session will hang

    5. Connect to your database as user SYSTEM with

    database control

    6. Navigate to the Performance tab from the

    database home page, and then the Database

    Locks link in the Additional Monitoring Linkssection

    2/4

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    33/34

    EXERCISE 09-2: Detect and Resolve Lock

    Contention

    http://asghars.blogspot.com33

    7. Observe that the second SYSTEM session is shownas waiting for an EXCLUSIVE lock. Select the radiobutton for the first blocking session and click KillSession

    8. In the confirmation window, click Show SQL, ClickReturn and Yes to execute the KILL SESSIONcommand

    3/4

    4/4

  • 7/29/2019 OCA 09 - Managing Data and Concurrency

    34/34

    EXERCISE 09-2: Detect and Resolve Lock

    Contention

    http://asghars blogspot com34

    9.

    Returning to your SQL*Plus sessions, you will findthat the second session is now working, but that

    the first session can no longer run any commands

    4/4