CSC 453 Database Systems Lecture

Slides:



Advertisements
Similar presentations
Transactions - Concurrent access & System failures - Properties of Transactions - Isolation Levels 4/13/2015Databases21.
Advertisements

1 Integrity Ioan Despi Transactions: transaction concept, transaction state implementation of atomicity and durability concurrent executions serializability,
TRANSACTION PROCESSING SYSTEM ROHIT KHOKHER. TRANSACTION RECOVERY TRANSACTION RECOVERY TRANSACTION STATES SERIALIZABILITY CONFLICT SERIALIZABILITY VIEW.
Transactions (Chapter ). What is it? Transaction - a logical unit of database processing Motivation - want consistent change of state in data Transactions.
Transaction Processing Lecture ACID 2 phase commit.
ACS-4902 R McFadyen 1 Chapter 17 Introduction to Transaction Processing Concepts and Theory 17.1, 17.2, 17.3, 17.5, 17.6.
Dec 15, 2003Murali Mani Transactions and Security B term 2004: lecture 17.
Cs3431 Transactions, Logging and Security. cs3431 Transactions: What and Why? A set of operations on a database must appear as one “unit”. Example: Consider.
Transaction Processing
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
Transaction Management WXES 2103 Database. Content What is transaction Transaction properties Transaction management with SQL Transaction log DBMS Transaction.
Managing Concurrency in Web Applications. DBI 2007 HUJI-CS 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses.
INTRODUCTION TO TRANSACTION PROCESSING CHAPTER 21 (6/E) CHAPTER 17 (5/E)
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
Transactions Sylvia Huang CS 157B. Transaction A transaction is a unit of program execution that accesses and possibly updates various data items. A transaction.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 10 Transaction Management.
DB Transactions CS143 Notes TRANSACTION: A sequence of SQL statements that are executed "together" as one unit:
Transactions1 Unit of work on a database. Transactions2 Transactions, concept Logical unit of work on the database –Examples Transfer money from bank.
TRANSACTIONS. Objectives Transaction Concept Transaction State Concurrent Executions Serializability Recoverability Implementation of Isolation Transaction.
1cs Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect.
1 Transactions. 2 Transaction Concept A transaction is a unit of program execution that accesses and possibly updates various data items. E.g. transaction.
Transaction processing Book, chapter 6.6. Problem: With a single user…. you run a query, you get the results, you run the next, etc. But database life.
1 Transactions Chapter Transactions A transaction is: a logical unit of work a sequence of steps to accomplish a single task Can have multiple.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 136 Database Systems I SQL Modifications and Transactions.
ICS 321 Fall 2011 The Database Language SQL (iv) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 10/26/20111Lipyeow.
Chapter 15: Transactions Loc Hoang CS 157B. Definition n A transaction is a discrete unit of work that must be completely processed or not processed at.
Giovanni Chierico | May 2012 | Дубна Data Concurrency, Consistency and Integrity.
Jennifer Widom Transactions Properties. Jennifer Widom Transactions Solution for both concurrency and failures A transaction is a sequence of one or more.
15.1 Transaction Concept A transaction is a unit of program execution that accesses and possibly updates various data items. E.g. transaction to transfer.
©Silberschatz, Korth and Sudarshan14.1Database System Concepts - 6 th Edition Chapter 14: Transactions Transaction Concept Transaction State Concurrent.
CM Name : p.rajesh Year/Semester : VI Semester Subject : Advanced database system Subject Code : CM-603 Topic : Advanced database concepts Duration.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
1 Advanced Database Concepts Transaction Management and Concurrency Control.
10 1 Chapter 10 - A Transaction Management Database Systems: Design, Implementation, and Management, Rob and Coronel.
1 Transaction Processing Case Study. 2 Interaksi Proses There is table Sells(shop,beverage,price), and suppose that Joe’s Shop sells only Juice for $2.50.
Transactions Introduction.
Managing Concurrency in Web Applications
Transactions and Concurrency Control
LAB: Web-scale Data Management on a Cloud
Database Management System
Isolation Levels Understanding Transaction Temper Tantrums
Transaction Processing
Transactions.
Transactions Isolation Levels.
Transactions Introduction.
March 21st – Transactions
Transactions Properties.
Transactions.
Chapter 15: Transactions
Interacting with Database
Transactions, Locking and Query Optimisation
Transactions Sylvia Huang CS 157B.
Chapter 10 Transaction Management and Concurrency Control
CSC 453 Database Systems Lecture
Chapter 14: Transactions
CSC 453 Database Systems Lecture
Transactions Isolation Levels.
Lecture 13: Transactions in SQL
Chapter 14: Transactions
Transactions and Concurrency
Chapter 14: Transactions
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Isolation Levels Understanding Transaction Temper Tantrums
UNIT -IV Transaction.
Lecture 05: SQL Systems Aspects
-Transactions in SQL -Constraints and Triggers
Advanced Topics: Indexes & Transactions
CSC 453 Database Systems Lecture
Lecture 11: Transactions in SQL
Presentation transcript:

CSC 453 Database Systems Lecture Tanu Malik College of CDM DePaul University

Final 2 hours 15 mins Comprehensive 2-sided A4 sheet Study guide will be posted Both descriptive and multiple-choice format

Practice Question 1

Practice Question 2 How many times will the loop run?

Practice Question 3 What is wrong with this code snippet?

Practice Question 4

Practice Question 5 Which of the following will open a cursor name cur_employee?

Basic Steps of Using JDBC Import the needed Java classes Download and load the JDBC driver Create a database connection Create one or more statements Issue one or more statements to the database (and work with the returned results, if any) Close the database connection

1. Importing Classes To import the needed Java classes: import java.sql.*; Includes DriverManager, Connection, Statement, ResultSet, ResultSetMetaData, etc….

2. Loading JDBC Driver To load the Oracle JDBC Driver: Download from http://www.oracle.com/technetwork/database/features/jdbc/jdbc-drivers-12c-download-1958347.html The exact Driver to download will depend on the version of Java you have installed on your machine.  Class.forName("oracle.jdbc.driver.OracleDriver"); CLASSPATH = ojdbc14.jar:.

3. Creating Database Connection To create database connection: String url = "jdbc:oracle:thin:@acadoradbprd01.dpu.depaul.edu:1521:ACADPRD0"; Connection c = DriverManager.getConnection("jdbc:oracle:thin:@acadoradbprd01.dpu.depaul.edu", “username”, “password”); This is where you specify the server name, port number, username, password

4. Creating Statements To create an empty, reusable statement: Statement s = c.createStatement(); Partially pre-compiled statements can be created with PreparedStatement p = c.prepareStatement(String partialStatement);

5. Issuing Statements to Database For non-query statements (returns an int): s.executeUpdate(statement); For queries (returns a ResultSet): s.executeQuery(query);

6. Closing Database Connection To close the connection and any associated statements and result sets: c.close();

Issuing SQL Statements For non-query SQL statements use method: int executeUpdate(String statement) Returns number of rows modified For SQL queries use method: ResultSet executeQuery(String query) Returned ResultSet contains result of query rasinsrv02.cstcis.cti.depaul.edu

ResultSet Methods boolean next(): Moves to next record, returns true if successful type getType(int i): Returns (String, int, or double) value in column i of current record also versions that take column names as input boolean wasNull(): Returns true if last call to getType resulted in a null value No way to indicate NULL otherwise void close() : Closes the ResultSet

Transactions Motivated by two independent requirements Concurrent database access Resilience to system failures

Transactions Concurrent Database Access DBMS Data More software Even more software Select… Update… Create Table… Drop Index… Help… Delete… More software DBMS Data

Transactions Concurrent Access: Attribute-level Inconsistency Select enrollment from College Where cName = ‘DePaul’ Update College Set enrollment = enrollment + 1000 Where cName = ‘DePaul’ concurrent with … C2 Select enrollment from College Where cName = ‘DePaul’ Update College Set enrollment = enrollment + 1500 Where cName = ‘DePaul’

Transactions: Flights Example Flights(fltNo,fltDate,seatNo,seatStatus) select seatNo from Flights Where fltNo= 123 and fltDate = ‘2008-12-25’ and seatStatus = ‘available’; Update Flights set seatStatus = ‘occuped’; Where fltNo= 123 and fltDate = ‘2008-12-25’ and seatNo = ’22A’; concurrent with … C2 select * select seatNo from Flights Where fltNo= 123 and fltDate = ‘2008-12-25’ and seatStatus = ‘available’; Update Flights set seatStatus = ‘occuped’; Where fltNo= 123 and fltDate = ‘2008-12-25’ and seatNo = ’22A’; Both modifying the apply record for the student id = 123

Transactions Concurrent Access: Tuple-level Inconsistency select * from Apply Where sID = 123 Update Apply Set major = ‘CS’ Where sID = 123 concurrent with … C2 select * from Apply Where sID = 123 Update Apply Set decision = ‘Y’ Where sID = 123 Both modifying the apply record for the student id = 123

Transactions Concurrent Access: Table-level Inconsistency Update Apply Set decision = ‘Y’ Where sID In (Select sID From Student Where GPA > 3.9) concurrent with … Update Student Set GPA = (1.1)  GPA Where sizeHS > 2500

Transactions Concurrent Access: Multi-statement inconsistency Insert Into Archive Select * From Apply Where decision = ‘N’; Delete From Apply Where decision = ‘N’; concurrent with … Select Count(*) From Apply; Select Count(*) From Archive;

Transactions Concurrency Goal Execute sequence of SQL statements so they appear to be running as a group or “in isolation” Simple solution: execute them in isolation But want to enable concurrency whenever safe to do so Database systems are geared toward performance. They typically operate in concurrent (multi- processor/multi-threaded/asynchronous I/O) environments. Clients may work on different parts of the DBMS

Transactions System Failure DBMS Data More software Select… Update… Create Table… Drop Index… Help… Delete… More software DBMS Data

Transactions Resilience to System Failures Bulk Load DBMS Data

Transactions Resilience to System Failures DBMS Data Insert Into Archive Select * From Apply Where decision = ‘N’; Delete From Apply Where decision = ‘N’; DBMS Data

Transactions Resilience to System Failures DBMS Lots of updates buffered in memory DBMS Data

Transactions System-Failure Goal Guarantee all-or-nothing execution, regardless of failures DBMS Data

Transactions Transactions Solution for both concurrency and failures A transaction is a sequence of one or more SQL operations treated as a unit Transactions appear to run in isolation If the system fails, each transaction’s changes are reflected either entirely or not at all Transactions

Transactions Transactions Solution for both concurrency and failures A transaction is a sequence of one or more SQL operations treated as a unit. SQL standard: Transaction begins with a Begin Transaction statement On “commit” transaction ends and new one begins Current transaction ends on session termination “Autocommit” turns each statement into transaction Transactions

Transactions Transactions Solution for both concurrency and failures A transaction is a sequence of one or more SQL operations treated as a unit Transactions appear to run in isolation If the system fails, each transaction’s changes are reflected either entirely or not at all Transactions

Transactions A Atomicity all-or-nothing 3 Every time a DBMS encounters a transaction, the DBMS software guarantees the following ACID Properties Meaning Order A Atomicity all-or-nothing 3 C Consistency consistent DB state 4 I Isolation appear to act in isolation 1 D Durability commits are persistent 2

Internal Representation of SQL statements Create Table X ( x int, y int , check (y = x) ) Select x from X Read(x) Select * from X Read(x);Read(Y) Insert into X values (5,5) Write(x);Write(y) Insert into X values (6,6) Write(x);Write(y) Update X set x = x*2 Read(x);Write(x) Update X set y = y*2 Read(y);Write(y)

SQL Statements Concurrent Access: Attribute-level Inconsistency Select enrollment from College Where cName = ‘DePaul’ Update College Set enrollment = enrollment + 1000 Where cName = ‘DePaul’ concurrent with … Select enrollment from College Where cName = ‘DePaul’ Update College Set enrollment = enrollment + 1500 Where cName = ‘DePaul’

Transactions: Internal Representation of SQL statements READ(enrollment, cName) WRITE(enrollment) concurrent with … C2 READ(enrollment, cName) WRITE(enrollment)

Read/Write Temporary variables are visible only within a transaction Read implies read into memory from disk Subsequent reads (across all transactions) are from memory Write implies write into memory (across all txns) Often an attribute is written from a temporary variable Temporary variables are visible only within a transaction Commit implies flush to disk

Transactions: Durability Begin Transaction READ(enrollment, cName) enrollment gets updated here WRITE(enrollment) Commit concurrent with … C2 Begin Transaction READ(enrollment, cName) WRITE(enrollment) Commit

Transactions: Atomicity (all or nothing) Begin Transaction READ(enrollment, cName) enrollment gets updated here WRITE(enrollment) Commit

Transactions: Consistency Create Table X ( x int, y int , check (y = x) ) Insert into X values (5,5) W(x);W(y) Insert into X values (6,6) W(x);W(y) Select x from X R(x) Select * from X R(x);R(Y) Update X set x = x*2 R(x);W(x) Update X set y = y*2 R(y);W(y)

Transactions: Consistency Create Table X ( x int, y int , check (y >= x) ) Insert into X values (5,5); W(x);W(y) Insert into X values (6,6); W(x);W(y) Select x from X R(x) Select * from X R(x);R(Y) Update X set y = x+y; Update X set x = x+y;

Transactions Transaction Rollback (= Abort) Undoes partial effects of transaction Can be system- or client-initiated Each transaction is “all-or-nothing,” never left half done Begin Transaction; <get input from user> SQL commands based on input <confirm results with user> If ans=‘ok’ Then Commit; Else Rollback;

Transactions . . . DBMS (ACID Properties) Isolation Serializability . . . Serializability Operations may be interleaved, but execution must be equivalent to some sequential (serial) order of all transactions DBMS Data

Serializability Basic Assumption– Each transaction preserves database consistency. Thus, serial execution of a set of transactions preserves database consistency. A (possibly concurrent) schedule is serializable if it is equivalent to a serial schedule.

Schedules Schedules– a sequences of instructions that specify the chronological order in which instructions of concurrent transactions are executed A schedule for a set of transactions must consist of all instructions of those transactions Must preserve the order in which the instructions appear in each individual transaction. A transaction that successfully completes its execution will have a commit instructions as the last statement A transaction that fails to successfully complete its execution will have an abort instruction as the last statement

Schedule 1 Let T1 transfer $50 from A to B, and T2 transfer 10% of the balance from A to B. An example of a serial schedule in which T1 is followed by T2 :

Schedule 2 A serial schedule in which T2 is followed by T1 :

Schedule 3 Let T1 and T2 be the transactions defined previously. The following schedule is not a serial schedule, but it is equivalent to Schedule 1. commit Note -- In schedules 1, 2 and 3, the sum “A + B” is preserved.

Schedule 4 The following concurrent schedule does not preserve the sum of “A + B” commit

Transactions  Overhead  Concurrency  Consistency Guarantees . . . (ACID Properties) Isolation Weaker “Isolation Levels” Read Uncommitted Read Committed Repeatable Read . . . Strongest “Isolation Levels” Serilizable order DBMS  Overhead  Concurrency Data  Consistency Guarantees

Transactions Set Transaction Isolation Level Read Uncommitted Begin Transaction Select GPA from Student Where sizeHS > 2500 Update Student Set GPA = (1.1)  GPA Where sizeHS > 2500 Commit concurrent with … Set Transaction Isolation Level Repeatable Read Begin Transaction Select Avg(GPA) From Student Commit

Transactions . . . Isolation Levels DBMS Per transaction “In the eye of the beholder” Affect applies to read statements . . . My transaction is Read Uncommitted My transaction is Repeatable Read DBMS Data

Transactions Dirty Reads “Dirty” data item: written by an uncommitted transaction Select enrollment from College Where cName = ‘DePaul’ Update College Set enrollment = enrollment + 1000 Where cName = ‘DePaul’ concurrent with … Enrollment is dirty over here. It is updated by the update stmt, but not committed by the txn. Select Avg(enrollment) From College

Transactions Dirty Reads “Dirty” data item: written by an uncommitted transaction Update Student Set GPA = (1.1)  GPA Where sizeHS > 2500 concurrent with … Select GPA From Student Where sID = 123 concurrent with … Update Student Set sizeHS = 2600 Where sID = 234

Transactions Isolation Level Read Uncommitted A transaction may perform dirty reads Select GPA from Student Where sizeHS > 2500 Update Student Set GPA = (1.1)  GPA Where sizeHS > 2500 concurrent with … Set Transaction Isolation Level Read Uncommitted; Select Avg(GPA) From Student;

Transactions Isolation Level Read Committed A transaction will not perform dirty reads Only reads commited values of other transactions Still does not guarantee global serializability Select GPA From Student Where sizeHS > 2500 Update Student Set GPA = (1.1)  GPA Where sizeHS > 2500 concurrent with … Set Transaction Isolation Level Read Committed; Select Avg(GPA) From Student; Select Max(GPA) From Student;

Transactions Isolation Level Repeatable Read A transaction may not perform dirty reads An item read multiple times cannot change value Still does not guarantee global serializability Update Student Set GPA = (1.1)  GPA; Update Student Set sizeHS = 1500 Where sID = 123; concurrent with … Set Transaction Isolation Level Repeatable Read; Select Avg(GPA) From Student; Select Avg(sizeHS) From Student;

Transactions Isolation Level Repeatable Read A transaction may not perform dirty reads An item read multiple times cannot change value But a relation can change: “phantom” tuples Insert Into Student [ 100 new tuples ] concurrent with … Set Transaction Isolation Level Repeatable Read; Select Avg(GPA) From Student; Select Max(GPA) From Student;

Transactions Isolation Level Repeatable Read A transaction may not perform dirty reads An item read multiple times cannot change value But a relation can change: “phantom” tuples Delete From Student [ 100 tuples ] concurrent with … Set Transaction Isolation Level Repeatable Read; Select Avg(GPA) From Student; Select Max(GPA) From Student;

Transactions Read Only transactions Helps system optimize performance Independent of isolation level Set Transaction Read Only; Set Transaction Isolation Level Repeatable Read; Select Avg(GPA) From Student; Select Max(GPA) From Student;

Transactions Serializable Strongest isolation level SQL Default Read Uncommitted A data item is dirty if it is written by an uncommitted transaction. Problem of reading dirty data written by another uncommitted transaction: what if that transaction eventually aborts?

Transactions Read Committed Cannot read dirty data written by other uncommitted transactions. But read-committed is still not necessarily serializable Repeatable Read If a tuple is read once, then the same tuple must be retrieved again if query is repeated. Still not serilizable; may see phantom tuples—tuples inserted by other concurrent transactions.

Transactions Isolation Levels: Summary dirty reads nonrepeatable reads phantoms Read Uncommitted Read Committed Repeatable Read Serializable

Transactions Isolation Levels: Summary Weaker isolation levels Standard default: Serializable Weaker isolation levels Increased concurrency + decreased overhead = increased performance Weaker consistency guarantees Some systems have default Repeatable Read Isolation level per transaction and “eye of the beholder” Each transaction’s reads must conform to its isolation level

Example Consider a relation R(A) containing {(5),(6)} and two transactions: T1: Update R set A = A+1; T2: Update R set A = 2*A. Suppose both transactions are submitted under the isolation and atomicity properties. Which of the following is NOT a possible final state of R? (10,12);(11,13);(11,12);(12,14)

Example Consider a table R(A) containing {(1),(2)} and two transactions: T1: Update R set A = 2*A; T2: Select avg(A) from R. If transaction T2 executes using "read uncommitted", what are the possible values it returns?

Example Consider tables R(A) and S(B), both containing {(1),(2)}. T1: Update R set A = 2*A; update S set B = 2*B T2: Select avg(A) from R; select avg(B) from S. If transaction T2 executes using "read committed", is it possible for T2 to return two different values?

Example 5 Consider table R(A) containing {(1),(2)}. T1:Update T set A=2*A; insert into R values (6); T2: Select avg(A) from R; select avg(A) from R; If transaction T2 executes using "repeatable read", what are the possible values returned by its SECOND statement?