SlideShare une entreprise Scribd logo
1  sur  42
Lecture 12
Concurrent Programming
Reading
 Fowler chapter 5 Concurrency
– Examples are from the chapter

 Fowler chapter 16 Offline Concurrency Patterns
– Optimistic Offline Lock
– Pessimistic Offline Lock
Agenda
 Concurrency
–
–
–
–
–
–

Problems with concurrency
Execution Contexts
Transactions, Isolation and immutability
Currency Control, Deadlocks
ACID properties and Isolation
Offline concurrency

 Patterns
– Optimistic Offline Lock (416)
– Pessimistic Offline Lock (426)
Concurrency
 Enterprise system must support many
simultaneous users
– Need to guaranty correctness of data

 Concurrency
– When computations overlap in time, and which may
permit the sharing of common resources between
those overlapped computations
– When two users are updating the same data, race
conditions can occur causing corrupt data
Concurrency Problems
 Martin opens file to work with
 David opens the same file, changes and finishes
before Martin and saves the file
 Martin than saves his changes and David's
changes are lost
Lost update
Concurrency Problems
 Martin needs to know how many files are in the
concurrency package
 The package contains two sub-packages
 Martin counts the number in first package, then
becomes busy
 In the meantime David adds new files to both
packages
 Then Martin continues and counts the files in the
second package
Inconsistent read
Concurrency Problems
 Both problems cause a failure of correctness
– Result when two people are working on the same
data at the same time

 To avoid these problems and provide
correctness we must lock access to the data
– Only one person can work on the data at the same
time
– Provides correctness
– Reduces concurrency

 Liveness suffers
– How much concurrent activity can go on
Execution Contexts
 Processing occurs in some context
– Two important contexts: request and session

 Request
– Single call from outside, system sends response

 Session
– Long-running interaction between client and server
– Multiple requests that must be linked together
– Example: user logs in, places items in a shopping
cart, buys, logs out
Isolation
 Partition the data so that any piece of it can only
be accessed by one active agent (program or
thread)
 Only one thread can enter critical section or
isolated zone Inconsistent read
at each
Immutability
 Concurrency problems occurs for data that can
be modified
 By recognizing immutable data we can relax
concurrency concerns and share it widely
Inconsistent read
EXCERISE
Two users of a source control system want to work on the
same file at the same time. How can we make sure that data is
not lost?
Concurrency Control
 Control of mutable data that we can’t isolate
 Pessimistic locking
– Martin opens the file
– When David wants to open the file, he’ll get denial,
saying it is already in use
– Conflicts avoidance
Concurrency Control
 Control of mutable data that we can’t isolate
 Optimistic locking
– Martin and David both edit the same file
– David finishes first and saves
– Then Martin saves, he’ll get an error since David has
updated the file
– Conflict detection
Concurrency Control
 Problem with pessimistic locking

– Avoids concurrency and reduces efficiency

 Optimistic locking provide more efficiency
– Locks are only used on commit
– The problem is what happens on conflicts

 Which one to use?

– Based on frequency and severity of conflicts
– If conflicts are sufficiently rare or if the consequence is not
great, optimistic locking works better
– If conflicts are frequent and painful, pessimistic locks are
better
Preventing Inconsistent Reads
 Inconsistent Reads
– Martin edits the Customer class and adds some calls
to the Order class. Meanwhile David edits the Order
class and changes the interface. David compiles and
checks in. Martin compiles and checks in. Now the
shared code is broken.

 How to avoid this?
– Pessimistic Lock
• Avoids the problem

– Optimistic Locks
• Detects the problem
Preventing Inconsistent Reads
 Pessimistic Lock

– To read data you need a read lock and to write data you
need to have write lock
– Many can have read lock, but if anyone has read lock,
nobody can get write lock
– If anyone has write lock, nobody can get read lock
– Can lead to Dead-lock

 Optimistic Locks

– Use timestamps or sequence number for version
marker
– If someone tries to commit broken code it is detected and
needs manual fix
Deadlock
 When two or more are waiting for each other
– David is using the Order file and is waiting for the
Customer file, but Martin has the Customer file and
is waiting for the Order file.
– This can happen in the pessimistic approach

 Solutions
– Detect the deadlock and find a victim
– Release resources from the victim so other can
progress
– Use timeouts
Transactions
 Transaction is a bounded sequence of work
– Both start and finish is well defined
– Transaction must complete on an all-or-nothing basis

 All resources are in consistent state before and
after the transaction
 Example: Database transaction
– Withdraw data from account
– Buy the product
– Update stock information

 Transactions must have ACID properties
ACID properties
 Atomicity
– All steps are completed successfully – or rolled back

 Consistency
– Data is consistent at the start and the end of the
transaction

 Isolation
– Transaction is not visible to any other until that transaction
commits successfully

 Durability
– Any results of a committed transaction must be made
permanent
Transactional Resources
 Anything that is transactional
– Use transaction to control concurrency
– Databases, printers, message queues

 Transaction must be as short as possible
– Provides greatest throughput
– Should not span multiple requests
– Long transactions span multiple request
Transaction Isolations and
Liveness lock tables (or resources)
 Transactions

– Need to provide isolation to guarantee correctness
– Liveness suffers
– We need to control isolation

 Serializable Transactions
–
–
–
–

Full isolation
Transactions are executed serially, one after the other
Benefits: Guarantees correctness
Drawbacks: Can seriously damage liveness and
performance
Isolation Level
 Problems can be controlled by setting the
isolation level
– We don’t want to lock table since it reduces
performance
– Solution is to use as low isolation as possible while
keeping correctness
Phantoms
 Description

– Transaction A reads rows. Transaction B adds (INSERT) a
new row. A reads rows again, but now a new row has been
added, “phantom” row.

– Repeatable Read isolation level
Unrepeatable Read
 Description

– Transaction A reads value. Transaction B updates the
value. A repeats the read but now the value is
different.

– Read Committed isolation level
Dirty Read
 Description

– Transaction A reads and updates value. Transaction B
reads the value. Then A rollbacks and resets value. B
updates value.

– Read uncommitted isolation level
Isolation Level
 Problems can be controlled by setting the
isolation level
– We don’t want to lock table since it reduces
performance
– Solution is to use as low isolation as possible while
keeping correctness
Transactions
 Pull together several requests that the clients
wants treated as if they were a single request
 System Transactions
– From the Application to the Database

 Business Transaction
– From the User to an Application
– Transactions that expand more than one request
Offline Concurrency
 Need ACID properties for Business Transactions
– Problem is with locking
– Application won’t be scalable because long
transactions will turn the database into a major
bottleneck

 Solution

– Business Transaction are broken into short system
transactions
– System must provide ACID properties between
system calls
Optimistic Offline Lock (416)
Prevents conflicts between concurrent business
transactions by detecting and rolling back the
transaction
 How It Works
– Validates chances to data when committed
– If someone else has in the meantime updated,
changes are not committed
– Based on version counters
– Can provide old and new version for comparisons

 When to Use It

– When chance of conflict is low, resolution is not too
hard
Optimistic Offline Lock (416)
Pessimistic Offline Lock (426)
Prevents conflicts between concurrent business
transactions by allowing only one business
transaction at a time to access data
 How It Works

– Prevents conflicts by avoiding them
– Data is locked so it cannot be edited
– Locks can be: exclusive write lock, exclusive read lock,
read/write lock
– Can be controlled by the application or the database

 When to Use It

– When data must be isolated and conflicts are likely
Pessimistic Offline Lock (426)
EXCERISE
Implement Optimistic Locking
EXCERISE
Implement Optimistic Locking
Add versions to the data and throw an exception if someone
tries to change the data that has already been changed
Example
 Table customer

create table customer
(
id int Identity (1, 1) primary key NOT NULL,
modifiedby varchar(32),
modified datetime,
version int,
name varchar(32)
)
Example
 Data Transfer Object reflects the customer
table
public class Customer
{
private int id;
private Date modified;
private String modifiedBy;
private int version;
private String name;
...
Example
 Layered Supertype for Data Mappers
package is.ru.honn.data;
import javax.sql.DataSource;
public abstract class AbstractMapper
{
private String owner;
private DataSource dataSource;
protected AbstractMapper()
{
}
...
}
Example
 CustomerMapper
public class CustomerMapper extends AbstractMapper
{
public Customer find(int id)
{
JdbcTemplate tpl = new JdbcTemplate(getDataSource());
return (Customer) tpl.query("select * from customer where id=" + id,
new CustomerRowMapper()).get(0);
}
Example
 CustomerMapper
public void update(Customer customer) throws ConcurrencyException
{
Customer current = find(customer.getId());
if (current.getVersion() > customer.getVersion())
throw new ConcurrencyException("Customer has been changed by " +
current.getModifiedBy() + " at " +
current.getModified() + " (version: " + customer.getVersion() + ")");
JdbcTemplate tpl = new JdbcTemplate(getDataSource());
tpl.update("update customer set name=?, modifiedby=?, modified=?, " +
"version=? where id=?",
new Object[]
{
customer.getName(),
this.getOwner(),
new Date(),
customer.getVersion() + 1,
Example
public static void main(String[] args)
{
Resource resource = new FileSystemResource("data.xml");
BeanFactory beanfactory = new XmlBeanFactory(resource);

CustomerMapper mapperMartin = (CustomerMapper)beanfactory.getBean("customerMa
mapperMartin.setOwner("Martin");
CustomerMapper mapperDavid = (CustomerMapper)beanfactory.getBean("customerMap
mapperDavid.setOwner("David");
Customer custM = mapperMartin.find(1);
custM.setName("Mr. Stimpson J. Cat");

Customer custD = mapperDavid.find(1);
custD.setName("Ren Hoek");
Example
try
{
mapperDavid.update(custD);
} catch (ConcurrencyException e) {
e.printStackTrace();
}

try {
mapperMartin.update(custM);
} catch (ConcurrencyException e) {
e.printStackTrace();
}
}
Summary
 Concurrency
–
–
–
–
–
–
–

Concurrency can cause problems with correctness
Transactions execute in execution Contexts
Transactions are isolated
Currency must be controlled
Deadlocks can happen
ACID properties and Isolation
Offline concurrency

 Patterns

– Optimistic Offline Lock (416)
– Pessimistic Offline Lock (426)

Contenu connexe

En vedette

Waterfall model in Software engineering
Waterfall model in Software engineeringWaterfall model in Software engineering
Waterfall model in Software engineeringEhtesham Mehmood
 
comparison of various sdlc models
comparison of various sdlc modelscomparison of various sdlc models
comparison of various sdlc modelssadaf ateeq
 
Comparison between waterfall model and spiral model
Comparison between waterfall model and spiral modelComparison between waterfall model and spiral model
Comparison between waterfall model and spiral modelGalaxyy Pandey
 
Spiral model explanation
Spiral model  explanationSpiral model  explanation
Spiral model explanationUmar Farooq
 
software development, process model, requirement engineering, srs, structured...
software development, process model, requirement engineering, srs, structured...software development, process model, requirement engineering, srs, structured...
software development, process model, requirement engineering, srs, structured...Ashok Mohanty
 
Scrum 101: Introduction to Scrum
Scrum 101: Introduction to ScrumScrum 101: Introduction to Scrum
Scrum 101: Introduction to ScrumArrielle Mali
 
Introduction to Scrum.ppt
Introduction to Scrum.pptIntroduction to Scrum.ppt
Introduction to Scrum.pptMohan Late
 
Database management system
Database management systemDatabase management system
Database management systemRizwanHafeez
 

En vedette (15)

Waterfall model in Software engineering
Waterfall model in Software engineeringWaterfall model in Software engineering
Waterfall model in Software engineering
 
comparison of various sdlc models
comparison of various sdlc modelscomparison of various sdlc models
comparison of various sdlc models
 
Comparison between waterfall model and spiral model
Comparison between waterfall model and spiral modelComparison between waterfall model and spiral model
Comparison between waterfall model and spiral model
 
waterfall model
waterfall modelwaterfall model
waterfall model
 
SDLC Model (Waterfall,Iterative Waterfall,Spiral)
SDLC Model (Waterfall,Iterative Waterfall,Spiral)SDLC Model (Waterfall,Iterative Waterfall,Spiral)
SDLC Model (Waterfall,Iterative Waterfall,Spiral)
 
Waterfallmodel
WaterfallmodelWaterfallmodel
Waterfallmodel
 
Mongo db
Mongo dbMongo db
Mongo db
 
Waterfall Model
Waterfall ModelWaterfall Model
Waterfall Model
 
Spiral model explanation
Spiral model  explanationSpiral model  explanation
Spiral model explanation
 
software development, process model, requirement engineering, srs, structured...
software development, process model, requirement engineering, srs, structured...software development, process model, requirement engineering, srs, structured...
software development, process model, requirement engineering, srs, structured...
 
Waterfall model
Waterfall modelWaterfall model
Waterfall model
 
Scrum 101: Introduction to Scrum
Scrum 101: Introduction to ScrumScrum 101: Introduction to Scrum
Scrum 101: Introduction to Scrum
 
Dbms
DbmsDbms
Dbms
 
Introduction to Scrum.ppt
Introduction to Scrum.pptIntroduction to Scrum.ppt
Introduction to Scrum.ppt
 
Database management system
Database management systemDatabase management system
Database management system
 

Similaire à L12 Concurrent Programming

Illuminate - Performance Analystics driven by Machine Learning
Illuminate - Performance Analystics driven by Machine LearningIlluminate - Performance Analystics driven by Machine Learning
Illuminate - Performance Analystics driven by Machine LearningjClarity
 
Unit no 5 transation processing DMS 22319
Unit no 5 transation processing DMS 22319Unit no 5 transation processing DMS 22319
Unit no 5 transation processing DMS 22319ARVIND SARDAR
 
Scalability Considerations
Scalability ConsiderationsScalability Considerations
Scalability ConsiderationsNavid Malek
 
Concurrency Control in Distributed Systems.pptx
Concurrency Control in Distributed Systems.pptxConcurrency Control in Distributed Systems.pptx
Concurrency Control in Distributed Systems.pptxMArshad35
 
What is Database Management System
What is Database Management SystemWhat is Database Management System
What is Database Management SystemAbhiPatel171
 
Mwlug2014 - IBM Connections Security and Migration
Mwlug2014 - IBM Connections Security and MigrationMwlug2014 - IBM Connections Security and Migration
Mwlug2014 - IBM Connections Security and MigrationVictor Toal
 
Overview of Concurrency Control & Recovery in Distributed Databases
Overview of Concurrency Control & Recovery in Distributed DatabasesOverview of Concurrency Control & Recovery in Distributed Databases
Overview of Concurrency Control & Recovery in Distributed DatabasesMeghaj Mallick
 
dbms.ppt
dbms.pptdbms.ppt
dbms.pptRomyA2
 
Problems of cooperative system
Problems of cooperative systemProblems of cooperative system
Problems of cooperative systemfazli khaliq
 
L12 Session State and Distributation Strategies
L12 Session State and Distributation StrategiesL12 Session State and Distributation Strategies
L12 Session State and Distributation StrategiesÓlafur Andri Ragnarsson
 
Managing Memory & Locks - Series 2 Transactions & Lock management
Managing  Memory & Locks - Series 2 Transactions & Lock managementManaging  Memory & Locks - Series 2 Transactions & Lock management
Managing Memory & Locks - Series 2 Transactions & Lock managementDAGEOP LTD
 
Replication in the wild ankara cloud meetup - feb 2017
Replication in the wild   ankara cloud meetup - feb 2017Replication in the wild   ankara cloud meetup - feb 2017
Replication in the wild ankara cloud meetup - feb 2017AnkaraCloud
 

Similaire à L12 Concurrent Programming (20)

Sql server concurrency
Sql server concurrencySql server concurrency
Sql server concurrency
 
Illuminate - Performance Analystics driven by Machine Learning
Illuminate - Performance Analystics driven by Machine LearningIlluminate - Performance Analystics driven by Machine Learning
Illuminate - Performance Analystics driven by Machine Learning
 
Dbms voc 5 unit
Dbms voc 5 unitDbms voc 5 unit
Dbms voc 5 unit
 
Unit no 5 transation processing DMS 22319
Unit no 5 transation processing DMS 22319Unit no 5 transation processing DMS 22319
Unit no 5 transation processing DMS 22319
 
Replication in the Wild
Replication in the WildReplication in the Wild
Replication in the Wild
 
Scalability Considerations
Scalability ConsiderationsScalability Considerations
Scalability Considerations
 
Concurrency Control in Distributed Systems.pptx
Concurrency Control in Distributed Systems.pptxConcurrency Control in Distributed Systems.pptx
Concurrency Control in Distributed Systems.pptx
 
dbms.ppt
dbms.pptdbms.ppt
dbms.ppt
 
dbms.ppt
dbms.pptdbms.ppt
dbms.ppt
 
What is Database Management System
What is Database Management SystemWhat is Database Management System
What is Database Management System
 
DBMS.pptx
DBMS.pptxDBMS.pptx
DBMS.pptx
 
Mwlug2014 - IBM Connections Security and Migration
Mwlug2014 - IBM Connections Security and MigrationMwlug2014 - IBM Connections Security and Migration
Mwlug2014 - IBM Connections Security and Migration
 
Overview of Concurrency Control & Recovery in Distributed Databases
Overview of Concurrency Control & Recovery in Distributed DatabasesOverview of Concurrency Control & Recovery in Distributed Databases
Overview of Concurrency Control & Recovery in Distributed Databases
 
dbms.ppt
dbms.pptdbms.ppt
dbms.ppt
 
Problems of cooperative system
Problems of cooperative systemProblems of cooperative system
Problems of cooperative system
 
L12 Session State and Distributation Strategies
L12 Session State and Distributation StrategiesL12 Session State and Distributation Strategies
L12 Session State and Distributation Strategies
 
Managing Memory & Locks - Series 2 Transactions & Lock management
Managing  Memory & Locks - Series 2 Transactions & Lock managementManaging  Memory & Locks - Series 2 Transactions & Lock management
Managing Memory & Locks - Series 2 Transactions & Lock management
 
MSB-Distributed systems goals
MSB-Distributed systems goalsMSB-Distributed systems goals
MSB-Distributed systems goals
 
Lecture 5 inter process communication
Lecture 5 inter process communicationLecture 5 inter process communication
Lecture 5 inter process communication
 
Replication in the wild ankara cloud meetup - feb 2017
Replication in the wild   ankara cloud meetup - feb 2017Replication in the wild   ankara cloud meetup - feb 2017
Replication in the wild ankara cloud meetup - feb 2017
 

Plus de Ólafur Andri Ragnarsson

New Technology Summer 2020 Course Introduction
New Technology Summer 2020 Course IntroductionNew Technology Summer 2020 Course Introduction
New Technology Summer 2020 Course IntroductionÓlafur Andri Ragnarsson
 
New Technology 2019 L13 Rise of the Machine
New Technology 2019 L13 Rise of the Machine New Technology 2019 L13 Rise of the Machine
New Technology 2019 L13 Rise of the Machine Ólafur Andri Ragnarsson
 

Plus de Ólafur Andri Ragnarsson (20)

Nýsköpun - Leiðin til framfara
Nýsköpun - Leiðin til framfaraNýsköpun - Leiðin til framfara
Nýsköpun - Leiðin til framfara
 
Nýjast tækni og framtíðin
Nýjast tækni og framtíðinNýjast tækni og framtíðin
Nýjast tækni og framtíðin
 
New Technology Summer 2020 Course Introduction
New Technology Summer 2020 Course IntroductionNew Technology Summer 2020 Course Introduction
New Technology Summer 2020 Course Introduction
 
L01 Introduction
L01 IntroductionL01 Introduction
L01 Introduction
 
L23 Robotics and Drones
L23 Robotics and Drones L23 Robotics and Drones
L23 Robotics and Drones
 
L22 Augmented and Virtual Reality
L22 Augmented and Virtual RealityL22 Augmented and Virtual Reality
L22 Augmented and Virtual Reality
 
L20 Personalised World
L20 Personalised WorldL20 Personalised World
L20 Personalised World
 
L19 Network Platforms
L19 Network PlatformsL19 Network Platforms
L19 Network Platforms
 
L18 Big Data and Analytics
L18 Big Data and AnalyticsL18 Big Data and Analytics
L18 Big Data and Analytics
 
L17 Algorithms and AI
L17 Algorithms and AIL17 Algorithms and AI
L17 Algorithms and AI
 
L16 Internet of Things
L16 Internet of ThingsL16 Internet of Things
L16 Internet of Things
 
L14 From the Internet to Blockchain
L14 From the Internet to BlockchainL14 From the Internet to Blockchain
L14 From the Internet to Blockchain
 
L14 The Mobile Revolution
L14 The Mobile RevolutionL14 The Mobile Revolution
L14 The Mobile Revolution
 
New Technology 2019 L13 Rise of the Machine
New Technology 2019 L13 Rise of the Machine New Technology 2019 L13 Rise of the Machine
New Technology 2019 L13 Rise of the Machine
 
L12 digital transformation
L12 digital transformationL12 digital transformation
L12 digital transformation
 
L10 The Innovator's Dilemma
L10 The Innovator's DilemmaL10 The Innovator's Dilemma
L10 The Innovator's Dilemma
 
L09 Disruptive Technology
L09 Disruptive TechnologyL09 Disruptive Technology
L09 Disruptive Technology
 
L09 Technological Revolutions
L09 Technological RevolutionsL09 Technological Revolutions
L09 Technological Revolutions
 
L07 Becoming Invisible
L07 Becoming InvisibleL07 Becoming Invisible
L07 Becoming Invisible
 
L06 Diffusion of Innovation
L06 Diffusion of InnovationL06 Diffusion of Innovation
L06 Diffusion of Innovation
 

Dernier

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 

Dernier (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 

L12 Concurrent Programming

  • 2. Reading  Fowler chapter 5 Concurrency – Examples are from the chapter  Fowler chapter 16 Offline Concurrency Patterns – Optimistic Offline Lock – Pessimistic Offline Lock
  • 3. Agenda  Concurrency – – – – – – Problems with concurrency Execution Contexts Transactions, Isolation and immutability Currency Control, Deadlocks ACID properties and Isolation Offline concurrency  Patterns – Optimistic Offline Lock (416) – Pessimistic Offline Lock (426)
  • 4. Concurrency  Enterprise system must support many simultaneous users – Need to guaranty correctness of data  Concurrency – When computations overlap in time, and which may permit the sharing of common resources between those overlapped computations – When two users are updating the same data, race conditions can occur causing corrupt data
  • 5. Concurrency Problems  Martin opens file to work with  David opens the same file, changes and finishes before Martin and saves the file  Martin than saves his changes and David's changes are lost Lost update
  • 6. Concurrency Problems  Martin needs to know how many files are in the concurrency package  The package contains two sub-packages  Martin counts the number in first package, then becomes busy  In the meantime David adds new files to both packages  Then Martin continues and counts the files in the second package Inconsistent read
  • 7. Concurrency Problems  Both problems cause a failure of correctness – Result when two people are working on the same data at the same time  To avoid these problems and provide correctness we must lock access to the data – Only one person can work on the data at the same time – Provides correctness – Reduces concurrency  Liveness suffers – How much concurrent activity can go on
  • 8. Execution Contexts  Processing occurs in some context – Two important contexts: request and session  Request – Single call from outside, system sends response  Session – Long-running interaction between client and server – Multiple requests that must be linked together – Example: user logs in, places items in a shopping cart, buys, logs out
  • 9. Isolation  Partition the data so that any piece of it can only be accessed by one active agent (program or thread)  Only one thread can enter critical section or isolated zone Inconsistent read at each
  • 10. Immutability  Concurrency problems occurs for data that can be modified  By recognizing immutable data we can relax concurrency concerns and share it widely Inconsistent read
  • 11. EXCERISE Two users of a source control system want to work on the same file at the same time. How can we make sure that data is not lost?
  • 12. Concurrency Control  Control of mutable data that we can’t isolate  Pessimistic locking – Martin opens the file – When David wants to open the file, he’ll get denial, saying it is already in use – Conflicts avoidance
  • 13. Concurrency Control  Control of mutable data that we can’t isolate  Optimistic locking – Martin and David both edit the same file – David finishes first and saves – Then Martin saves, he’ll get an error since David has updated the file – Conflict detection
  • 14. Concurrency Control  Problem with pessimistic locking – Avoids concurrency and reduces efficiency  Optimistic locking provide more efficiency – Locks are only used on commit – The problem is what happens on conflicts  Which one to use? – Based on frequency and severity of conflicts – If conflicts are sufficiently rare or if the consequence is not great, optimistic locking works better – If conflicts are frequent and painful, pessimistic locks are better
  • 15. Preventing Inconsistent Reads  Inconsistent Reads – Martin edits the Customer class and adds some calls to the Order class. Meanwhile David edits the Order class and changes the interface. David compiles and checks in. Martin compiles and checks in. Now the shared code is broken.  How to avoid this? – Pessimistic Lock • Avoids the problem – Optimistic Locks • Detects the problem
  • 16. Preventing Inconsistent Reads  Pessimistic Lock – To read data you need a read lock and to write data you need to have write lock – Many can have read lock, but if anyone has read lock, nobody can get write lock – If anyone has write lock, nobody can get read lock – Can lead to Dead-lock  Optimistic Locks – Use timestamps or sequence number for version marker – If someone tries to commit broken code it is detected and needs manual fix
  • 17. Deadlock  When two or more are waiting for each other – David is using the Order file and is waiting for the Customer file, but Martin has the Customer file and is waiting for the Order file. – This can happen in the pessimistic approach  Solutions – Detect the deadlock and find a victim – Release resources from the victim so other can progress – Use timeouts
  • 18. Transactions  Transaction is a bounded sequence of work – Both start and finish is well defined – Transaction must complete on an all-or-nothing basis  All resources are in consistent state before and after the transaction  Example: Database transaction – Withdraw data from account – Buy the product – Update stock information  Transactions must have ACID properties
  • 19. ACID properties  Atomicity – All steps are completed successfully – or rolled back  Consistency – Data is consistent at the start and the end of the transaction  Isolation – Transaction is not visible to any other until that transaction commits successfully  Durability – Any results of a committed transaction must be made permanent
  • 20. Transactional Resources  Anything that is transactional – Use transaction to control concurrency – Databases, printers, message queues  Transaction must be as short as possible – Provides greatest throughput – Should not span multiple requests – Long transactions span multiple request
  • 21. Transaction Isolations and Liveness lock tables (or resources)  Transactions – Need to provide isolation to guarantee correctness – Liveness suffers – We need to control isolation  Serializable Transactions – – – – Full isolation Transactions are executed serially, one after the other Benefits: Guarantees correctness Drawbacks: Can seriously damage liveness and performance
  • 22. Isolation Level  Problems can be controlled by setting the isolation level – We don’t want to lock table since it reduces performance – Solution is to use as low isolation as possible while keeping correctness
  • 23. Phantoms  Description – Transaction A reads rows. Transaction B adds (INSERT) a new row. A reads rows again, but now a new row has been added, “phantom” row. – Repeatable Read isolation level
  • 24. Unrepeatable Read  Description – Transaction A reads value. Transaction B updates the value. A repeats the read but now the value is different. – Read Committed isolation level
  • 25. Dirty Read  Description – Transaction A reads and updates value. Transaction B reads the value. Then A rollbacks and resets value. B updates value. – Read uncommitted isolation level
  • 26. Isolation Level  Problems can be controlled by setting the isolation level – We don’t want to lock table since it reduces performance – Solution is to use as low isolation as possible while keeping correctness
  • 27. Transactions  Pull together several requests that the clients wants treated as if they were a single request  System Transactions – From the Application to the Database  Business Transaction – From the User to an Application – Transactions that expand more than one request
  • 28. Offline Concurrency  Need ACID properties for Business Transactions – Problem is with locking – Application won’t be scalable because long transactions will turn the database into a major bottleneck  Solution – Business Transaction are broken into short system transactions – System must provide ACID properties between system calls
  • 29. Optimistic Offline Lock (416) Prevents conflicts between concurrent business transactions by detecting and rolling back the transaction  How It Works – Validates chances to data when committed – If someone else has in the meantime updated, changes are not committed – Based on version counters – Can provide old and new version for comparisons  When to Use It – When chance of conflict is low, resolution is not too hard
  • 31. Pessimistic Offline Lock (426) Prevents conflicts between concurrent business transactions by allowing only one business transaction at a time to access data  How It Works – Prevents conflicts by avoiding them – Data is locked so it cannot be edited – Locks can be: exclusive write lock, exclusive read lock, read/write lock – Can be controlled by the application or the database  When to Use It – When data must be isolated and conflicts are likely
  • 34. EXCERISE Implement Optimistic Locking Add versions to the data and throw an exception if someone tries to change the data that has already been changed
  • 35. Example  Table customer create table customer ( id int Identity (1, 1) primary key NOT NULL, modifiedby varchar(32), modified datetime, version int, name varchar(32) )
  • 36. Example  Data Transfer Object reflects the customer table public class Customer { private int id; private Date modified; private String modifiedBy; private int version; private String name; ...
  • 37. Example  Layered Supertype for Data Mappers package is.ru.honn.data; import javax.sql.DataSource; public abstract class AbstractMapper { private String owner; private DataSource dataSource; protected AbstractMapper() { } ... }
  • 38. Example  CustomerMapper public class CustomerMapper extends AbstractMapper { public Customer find(int id) { JdbcTemplate tpl = new JdbcTemplate(getDataSource()); return (Customer) tpl.query("select * from customer where id=" + id, new CustomerRowMapper()).get(0); }
  • 39. Example  CustomerMapper public void update(Customer customer) throws ConcurrencyException { Customer current = find(customer.getId()); if (current.getVersion() > customer.getVersion()) throw new ConcurrencyException("Customer has been changed by " + current.getModifiedBy() + " at " + current.getModified() + " (version: " + customer.getVersion() + ")"); JdbcTemplate tpl = new JdbcTemplate(getDataSource()); tpl.update("update customer set name=?, modifiedby=?, modified=?, " + "version=? where id=?", new Object[] { customer.getName(), this.getOwner(), new Date(), customer.getVersion() + 1,
  • 40. Example public static void main(String[] args) { Resource resource = new FileSystemResource("data.xml"); BeanFactory beanfactory = new XmlBeanFactory(resource); CustomerMapper mapperMartin = (CustomerMapper)beanfactory.getBean("customerMa mapperMartin.setOwner("Martin"); CustomerMapper mapperDavid = (CustomerMapper)beanfactory.getBean("customerMap mapperDavid.setOwner("David"); Customer custM = mapperMartin.find(1); custM.setName("Mr. Stimpson J. Cat"); Customer custD = mapperDavid.find(1); custD.setName("Ren Hoek");
  • 41. Example try { mapperDavid.update(custD); } catch (ConcurrencyException e) { e.printStackTrace(); } try { mapperMartin.update(custM); } catch (ConcurrencyException e) { e.printStackTrace(); } }
  • 42. Summary  Concurrency – – – – – – – Concurrency can cause problems with correctness Transactions execute in execution Contexts Transactions are isolated Currency must be controlled Deadlocks can happen ACID properties and Isolation Offline concurrency  Patterns – Optimistic Offline Lock (416) – Pessimistic Offline Lock (426)