Is a strict schedule always serializable? - database

In DBMS, is a strict schedule always serializable?
If the answer is no, then please provide an example of a strict schedule which is not serializable.

(For the following, I assume conflict-serializability.)
No, a strict schedule is not always serializable. The following schedule is strict (no reading from or overwriting of data items before a commit), but not serializable (there are two conflict pairs: r_1[x] and w_2[x] & w_2[x] and w_1[x], but they are ordered differently). The operations with subscript 1 belong to transaction T_1, those with subscript 2 to transaction T_2.
r_1[x] w_2[x] c_2 w_1[x] c_1
Contrary to the answer given above, a serializable schedule is not necessarily strict. See the following example:
w_1[x] w_2[x] c_2 r_1[y] c_1
Transaction T_2 overwrites the w_1[x] of T_1 before the commit of T_1. Nevertheless, the schedule is (conflict-)equivalent to the serial schedule T_1 T_2.

Every serializable schedule is strict.
But every strict schedule is not serializable.
A Schedule is a serializable schedule if its outcome is equal to the outcome of its transactions executed serially i.e. without interleaving the transactions.
Whereas, a strict schedule is the one in which some other transaction T2 cannot read or write the data written by T1 unless it is committed.
So for example,
In other words in Strict schedule T2 can read the data which is being read by T1, but once T1 writes the data, from that time to the time it commits, T2 cannot read or write the data.

Related

Datastore Entity read (keys_only) inside ndb transactions

I have a question regarding the datastore entity reads inside a ndb transaction.
I know that when we read an entity inside an ndb transaction, that specific entity gets locked and no other thread can put/update/write the same entity because it will result in a contention error.
That totally makes sense.
However, what happens when we read only the key of an entity instead of the whole entity itself inside the transaction? This can be done by passing keys_only flag as True in ndb.query().fetch()
In that case, will the entity again get locked?
The Datastore documentation for Transaction Locks says:
Read-write transactions use reader/writer locks to enforce isolation and serializability.
And it does not mention any situation with the specifics of the use of keys_only during transactions. So I would assume that the same applies to that situation, which does make sense if you consider that you are still making a read never the less, you are just ignoring the data.
That being said, maybe this is something that could be improved into Datastore, or even made clear in the documentation. If you wish, you could consider opening a Feature Request for Google to implement that by following this link.
In general, it's better to think of transactions in terms of their guarantee - serializability - rather than their implementation details - in this case, read/write locks. The implementation details (how queries are executed, locking granularity, exactly what gets locked, etc) can potentially change at any time, while the guarantee will not change.
For this specific question, and assuming the current implementation of Firestore in Datastore Mode: to ensure serializability, a keys-only query in a transaction T1 locks the ranges of index entries examined by the query. If such a query returned a key K for entity E, then an attempt to delete E in a different transaction T2 must remove all of E's index entries, including the one in the range locked by the query. So in this example T1 and T2 require the same locks and one of the two transactions will be delayed or aborted.
Note that there are other ways for T2 to conflict with T1: it could also be creating a new entity that would match T1's query (which would require writing an index entry in the range locked by T1's query).
Finally, if T2 were to update (rather than delete) E in a way that does require any updates to index entries in the range examined by T1's query (e.g., if the query is something like 'select * from X where a = 5' and the update to E does not change the value of it's 'a' property) then T1 and T2 will not conflict (this is an optimisation - behaviour would still be correct if these two transactions did conflict, and in fact for "Datastore Native" databases they can conflict).

what is difference between atomicity and isolation in ACID properties?

the difference between atomicity and isolation of DBMS is somewhat vague so i am asking for a clear difference between the two ?
Atomicity and isolation, are ensured in classical database transactions by using a commit protocol. This protocol is used to turn temporary storage into permanent storage - that is, the updates to the transaction's data are not visible until the commit protocol validates the stored data. Note that it is the presence of a commit record in the database log which effectively validates the transaction's data.
Atomicity describes the behavior within an individual transaction, and Isolation describes the behavior among one or more transactions.
Atomicity: Either all of the database operations in a transaction are executed or none are.
Isolation: Each transaction appears to execute in isolation from other transactions.
Atomicity: all operations in a transaction are executed as a single unit.
Isolation: transactions don't see intermediate changes of other transactions.
According to these definitions, Isolation is just a part of Atomicity. Because when other transactions can see intermediate impact of our transaction, operations of our transaction cannot be considered as a single atomic unit.
Atomicity means if ONE transaction contains multiple operations, either ALL of them or NONE are performed.
The counterintuitive point is, this definition still allows for interleaving the operations with other transactions. As a result, atomicity itself does not guarantee isolation.
Example: Transaction A and B both contain two operations:
Transaction A:
A1: Read Alice's account balance
A2: Add 100 and write it back to Alice's balance
Transaction B:
B1: Read Alice's account balance
B2: Add 50 and write it back to Alice's balance
Suppose now Alice's balance is 0, and transactions A and B happen concurrently in the order of A1-B1-B2-A2, what is the final balance? It's 100.
Both A and B are atomically committed, in the sense that all their operations occurred. But without proper isolation, the result is probably not what you want.

What is Conflict serializability?

I am reading in google about the conflict serializability and serializable.
But I am not getting the correct definition and the difference between serializable and conflict serializability.
I am getting only one thing.That is Conflict serializability implies serializable.
In many things they told most over the serializable and conflict serializable are same.
Can anyone please explain what is conflict serializable and difference between serializability and serializable with examples.
Thanks for advance !
I was found a answer for my question.
Serializable means the transaction is done in serial manner. That means if the scheduling is done, but the transactions are not use the same variable for read and write.
Example:-
T1 T2
Read(X)
Read(y)
Write(X)
Write(Y)
In this example, the two transactions are does not use the shared variable.
So, in here there is no conflict.
Conflict serializability means the transactions in done in concurrently. The two transactions are used the same variable, the output of the transaction is conflict.
Example:-
T1 T2
Read(X)
Read(X)
Write(X)
Write(X)
Read(Y)
Write(Y)
Read(Y)
Write(Y)
In this example the two transaction T1, and T2 uses the same variable.
So, the transaction T2 writes the X before T1 write. After the T1 writes the X. In here there is no use of transaction T2 writes. This is conflict serializability.
To answer your question, I'll first explain few terminologies, quoting a line from the Operating Systems book by Galvin.
Serial Schedule : A schedule in which each transaction is executed atomically is called a serial schedule just like it's shown in Premraj's answer.
Likewise, when we allow the transactions to overlap their execution,
i.e they aren't atomic any longer, they're called Non Serial
Schedule.
Conflicting Operations : This helps to see whether a Non Serial schedule is(not) equivalent to the one represented by Serial Schedule. Two consecutive operations are said to be in conflict if they access the same data item and at least one of them is write operation.
We try to find out and swap all the Non Conflicting Operations, and if the given Non Serial Schedule can be transformed to a Serial Schedule, we say the given schedule was Conflict Serializable.
Schedule : is a bundle of transactions.
Serializability is a property of a transaction schedule. It relates to the isolation property of a database transaction. Serializability of a schedule means equivalence to a serial schedule.
Conflict Serializable can occur on Non-Serializable Schedule on following 3 conditions:
They must belong to different transactions.
They must operate on same value
at least one of the should have write operation.

Database locks and isolation level in JPA

I am not sure in understanding the Database Locks. I am using the repeatable read isolation level. According to wikipedia it keeps read and write locks (acquired on selected data) until the end of the transaction.
Let's consider the following scenario: "Let's have two threads A, B. Thread A begins a transaction. Let's say thread A retrieves a list of all users from table User. (I am expecting here that: Thread A acquired read&write locks on all users ??) Thread B begins another transaction, retrieves one concrete User u from table User and updates the User u then commits the transaction (Since A acquired the locks, does the Thread B has to wait until A commits the transaction ??)"
Is the describes behavior to expect if using JPA ?
Is the lock acquired if the Thread A reads the users outside a transaction (Let's say if I am using the Extended Peristence Context) ??
You are confusing the logical isolation level with its physical implementation. The SQL standard defines the four isolation levels Serializable, Repeatable Read, Read Committed and Read Uncommitted and the three ways in which serializability might be violated: dirty read, nonrepeatable read and phantom read.
How a particular DBMS achieves each level of isolation is an implementation detail which differs between each DBMS. Some DBMS may use a locking strategy which means that read locks are used that means writers are blocked until a transaction completes. Other DBMS may use other strategies, such as multi-version concurrency control, which means readers and writers do not block each other. In order to maximize the performance and scalability of your application you will need to code to the particular implementation of the DBMS you are using.

What is the difference between "conflict serializable" and "conflict equivalent"?

In database theory, what is the difference between "conflict serializable" and "conflict equivalent"?
My textbook has a section on conflict serializable but glosses over conflict equivalence. These are probably both concepts I am familiar with, but I am not familiar with the terminology, so I am looking for an explanation.
Conflict in DBMS can be defined as two or more different transactions accessing the same variable and atleast one of them is a write operation.
For example:
T1: Read(X)
T2: Read (X)
In this case there's no conflict because both transactions are performing just read operations.
But in the following case:
T1: Read(X)
T2: Write(X)
there's a conflict.
Lets say we have a schedule S, and we can reorder the instructions in them. and create 2 more schedules S1 and S2.
Conflict equivalent: Refers to the schedules S1 and S2 where they maintain the ordering of the conflicting instructions in both of the schedules. For example, if T1 has to read X before T2 writes X in S1, then it should be the same in S2 also. (Ordering should be maintained only for the conflicting operations).
Conflict Serializability: S is said to be conflict serializable if it is conflict equivalent to a serial schedule (i.e., where the transactions are executed one after the other).
From Wikipedia.
Conflict-equivalence
The schedules S1 and S2 are said to be conflict-equivalent if the following conditions are satisfied:
Both schedules S1 and S2 involve the same set of transactions (including ordering of actions within each transaction).
The order of each pair of conflicting actions in S1 and S2 are the same.
Conflict-serializable
A schedule is said to be conflict-serializable when the schedule is conflict-equivalent to one or more serial schedules.
Another definition for conflict-serializability is that a schedule is conflict-serializable if and only if its precedence graph/serializability graph, when only committed transactions are considered, is acyclic (if the graph is defined to include also uncommitted transactions, then cycles involving uncommitted transactions may occur without conflict serializability violation).
Just two terms to describe one thing in different ways.
Conflict equivalent: you need to say Schedule A is conflict equivalent to Schedule B. it must involve two schedules
Conflict serializable: Still use Schedule A and B. we can say Schedule A is conflict serializable. Schedule B is conflict serializable.
We didn't say Schedule A/B is conflict equivalent
We didn't say Schedule A is conflict serializable to Schedule B
If a schedule S can be transformed into a schedule S´ by a series of swaps of non-conflicting instructions, we say that S and S´ are conflict equivalent.
We say that a schedule S is conflict serializable if it is conflict equivalent to a serial schedule.
Conflict serializable means conflict equuivalent to any serial schedule.
Conflict Equivalent Schedules: if a Schedule S can be transformed into a schedule S' by a series of swaps of non conflicting instructions, we say that schedule S & S' are conflict equivalent.
Conflict Serializable Schedule: Schedule S is conflict serializable if it is conflict equivalent to a serial schedule.
Definitions have already been explained perfectly, but I feel this will be very useful to some.
I've developed a small console program (on github) which can test any schedule for conflict serializability and will also draw a precedence graph.
If there is at least one conflict equivalent schedule for considered transaction schedule, it is conflict serializable.

Resources