SQL – Transactions in SQLite

Slides:



Advertisements
Similar presentations
Database System Concepts 5 th Ed. © Silberschatz, Korth and Sudarshan, 2005 See for conditions on re-usewww.db-book.com Chapter 16 : Concurrency.
Advertisements

CM20145 Concurrency Control
Chapter 16 Concurrency. Topics in this Chapter Three Concurrency Problems Locking Deadlock Serializability Isolation Levels Intent Locking Dropping ACID.
Concurrency Control Amol Deshpande CMSC424. Approach, Assumptions etc.. Approach  Guarantee conflict-serializability by allowing certain types of concurrency.
Lock-Based Concurrency Control
Transaction Processing. Objectives After completing this lesson, you should be able to do the following: –Define transactions effectively for an application.
Prepared by: Mudra Patel (113) Locking Scheduler & Managing Hierarchies of Database Elements.
Transaction Management and Concurrency Control
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
Chapter 9 Transaction Management and Concurrency Control
9 Copyright © 2009, Oracle. All rights reserved. Managing Data Concurrency.
Database Administration Part 1 Chapter Six CSCI260 Database Applications.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
Concurrency Control Chapter 18 Section 18.5 Presented by Khadke, Suvarna CS 257 (Section II) Id
Managing Transaction and Lock Vu Tuyet Trinh Hanoi University of Technology 1.
1 IT420: Database Management and Organization Transactions 31 March 2006 Adina Crăiniceanu
Security, Transactions, and Views. Security Achieved through GRANT & REVOKE Assumes the database can recognize its users and verify their identity can.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 10 Transaction Management.
Oracle Locking Michael Messina Principal Database Analyst Indiana University.
1cs Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect.
Introduction to Data Management CSE 344 Lecture 23: Transactions CSE Winter
ITEC 3220M Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220m.htm
Unit 9 Transaction Processing. Key Concepts Distributed databases and DDBMS Distributed database advantages. Distributed database disadvantages Using.
Chapter 11 Concurrency Control. Lock-Based Protocols  A lock is a mechanism to control concurrent access to a data item  Data items can be locked in.
SCUJoAnne Holliday11–1 Schedule Today: u Transaction concepts. u Read Sections Next u Authorization and security.
TRANSACTION MANAGEMENT R.SARAVANAKUAMR. S.NAVEEN..
Concurrency Control in Database Operating Systems.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 136 Database Systems I SQL Modifications and Transactions.
How transactions work A transaction groups a set of Transact-SQL statements so that they are treated as a unit. Either all statements in the group are.
Module 11 Creating Highly Concurrent SQL Server® 2008 R2 Applications.
8 Copyright © 2005, Oracle. All rights reserved. Managing Data.
CSC271 Database Systems Lecture # 17. Summary: Previous Lecture  View updatability  Advantages and disadvantages of views  View materialization.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 1 Transations.
Module 11: Managing Transactions and Locks
Transaction Management and Concurrent Control
9 1 Chapter 9_B Concurrency Control Database Systems: Design, Implementation, and Management, Rob and Coronel.
NOEA/IT - FEN: Databases/Transactions1 Transactions ACID Concurrency Control.
10 1 Chapter 10_B Concurrency Control Database Systems: Design, Implementation, and Management, Rob and Coronel.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
©Bob Godfrey, 2002, 2005 Lecture 17: Transaction Integrity and Concurrency BSA206 Database Management Systems.
3 Database Systems: Design, Implementation, and Management CHAPTER 9 Transaction Management and Concurrency Control.
Module 14: Managing Transactions and Locks. Overview Introducing Transactions and Locks Managing Transactions Understanding SQL Server Locking Architecture.
Oracle 10g Database Administrator: Implementation and Administration Chapter 10 Basic Data Management.
Chapter 13 Managing Transactions and Concurrency Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
COMP 430 Intro. to Database Systems Transactions, concurrency, & ACID.
Transaction Management
Concurrency Control Managing Hierarchies of Database Elements (18.6)
Stored Procedures.
Transaction Management and Concurrency Control
LAB: Web-scale Data Management on a Cloud
Concurrency Control.
Schedule Today Transactions, Authorization. Sections
Part- A Transaction Management
Transaction & Record Scoping
18.5 An Architecture for a Locking Scheduler
Transaction Properties
18.5 An Architecture for Locking Scheduler
Database Processing: David M. Kroenke’s Chapter Nine: Part One
March 9th – Transactions
Transactions, Locking and Query Optimisation
Transactions.
Chapter 10 Transaction Management and Concurrency Control
On transactions, and Atomic Operations
Chapter 15 : Concurrency Control
ENFORCING SERIALIZABILITY BY LOCKS
Introduction of Week 13 Return assignment 11-1 and 3-1-5
Transactions and Concurrency
CONCURRENCY Concurrency is the tendency for different tasks to happen at the same time in a system ( mostly interacting with each other ) .   Parallel.
CSE 542: Operating Systems
-Transactions in SQL -Constraints and Triggers
Presentation transcript:

SQL – Transactions in SQLite

Transaction Lifecycle It starts with a BEGIN statement (which defines the type of transaction). Then, there are the statements within the transaction. The transaction can end a few ways: With a COMMIT statement (indicating that the database modifications should be made permanent) With a ROLLBACK statement (indicating an abort of the transaction and undoing of its effects) With an error (which achieves the same effect as ROLLBACK) We'll discuss ROLLBACK later in the class.

BEGIN and COMMIT You start a new transaction with "BEGIN TRANSACTION;". You finish a transaction with "COMMIT TRANSACTION;". BEGIN TRANSACTION; INSERT INTO students VALUES ('Josh', 3.5); UPDATE students SET grade = grade + 0.5; COMMIT TRANSACTION;

Auto commit and Python - SQLite By default, SQLite auto commits after every statement. This means that outside of an explicit transaction, every statement is its own transaction. When you specify "BEGIN TRANSACTION", auto commit turns off until you say "COMMIT TRANSACTION" (or "ROLLBACK TRANSACTION"). The python-SQLite module tries to be helpful by adding "BEGINs" and "COMMITs" for you. We want to turn this behavior off by setting isolation_level to None (See next slide). Python-sqlite3 module also has commit (and rollback) methods on it's Connection objects. DO NOT USE THEM! They don't play nicely with explicit "BEGIN" and "COMMIT" (and "ROLLBACK").

import sqlite3 con1 = sqlite3.connect("test.db", isolation_level=None) con2 = sqlite3.connect("test.db", isolation_level=None) con1.execute("DROP TABLE IF EXISTS students;") con1.execute("CREATE TABLE students (name TEXT);") con1.execute("INSERT INTO students VALUES ('Grant');") con1.execute("BEGIN TRANSACTION;") con1.execute("INSERT INTO students VALUES ('Josh');") con2.execute("BEGIN TRANSACTION;") res = con2.execute("SELECT * FROM STUDENTS;") print("con2:",list(res)) # con2: [('Grant',)] res = con1.execute("SELECT * FROM STUDENTS;") print("con1:", list(res)) # con1: [('Grant',), ('Josh',)] con2.execute("COMMIT TRANSACTION;") con1.execute("COMMIT TRANSACTION;")

Normal usage In the previous example, one thread (one python script) had two connections to the same database. In normal usage, different people/programs would be talking to the same database. Because SQLite uses locking to ensure serializability, it can't commit database modifications until it holds the exclusive lock. But it our case, the next python command can't run until the previous command finished. So if the previous command is waiting for a lock to be released, it will wait forever. (or until the default timeout length of 5 seconds passes) Normally, transactions will take less than a second to run, so lock out isn't a very big issue.

Database element should SQLite use to lock with? Value Row Table Database

Five states of locking the database UNLOCKED No locks are held on the database. The database may be neither read nor written. Any internally cached data is considered suspect and subject to verification against the database file before being used. Other processes can read or write the database as their own locking states permit. This is the default state. SHARED The database may be read but not written. Any number of processes can hold SHARED locks at the same time, hence there can be many simultaneous readers. But no other thread or process is allowed to write to the database file while one or more SHARED locks are active. RESERVED A RESERVED lock means that the process is planning on writing to the database file at some point in the future but that it is currently just reading from the file. Only a single RESERVED lock may be active at one time, though multiple SHARED locks can coexist with a single RESERVED lock. RESERVED differs from PENDING in that new SHARED locks can be acquired while there is a RESERVED lock.

Five states of locking the database PENDING A PENDING lock means that the process holding the lock wants to write to the database as soon as possible and is just waiting on all current SHARED locks to clear so that it can get an EXCLUSIVE lock. No new SHARED locks are permitted against the database if a PENDING lock is active, though existing SHARED locks are allowed to continue. EXCLUSIVE An EXCLUSIVE lock is needed in order to write to the database file. Only one EXCLUSIVE lock is allowed on the file and no other locks of any kind are allowed to coexist with an EXCLUSIVE lock. In order to maximize concurrency, SQLite works to minimize the amount of time that EXCLUSIVE locks are held.

SQlite's Three Transaction Modes The default mode (and the mode I've been demonstrating) is called DEFERRED. It requests a shared lock the first time it tries to read It requests an exclusive lock the first time it tries to write IMMEDIATE transaction mode: It requests a RESERVED lock immediately (during the "BEGIN" command) It will fail/wait if another transaction is holding an exclusive lock It requests an exclusive lock prior to writing On commit, the reserved lock is promoted to an exclusive lock EXCLUSIVE transaction mode: It requests an exclusive lock immediately It will fail/wait if another transaction is holding a shared or exclusive lock All transactions release their all their locks when committing

Specifying a transaction mode BEGIN TRANSACTION; -- Defaults to DEFERRED BEGIN DEFERRED TRANSACTION; -- Same as above BEGIN IMMEDIATE TRANSACTION; -- Starts in immediate mode BEGIN EXCLUSIVE TRANSACTION; -- Starts in exclusive mode COMMIT TRANSACTION; -- Commits regardless of transaction mode ROLLBACK TRANSACTION; -- Rollback regardless of transaction mode

Which is the best transaction mode? Default Deferred Immediate Exclusive