Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 453 Database Systems Lecture

Similar presentations


Presentation on theme: "CSC 453 Database Systems Lecture"— Presentation transcript:

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

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

3 Practice Question 1

4 Practice Question 2 How many times will the loop run?

5 Practice Question 3 What is wrong with this code snippet?

6 Practice Question 4

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

8 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

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

10 2. Loading JDBC Driver To load the Oracle JDBC Driver:
Download from 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:.

11 3. Creating Database Connection
To create database connection: String url = Connection c = “username”, “password”); This is where you specify the server name, port number, username, password

12 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);

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

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

15 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

16 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

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

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

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

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

21 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

22 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

23 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;

24 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

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

26 Transactions Resilience to System Failures Bulk Load DBMS Data

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

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

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

30 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

31 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

32 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

33 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

34 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)

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

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

37 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

38 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

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

40 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)

41 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;

42 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;

43 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

44 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.

45 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

46 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 :

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

48 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.

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

50 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

51 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

52 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

53 Transactions Dirty Reads
“Dirty” data item: written by an uncommitted transaction Select enrollment from College Where cName = ‘DePaul’ Update College Set enrollment = enrollment 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

54 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

55 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;

56 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;

57 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;

58 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;

59 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;

60 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;

61 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?

62 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.

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

64 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

65 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)

66 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?

67 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?

68 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?


Download ppt "CSC 453 Database Systems Lecture"

Similar presentations


Ads by Google