‏הצגת רשומות עם תוויות DB. הצג את כל הרשומות
‏הצגת רשומות עם תוויות DB. הצג את כל הרשומות

Concurrent transaction locking

לאור כמה שיחות עם אנשים אני מתכוון לסקור כאן בקצרה את עניין נעילות ה- DB.
אני לא מדבר על נעילות יזומות ( מפורשות ) כאשר ה – CLIENT מבקש נעילה על אזורים מסוימים ב- DATABASE ( כל VENDOR  מאפשר SYNTAX שונה וכן נעילות שונות ).
אלא אני מדבר על נעילות שה – DATABASE  עושה בעצמו על מנת לשמור על עקביות של נתונים, על מנת לנהל נתונים...

בואו ניקח את ה – scenario  הבא:
Client B (Isolation level = read committed) מנסה לקרוא נתונים מטבלה בזמן ש-  client A נמצא בתוך טרנזקציה ועושה שינויים על הטבלה הזאת. האם client B  ינעל ויחכה עד ש- client A  יסיים את הטרנזקציה?

לאחרונה שמעתי מיותר מדי אנשים את התשובה – נעילה, כלומר client B ינעל ולא יוכל לבצע את השאילתא עד ש- clinet B לא יגמור את הטרנזקציה.

ובכן – התשובה היא מאכזבת – תלוי.
במה זה תלוי – זה תלוי במימוש הטרנזקציות הפנימי של אותו db vendor.
במידה ואותו vendor   לא מנהל גרסאות מקבילות של אותה רשומה , אז כדי להבטיח read committed  הוא יצטרך לנעול את הטבלה או האזור לקריאה עד שלא יתבצע commit  על הנתונים.

ניהול של גרסאות מקבילות של אותה רשומה על מנת למנוע נעילה של הרשומה לקריאה במקביל לשינוי הוא משהו מאוד בסיסי ביכולות שאנחנו מצפים מה – database  שלנו.
אותי בתור client  לא צריך לעניין שיש clients  אחרים שמשנים את הרשומה וטרם ביצעו commit. ( במידה ואני ב- isolation read committed ).

ל – MSSQL לקח זמן להבין את זה והם הצטרפו די מאוחר לתמיכה בגרסאות שונות לאותה רשומה וכיום הם תומכים ב –row version.
התמיכה הזאת נעשית דרך הגדרת הDB בתור SNAPSHOT.והתמיכה החלה החל מ-2005.
SQL Server 2005 introduces a new snapshot isolation level to enhance concurrency for OLTP applications. In earlier versions of SQL Server, concurrency was based solely on locking, which caused blocking and deadlocking problems for some applications. Snapshot isolation, by contrast, depends on enhancements to row versioning and is intended to improve performance by avoiding reader-writer blocking scenarios.

בואו נדגים את המקרה שתארנו על שני DB שונים- mysql, mssql (2005)

יצירת הטבלה :
MySql:
create table TestLock)
       IK INT auto_increment NOT NULL,
       MyName NVARCHAR(200) not null,
       primary key (IK)
)type=innodb;
insert into TestLock(MyName) values (1);
insert into TestLock(MyName) values (2);
insert into TestLock(MyName) values (3);
insert into TestLock(MyName) values (4);

Client A:
00:
Begin;
insert into TestLock(MyName) values (5);

//Not committed!!!

Client b:
01:
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
Begin;
Select * from TestLock;

// No problem – we get committed data – no lock

Mssql :
createtable TestLock(
       IK INT IDENTITY NOT NULL,
       MyName NVARCHAR(200) not null,
       primary key (IK)
    )

insert into TestLock(MyName) values (1);
insert into TestLock(MyName) values (2);
insert into TestLock(MyName) values (3);
insert into TestLock(MyName) values (4);

we will distinguish between two db types – snapshot on and off.

Snapshot off:
Client A:
00:
set transaction isolation level  read committed  set implicit_transactions off 
go
set implicit_transactions on 
go
insert into TestLock(MyName) values (5);

//Not committed!!!

Client b:
01:
set transaction isolation level  read committed  set implicit_transactions off 
go
set implicit_transactions on 
go
Select * from TestLock;
//Lock – because mssql lock the last rows of the table since it need to add data to them.
Mssql doesn't lock the table. It lock the area that it need (it depend on the block size … ), so if we limit the select query range to range that not include the lock area the read will not be blocked.
For example:
Select * from testlock where IK < 4
//No lock !!!!
If we turn on the snapshot (in the db and in the transaction committed_snapshot) – No lock needed, so select all will work even there is a parallel uncommitted transaction that insert data to the table.


DB- is RDBMS is the only one solution?



Most leading DB today's are RDBMS (relational database management system) .
The "relation modeling" concepets frist  formulated and proposed in 1969 by E.F. Codd .
The idea is to describe a database as a collection of predicates over a finite set of predicate variables, describing constraints on the possible values and combinations of values. The content of the database at any given time is a finite (logical) model of the database, i.e. a set of relations, one per predicate variable, such that all predicates are satisfied. A request for information from the database (a database query) is also a predicate. (http://en.wikipedia.org/wiki/Relational_model).
However relation modeling cannot suite to any kind of application.
In some application there is no a restrict schema and the model is flexible.
In those cases the traditional "relation modeling" is a bad solution.
Skinny data models are one of the most notorious and problematic abuses of relational Database systems. Unfortunately, they’re sometimes necessary. A skinny data model basically turns each table into a generic data structure that can store sets of name and value pairs, much like a properties file in Java.
The relational model that these databases are built on, however, was designed many years ago, when the World Wide Web and Internet were unheard of. Although the strict schema-based structure these databases adhere to is required in some web applications, such as transaction systems, it is not a good fit for many modern web projects, such as blogs, wikis, and discussion forums, which by their nature are a better fit to a document-oriented database.
Another problem in RDBMS is making schema changes or adding indexes to a database with more than 10 - 20 million rows completely locks the database for hours at a time.
Different solutions are available. Some of them by using in specific db solutions and implemented based on existing RDBMS.
I want to focus on 2 solutions (there are a lot of alternatives), but first disclaimer.
Disclaimer:
When you decide to fall from the standard does it only after you consult with at least two different consultants, who are consider as leaders in these fields.
Falling from the standard has major impacts:
 Impact on - employee training, scalability, performance, bugs and design solutions. When you take the "off road" you are missing all the benefit in the primary road like -  big community, Google answers, books, solutions and so on. So be careful when you take the off road.
If you chose to take the "off road" make sure you isolate you solution as much as possible.
Famous computer scientist Butler W. Lampson (who envisioned the modern personal computer at Xerox way back in 1972) once said, “All problems in computer science can be solved by another level of indirection.” So try to encapsulate your "off road" solution as much as possible. There is a big chance that you will decide to go back to the main road in the future.
Use a solution above RDBS:
This kind of approach pulished by " FriendFeed " they choose to implement their own soltuon above MySql DB - http://bret.appspot.com/entry/how-friendfeed-uses-mysql
In the article they explain why they chose to implemrt thir own solution and not use in an existing one:
Lots of projects exist designed to tackle the problem storing data with flexible schemas and building new indexes on the fly (e.g., CouchDB). However, none of them seemed widely-used enough by large sites to inspire confidence. In the tests we read about and ran ourselves, none of the projects were stable or battle-tested enough for our needs (see this somewhat outdated article on CouchDB, for example). MySQL works. It doesn't corrupt data. Replication works. We understand its limitations already. We like MySQL for storage, just not RDBMS usage patterns.
Use an existing solution for example – CoachDB:
CouchDB is a document-oriented database management system, released under the open source Apache License. In contrast to most database systems, it stores data in a schema-free manner. This means that, unlike traditional SQL-based databases, there are no tables and columns, primary and foreign keys, joins, and relationships. Instead, CouchDB stores data in a series of documents and offers a
JavaScript-based view model for aggregating and reporting on the data.
Coachdb has many more advantage like replication, local data, versioning and many more.
It has a big community and good books.
One small comment – the local data feature is one of his "sexy features". This allows you to install light DB on the client side. Give the client the option to work offline and sync the client db with the server replication later. The same idea as a version control.