0% found this document useful (0 votes)
29 views9 pages

DBMS Module 5

The document discusses various database concepts including the Two-Phase Locking Protocol for ensuring serializability in transactions, deadlock prevention protocols like Wait-Die and Wound-Wait, and the four major categories of NoSQL systems. It also covers multiple granularity locking, MongoDB CRUD operations, the Neo4j data model, concurrency control based on timestamp ordering, and the CAP theorem related to NoSQL databases. Additionally, it explains the characteristics and operations of document-based NoSQL systems and graph databases, particularly Neo4j.

Uploaded by

wosurya1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views9 pages

DBMS Module 5

The document discusses various database concepts including the Two-Phase Locking Protocol for ensuring serializability in transactions, deadlock prevention protocols like Wait-Die and Wound-Wait, and the four major categories of NoSQL systems. It also covers multiple granularity locking, MongoDB CRUD operations, the Neo4j data model, concurrency control based on timestamp ordering, and the CAP theorem related to NoSQL databases. Additionally, it explains the characteristics and operations of document-based NoSQL systems and graph databases, particularly Neo4j.

Uploaded by

wosurya1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Module 5 :

Question 1 : What is the two phase locking protocol ? How does it Guarantee
Serializability.
Two-Phase Locking Protocol (2PL)
1. It is a concurrency control protocol used to ensure conflict serializability in
database transactions
2. It requires that all locking operations occur in two distinct phases:
a) Growing Phase
● A transaction may acquire locks on data items
● No locks are released during this phase
b) Shrinking Phase
● A transaction may release locks
● No new locks can be acquired after releasing even one lock
How 2PL Guarantees Serializability
1. Ensures locking and unlocking are strictly separated, so transactions behave like
in a serial order
2. Prevents conflicting operations by enforcing exclusive access
3. Guarantees that the transaction schedule is conflict-serializable
4. Conflicting operations are isolated, preserving correctness of concurrent
transactions
Example

T1: Lock A Read A Lock B Read B Unlock A Unlock B

T2 must wait until T1 releases locks before accessing A or B, enforcing serial execution
Drawbacks
● May lead to deadlocks if transactions wait on each other's locks
● Increases overhead due to lock management
Question 2 : Describe the wait-die and wound-wait protocols for deadlock prevention.
Deadlock Prevention Using Timestamps :
Both Wait-Die and Wound-Wait protocols use transaction timestamps to prevent circular
wait conditions that lead to deadlock.
1. Wait-Die Protocol :
Older transaction may wait, younger one is aborted (dies)
If younger transaction requests a lock held by older it dies (rolled back)
If older transaction requests a lock held by younger it waits
Example: T1 (timestamp 10) holds lock, T2 (timestamp 20) requests T2 is aborted
Advantage: Prevents deadlock by avoiding circular wait
2. Wound-Wait Protocol
Older transaction wounds (forces rollback of) younger transaction
If older transaction requests a lock held by younger younger is aborted
If younger transaction requests a lock held by older it waits
Example: T1 (timestamp 5) requests lock held by T2 (timestamp 15) T2 is aborted
Advantage: Avoids starvation, older transactions complete faster.

Question 3 : List and explain the four major categories of NOSQL system.
Four Major Categories of NoSQL Systems :
1. Key-Value Stores :
Store data as a collection of key-value pairs
Keys are unique identifiers; values can be strings, JSON, or binary
Simple and fast; best for caching and session storage
Example: Redis, DynamoDB, Riak
2. Document Stores :
Store data as documents (usually JSON, BSON, or XML format)
Documents are self-describing and can contain nested structures
Ideal for content management systems, blogs, catalogs
Example: MongoDB, CouchDB
3. Column-Family Stores (Wide-Column Stores) :
Store data in tables with rows and dynamic columns grouped into families
Optimized for read/write operations on large datasets
Suitable for analytical applications and time-series data
Example: Apache Cassandra, HBase
4. Graph Databases :
Store data as nodes and edges representing entities and relationships
Ideal for querying highly connected data like social networks or recommendation
engines
Use graph-specific query languages (like Cypher in Neo4j)
Example: Neo4j, Amazon Neptune.

Question 4 : What is multiple Granularity locking? How is it implemented using intension


locks ? Explain.
Multiple Granularity Locking :
1. Used to manage locks efficiently when data is organized in a hierarchical
structure (e.g., database table page record)
2. Allows transactions to lock different levels (granularities) of data items
3. Improves concurrency and reduces locking overhead
4. Helps avoid locking entire database/table when only part of it is accessed
Why Needed?
● Locking at a coarse level (e.g., entire table) reduces concurrency
● Locking at a fine level (e.g., rows) increases overhead
● Multiple granularity locking provides a balanced approach
Intention Locks :
To support multiple granularity, intention locks are used at higher levels to indicate a
lower-level lock will be requested.
Types of Intention Locks:
1. IS (Intention Shared): Transaction intends to acquire shared locks on lower-level
nodes
2. IX (Intention Exclusive): Transaction intends to acquire exclusive locks on lower-
level nodes
3. S (Shared): Transaction reads the item, no modification
4. X (Exclusive): Transaction reads and writes the item
5. SIX (Shared and Intention Exclusive): Shared lock at current level, exclusive
locks at lower levels
How It Works (Implementation) :
● Before locking a node (e.g., a record), a transaction must first set intention locks
on all higher levels (e.g., page, table)
● Prevents conflict between transactions working at different levels
● Ensures lock compatibility and deadlock avoidance
Example:
● To lock a row in exclusive mode:
o Set IX on table
o Set IX on page
o Set X on row
● Other transactions will see IX on table and know someone may lock rows,
avoiding conflicts.
Question 5 : Discuss the following MongoDB CRUD operations with their formats :
[Link], 2. Delete, 3. Read.
1. Insert Operation :
Used to insert documents into a MongoDB collection.
Syntax (Single Document):
[Link]({ key1: value1, key2: value2, ... })
Syntax (Multiple Documents):
[Link]([
{ key1: value1, key2: value2 },
{ key1: value3, key2: value4 }
])
Example:
[Link]({ name: "Alice", age: 22, course: "DBMS" })

2. Delete Operation :
Used to delete one or more documents from a collection.
Syntax (Delete One):
[Link]({ condition })
Syntax (Delete Many):
[Link]({ condition })
Example:
[Link]({ name: "Alice" })

3. Read Operation (Find) :


Used to retrieve documents from a collection.
Syntax (Find All):
[Link].find()
Syntax (With Condition):
[Link].find({ condition })
Example:
[Link].find({ course: "DBMS" })

Question 6 : Briefly discuss about Neo4j data model.


Neo4j Data Model (Graph-Based) :
Neo4j is a graph database that represents data using a property graph model, focusing
on nodes, relationships, and properties.
1. Nodes :
Represent entities (e.g., Person, Product, City)
Equivalent to rows in relational databases
Can have one or more labels (types)
Contain properties (key-value pairs)
Example :
(:Person {name: “Alice”, age: 30})
2. Relationships
Represent connections between nodes
Are directed (e.g., KNOWS, WORKS_FOR)
Can also have properties
Describe the type and meaning of connection
Example:
(:Person {name: “Alice”}) –[:FRIEND_OF]-> (:Person {name: “Bob”})
3. Properties
Stored as key-value pairs on both nodes and relationships
Hold metadata or actual data (e.g., age, date, role)
4. Labels and Types
Labels group nodes of the same kind
Relationship types define connection mmeanin
Useful for indexing and fast querying
5. Cypher Query Language
Neo4j uses Cypher, a declarative query language
Syntax resembles ASCII-art of the graph
Example Query:
MATCH (p:Person)-[:FRIEND_OF]->(f)
WHERE [Link] = “Alice”
RETURN [Link]
Advantages of Neo4j Model :
Ideal for highly connected data
No joins needed — relationships are stored directly
Efficient for social networks, recommendation engines, fraud detection.

Question 7 : Demonstrate the Concurrency control based on Timestamp ordering.


Concurrency Control Based on Timestamp Ordering
1. Purpose
● Ensures serializability of concurrent transactions using timestamps
● Avoids conflict without using locks (non-locking protocol)
2. Basic Concept
● Each transaction Ti is assigned a unique timestamp TS(Ti) when it starts
● Timestamp represents logical start time
● Older transactions get smaller timestamps
3. Data Item Timestamps
● For each data item X, maintain:
o read_TS(X): largest timestamp of any transaction that read X
o write_TS(X): largest timestamp of any transaction that wrote X
4. Rules for Operation Execution

a) Read(X) by Ti
● If TS(Ti) < write_TS(X) Abort Ti (reading outdated data)
● Else Allow read and update read_TS(X)

b) Write(X) by Ti
● If TS(Ti) < read_TS(X) Abort Ti (newer transaction already read X)
● If TS(Ti) < write_TS(X) Abort Ti (newer transaction already wrote X)
● Else Allow write and update write_TS(X)
5. Example
● T1 (TS = 5), T2 (TS = 10)
● T1 writes X allowed
● T2 reads X allowed
● T1 tries to write X again aborted (TS(T1) < read_TS(X))
6. Advantages
● Ensures serializability
● Avoids deadlocks (no locks used)
7. Disadvantages
● Cascading aborts possible
● Overhead of maintaining timestamps
● Starvation of older transactions possible
Question 8 : Why Concurrency control is needed? Demonstrate with an example.
1. Concurrency control ensures correct execution of multiple transactions
running simultaneously
2. Prevents problems like lost updates, dirty reads, uncommitted data
access, and inconsistent analysis
3. Maintains database consistency, isolation, and serializability

4. Avoids conflicts when transactions access the same data concurrently


5. Ensures transactions do not interfere or overwrite each other’s changes
Example :
T1 reads balance = 5000 from account A
T2 reads same balance = 5000 from account A
T1 deducts 1000 and writes new balance = 4000
T2 deducts 500 and also writes new balance = 4500
Final balance should be 3500 but becomes 4500 due to lost update
Concurrency control prevents this by scheduling operations safely or locking data items

Question 9 : What is NOSQL? Explain the CAP theorem.


What is NoSQL?
1. NoSQL stands for “Not Only SQL”
2. It refers to non-relational databases designed to handle large-scale,
unstructured, or semi-structured data
3. Supports flexible schema, high scalability, and distributed data storage
4. Suitable for big data, real-time web apps, social networks, and IoT
5. Types include key-value stores, document stores, column-family stores,
and graph databases
6. Examples: MongoDB, Cassandra, Redis, Neo4j
CAP Theorem (Consistency, Availability, Partition Tolerance) :
1. Proposed by Eric Brewer, states that a distributed system can achieve
only two out of three properties at the same time
2. Consistency ©: All nodes see the same data at the same time
3. Availability (A): Every request gets a response, even if some nodes fail
4. Partition Tolerance (P): System continues to work despite network
failures or message loss between nodes
5. No distributed system can guarantee all three simultaneously
6. Systems must choose trade-offs:
CP: Consistent and Partition tolerant (e.g., HBase)
AP: Available and Partition tolerant (e.g., Cassandra)
CA: Consistent and Available (only possible if no partition occurs).

Question 10 : What are document based NOSQL systems? Explain basic operations
CRUD in MongoDB.
Document-Based NoSQL Systems :
1. Store data in the form of documents, typically using JSON or BSON
format
2. Each document is a self-contained unit with fields and values (like a
record)
3. Schema-less or flexible schema – different documents can have
different structures
4. Ideal for applications needing fast access, flexible data, or hierarchical/
nested data
5. Example systems: MongoDB, CouchDB, RavenDB
6. Best used for content management, product catalogs, blogging
systems, etc.
Basic CRUD Operations in MongoDB :
1. Create (Insert) :
insertOne(): inserts a single document
insertMany(): inserts multiple documents
[Link]({ name: “Alice”, age: 21 })
2. Read (Query) :
Find(): retrieves documents
Supports conditions, projections, sorting, etc.
[Link].find({ age: { $gt: 18 } })
3. Update :
updateOne(): updates the first matching document
updateMany(): updates all matching documents
[Link]({ name: “Alice” }, { $set: { age: 22 } })
4. Delete :
deleteOne(): deletes the first matching document
deleteMany(): deletes all matching documents
[Link]({ name: “Alice” })

Question 11 : What is NOSQL Graph database? Explain Neo4j.


NoSQL Graph Database :
1. A NoSQL graph database stores data using nodes (entities), edges
(relationships), and properties (key-value pairs)
2. Designed based on graph theory, enabling efficient modeling of complex
relationships
3. Nodes represent real-world objects (e.g., Person, Product)
4. Edges represent connections (e.g., FRIEND_OF, BOUGHT, LIKES) and can
also store properties
5. Ideal for highly connected data such as social networks, fraud detection,
recommendation engines.
6. Unlike relational databases, joins are not required—relationships are
stored directly, making traversals fast.
Neo4j – A Popular Graph Database :
1. Neo4j is the most widely used native graph database.
2. Stores data in property graph model :
Nodes with labels and properties.
Directed relationships with types and properties.
3. Uses Cypher query language (declarative, pattern-based) :
Example:
CREATE (a:Person {name: “Alice”})
CREATE (b:Person {name: “Bob”})
CREATE (a)-[:FRIEND_OF]->(b)
4. Allows pattern matching for queries :
MATCH (p:Person)-[:FRIEND_OF]->(f)
WHERE [Link] = “Alice”
RETURN [Link]
5. Supports ACID transactions, indexing, full-text search, and scalability
6. Common use cases:
Social networks (people and connections)
Knowledge graphs
Access control systems
Real-time recommendations

You might also like