Instructor: Craig Duckett. Lecture 07: Tuesday, April 17 th, 2018 Conflicts and Isolation, MySQL Workbench

Similar documents
Instructor: Craig Duckett. Lecture 03: Tuesday, April 3, 2018 SQL Sorting, Aggregates and Joining Tables

Instructor: Craig Duckett. Lecture 04: Thursday, April 5, Relationships

Assignment 1 DUE TONIGHT

Instructor: Craig Duckett. Lecture 02: Thursday, March 29 th, 2018 SQL Basics and SELECT, FROM, WHERE

Database Management System Prof. D. Janakiram Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

Instructor: Craig Duckett. Lecture 14: Tuesday, May 15 th, 2018 Stored Procedures (SQL Server) and MySQL

How to Improve Your Campaign Conversion Rates

Database Management System Prof. D. Janakiram Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

MITOCW watch?v=0jljzrnhwoi

CSE 530A ACID. Washington University Fall 2013

MITOCW ocw f99-lec12_300k

Linked Lists. What is a Linked List?

In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology.

MITOCW watch?v=zm5mw5nkzjg

CPS352 Lecture - The Transaction Concept

CS352 Lecture - The Transaction Concept

MITOCW ocw f99-lec07_300k

Database Usage (and Construction)

MITOCW watch?v=yarwp7tntl4

bobpusateri.com heraflux.com linkedin.com/in/bobpusateri. Solutions Architect

DATABASE TRANSACTIONS. CS121: Relational Databases Fall 2017 Lecture 25

Instructor: Craig Duckett. Lecture 11: Thursday, May 3 th, Set Operations, Subqueries, Views

Photoshop Tutorial: Basic Selections

Information Systems (Informationssysteme)

Transcript: A Day in the Life Desiree: 7 th Grade Learning Coach Profile

IntroductionToRobotics-Lecture02

Lesson 3 Transcript: Part 1 of 2 - Tools & Scripting

Slide 1 CS 170 Java Programming 1 Testing Karel

MITOCW ocw apr k

DB2 Lecture 10 Concurrency Control

A lot of people make repeated mistakes of not calling their functions and getting errors. Make sure you're calling your functions.

Instructor: Craig Duckett SQL SERVER WALK-THROUGH LAB 13 Lecture 13: Thursday, May 10 th, 2018 Microsoft Azure and SQL SERVER INTRO

Note: Please use the actual date you accessed this material in your citation.

6.830 Lecture Transactions October 23, 2017

Instructor: Craig Duckett. Lecture 01: Tuesday, March 27 th, 2018 Orientation and Database Introduction

P1_L3 Operating Systems Security Page 1

MySQL 05/29/ :46:01 AM

6.033 Computer System Engineering

Introduction to Databases and SQL

Section 7.2: Applications of the Normal Distribution

Databases - Transactions

CheckBook Pro 2 Help

Introduction to Cryptology Dr. Sugata Gangopadhyay Department of Computer Science and Engineering Indian Institute of Technology, Roorkee

CSC 261/461 Database Systems Lecture 24

Concurrency Control & Recovery

I'm Andy Glover and this is the Java Technical Series of. the developerworks podcasts. My guest is Brian Jakovich. He is the

P1_L6 Mandatory Access Control Page 1

Skill 1: Multiplying Polynomials

Introduction to Databases, Fall 2005 IT University of Copenhagen. Lecture 10: Transaction processing. November 14, Lecturer: Rasmus Pagh

Isolation levels. Introduction to Database Design 2012, Lecture 14. Rasmus Ejlers Møgelberg. Questions class about one week before exam

MITOCW watch?v=9h6muyzjms0

MITOCW watch?v=rvrkt-jxvko

MITOCW watch?v=4dj1oguwtem

Can You Run Internet Explorer 10 On Windows 8.1

Page 1. Quiz 18.1: Flow-Control" Goals for Today" Quiz 18.1: Flow-Control" CS162 Operating Systems and Systems Programming Lecture 18 Transactions"

Hello, and welcome to another episode of. Getting the Most Out of IBM U2. This is Kenny Brunel, and

Database Systems CSE 414

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between

MITOCW watch?v=penh4mv5gag

Concurrency Control 1

Page 1. Goals for Today" What is a Database " Key Concept: Structured Data" CS162 Operating Systems and Systems Programming Lecture 13.

MITOCW watch?v=r6-lqbquci0

CSC 261/461 Database Systems Lecture 21 and 22. Spring 2017 MW 3:25 pm 4:40 pm January 18 May 3 Dewey 1101

COURSE 1. Database Management Systems

T Mobile Manual Contract Cancellation Fee 2012

Formal Methods of Software Design, Eric Hehner, segment 24 page 1 out of 5

Post Experiment Interview Questions

What are Transactions? Transaction Management: Introduction (Chap. 16) Major Example: the web app. Concurrent Execution. Web app in execution (CS636)

Module 2.3a IP 0:00 - 1:00 -

The Stack, Free Store, and Global Namespace

Transactions and Concurrency Control

Blitz2D Newbies: Definitive Guide to Types by MutteringGoblin

CMPT 354: Database System I. Lecture 11. Transaction Management

MITOCW watch?v=w_-sx4vr53m

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2002

FIOSys (How FIOSys Works)

B - Broken Track Page 1 of 8

COSC344 Database Theory and Applications. Lecture 21 Transactions

Transaction Management: Introduction (Chap. 16)

A transaction is a sequence of one or more processing steps. It refers to database objects such as tables, views, joins and so forth.

Cryptography III Want to make a billion dollars? Just factor this one number!

Topics in Reliable Distributed Systems

Selecting Your Wordpress Theme

mk-convert Contents 1 Converting to minikanren, quasimatically. 08 July 2014

T Mobile Manual Contract Deals Iphone 5s Work In India

mismatch between what is maybe possible today and what is going on in many of today's IDEs.

Slide 1 CS 170 Java Programming 1

DB2 for z/os Best Practices Optimizing Insert Performance - Part 1

CS61A Lecture 28 Distributed Computing. Jom Magrotker UC Berkeley EECS August 6, 2012

Manual Update Android 4.3 Galaxy S4 Active S

PROFESSOR: So far in this course we've been talking a lot about data abstraction. And remember the idea is that

Manually Sync Itouch Touch Itunes Wont Let Me Update My Music To My

Lesson 11 Transcript: Concurrency and locking

Quiz 3; Tuesday, January 27; 5 minutes; 5 points [Solutions follow on next page]

Computer Security module

Welcome to this IBM Rational podcast, Using the. System Architect Migration Toolkit to Migrate Your DoDAF 1.5

MITOCW watch?v=flgjisf3l78

Administration Naive DBMS CMPT 454 Topics. John Edgar 2

CHAPTER 3 RECOVERY & CONCURRENCY ADVANCED DATABASE SYSTEMS. Assist. Prof. Dr. Volkan TUNALI

So on the survey, someone mentioned they wanted to work on heaps, and someone else mentioned they wanted to work on balanced binary search trees.

Transcription:

Instructor: Craig Duckett Lecture 07: Tuesday, April 17 th, 2018 Conflicts and Isolation, MySQL Workbench 1

MID-TERM EXAM is LECTURE 10, Tuesday, May 1st Assignment 2 is due LECTURE 12, Tuesday, May 8 th in StudentTracker by MIDNIGHT Assignment 3 is due LECTURE 20, Tuesday, June 5 th Database Presentation is due LECTURE 20, Tuesday, June 5 th Final Exam is LECTURE 21, Thursday, June 7 th 2

3 x 150 Points (450 points Total) Assignment 1 GRADED! Assignment 2 (Stage 2): DUE LECTURE 12 Tuesday, May 8 th Assignment 3 (Stage 3): DUE LECTURE 20 Tuesday, June 5 th Database Presentation: DUE LECTURE 20 Tuesday, June 5 th 3

Tuesday (LECTURE 7) Database Design for Mere Mortals: Chapter 4 4

XAMPP LITE: http://www.usbwebserver.net 5

Conflicts and Isolation 6

Conflicts and Isolation http://dev.mysql.com/doc/refman/5.6/en/commit.html 7

Early we had talked about transactions and the ACID test, and now it's time to talk a bit more about them. Earlier I had talked about the characteristics of a transaction, but here I'm going to focus just on one part of ACID, the idea of isolation. The idea that when simultaneous users are interacting with the database at the same time, how do we stop them from treading on each other's toes? This is the inherent problem of concurrency, or concurrent users.

Conflicts and Isolation Well, first, let's understand how they might conflict with each other. Here's an example. We own a small bank, and we've got Alice and Bob who are two customers in our system and they have three accounts: a joint account, and two separate accounts for personal incidentals. These are each a different row in this account table, about as simple as I could possibly make it here. Every so often, Alice and Bob want to transfer money from their joint account into the individual one. Every time they do, the process goes something like this (I'm going to assume that Alice is doing this transaction):

Conflicts and Isolation I'm just going write pseudo code here, because it's not the SQL I want to talk about, it's the steps. We get the balance of the joint account. We reach into the table. Okay, joint is $10,000.

All well and good. We've subtracted $1,000 from joint. We've added $1,000 to Alice. Conflicts and Isolation We then get balance of Alice's account which is $50. Then we'll perform an operation to update the balance of the joint account to what I just retrieved which was $10,000 minus $1,000. So we're reaching into the database, setting it to $9,000, and then we'll reach back in and update the balance of Alice's account to $1,050.

Conflicts and Isolation Then one day, they both log on to the website at the same time and both attempt to do this move. Alice starts her operation. She reads the balance of the joint account. It's $10,000.

Conflicts and Isolation Bob starts his. He reads the balance of the joint account. It's also $10,000. No changes have happened yet.

Conflicts and Isolation Alice gets her balance, which is $50. Bob gets his balance, which is $45.

Conflicts and Isolation Then Alice issues the update to joint. She'd retrieved it as $10,000. She will subtract $1,000 from that, which is $9,000, she changes the database.

Conflicts and Isolation Bob also tries to perform that operation. He believes that joint account is $10,000. So he will also set that to $9,000. Then Alice updates her account adding $1,000 to it. Bob updates his adding $1,000 to that. Now the end result is that the joint account is down $1,000, but both Alice and Bob's account have been credited $1,000 each. The balance should be $8,000, not $9,000.

Conflicts and Isolation Now Alice and Bob might be perfectly happy with this, but the bank would not. This is what often called a race condition. It's a conflict where two people or two programming threads are doing very similar steps and just getting a little bit ahead of each other. They're trying to affect the same data. When they're doing it together, you'll end up with a very different outcome to what would have happened if they done exactly the same steps, but Alice did all hers first and then Bob did his second.

Transactions So the first step to fixing this situation as we make these atomic. We're making these several actions grouped into one indivisible unit by making them transactions. We do this by adding SQL keywords at the start and end. In this case, we use the words Begin Transaction. We'll do all the operations we want to do and then we write the SQL keyword commit to say our transaction is done. Now, these are SQL keywords that deal with transactions, although you will find that you write slightly different code to create a transaction in the different database management systems. Sometimes it's just Begin, sometimes it's just Begin Work, sometimes it's Begin Transaction or Start Transaction (as in MySQL), but again it's the core concept we're going for here. We want to go all the way through and commit if everything is successful. If there's a problem anywhere in the transaction, we want the database to roll back to the start of the transaction as if no part of it had ever happened.

Transactions I said this is step one, because this by itself may not help our race condition problem. If both of these transactions are still executing at exactly the same time and they're allowed to do that, because we didn't actually have any errors occur. The issue is that Bob is being allowed to read from the same table that Alice is in the process of changing, but she hasn't finished changing it yet. This is often what's referred to as a dirty read. There's a transaction going on that's partway through it. It's going to be changing this data, but we're allowed to read from somewhere in the middle of that transaction. Just making the transaction is not good enough. We also want some kind of locking to occur so that we are prevented from simultaneously changing the same data at least until the transaction is over.

Pessimistic Locking Now one way of doing this is what's referred to as pessimistic locking where we are pessimistic that transactions will conflict with each other. So as soon as a transaction starts, we're going to lock the data that it's referring to, and only unlock it once the transaction commits. Here's how this would work. Going back to the beginning situation we are creating transactions for both Alice and Bob. So Alice's transaction starts. She reads the balance of the joint account and she will lock that information. Now Bob would then attempt to read in his transaction, but he won't be allowed to. He'll be refused.

Pessimistic Locking Hopefully only have milliseconds are going on here. Bob is waiting, Alice goes ahead. She gets the balance of her account. She reaches in and changes the joint account to $9,000. She then updates her account and then hits commit. Her transaction is over, and that means any lock that she was applying is now unlocked. Bob will now get that message back. The balance is $9,000.

Pessimistic Locking Bob is allowed to continue to get his balance. He can subtract $1,000 from that. We're down to $8,000 as we should be. We update, and we commit his changes. The thing is pessimistic locking is great for Alice, but not necessarily so great for Bob.

Alice will read hers, Bob will read his. Optimistic Locking One option with some database systems is to allow what's called optimistic locking, which means Bob would be allowed to read from the table while the transaction is going on, because we are optimistic that there won't be a conflict. We begin with the same situation. Alice and Bob both start. Alice starts her transaction and reads the joint account balance. Bob tries, but where he was refused a moment ago with pessimistic locking, he's going to be allowed to right now. Both continue.

Optimistic Locking Alice will change the joint account down by $9,000. Now here's the difference. Bob will try and update that too. He thinks he is going to update it from $10,000 to $9,000. What's going to happen withoptimistic locking is the database will immediately realize that the contents of this row has changed since he read it, and it's going to actually issue an error. It will detect that there was a dirty read, and this transaction can't go any further, because it doesn't know how to handle that conflict. In most cases, it will just roll back to the beginning of the transaction. Bob would get some kind of error that the application would need to deal with.

Optimistic Locking Alice can continue on, because her transaction hasn't thrown any kind of issue. She can commit with her changes in place. When you're working withlocking, both pessimistic and optimistic, this is one of the areas where it's implemented a little differently across Database Management Systems, although the principles are the same. Now with some DBMS, you have specific keywords to explicitly lock data that you're accessing. With others, there's default behavior, what's called the default isolation level of a database, meaning, do we naturally do pessimistic locking or optimistic locking or perhaps something inbetween. We will lock any inserts and updates in the transaction until they commit, but not the select, just the basic read. Take a look at your chosen database management system when it comes time to do this.

MySQL Workbench https://www.mysql.com/products/workbench/

MySQL Workbench Links & Tutorials MySQL Workbench: Introduction (YouTube) MySQL Workbench: Connecting to a Server and Troubleshooting (YouTube) For those who are interested in giving MySQL Workbench a try, it will make more sense after the ICE today using phpmyadmin, since a lot of the functionality is similar. A good rule-of-thumb, is to get good first at working with phpmyadmin, then give MySQL Workbench a try since it has more bells-and-whistles and has a bit more of a learning curve. 27

BIT 275 ICE 07 Creating a Database and Tables and Inserting Data with phpmyadmin 28