Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
The-Manga-guide-to-databases.pdf
Скачиваний:
8
Добавлен:
17.11.2024
Размер:
36 Мб
Скачать

What Is a Lock?

 

In a database,

when they

operations by

access the

many users are

database

controlled so

concurrently.

that nothing goes

 

wrong...

 

I see.

You mean “lock" as in "lock and key"?

For that purpose, a method called a lock is used.

Exactly.

You lock data to prevent it from being erroneously processed.

Data

Data

Let me explain using the previous example.

You've drawn so many diagrams!

That's news to me.

Flip

Flip

Let's Operate a Database!  131

Andy locks the data before

When Becky tries to perform her

performing a series of

operations, she must wait until

operations.

Andy is finished.

30

30

 

apples

apples

 

 

 

 

 

 

 

I’m

 

 

waiting in

 

 

a queue!

 

Waiting for Andy to

 

 

 

 

finish processing.

add 10.

 

 

40 apples

40

 

apples

 

 

add 10.

Becky can only perform

50 apples

her operations after

 

Andy is through with his

 

operations.

 

 

50 apples

As a result, the database yields the value 50, as it

should.

Oh, boy! Cain, I'm impressed.

Yeah, yeah.

132  Chapter 5

Database

So, I understand that

That finalization is

called a commit

operations on the

operation.

database are finalized

Read

when each transaction

 

is processed

 

correctly.

Write

Although a lock has its

So we use different

types of locks

own role in a database,

depending on the

locking should not

situation.

be overused. It can

 

hinder the purpose of a

 

database: sharing data

 

with a lot of people.

 

How do we do that?

For example, you can

 

but cannot

While a shared lock

perform

use a shared lock

is applied, other users

a write

for a read operation

can read the data...

operation

when it is the only

 

on it.

operation needed.

 

 

 

 

 

Shared

 

 

lock

 

Shared

 

 

lock

 

Write?

Read?

Let's Operate a Database!  133

When performing a write operation, a user applies an exclusive lock.

Exclusive

lock

When an exclusive

 

lock is applied,

 

other users cannot

 

read or write data.

Oh, my.

Exclusive

lock

Read? Write?

I see that there

 

 

are different

 

 

types of locks.

 

When a lock is used to

 

 

control two or more

 

transactions, that is called

 

 

concurrency control.

That

 

 

makes

 

 

sense.

 

 

 

as many users as

 

In a database,

possible to use a

By the way,

database at one time

Cain!

concurrency

while preventing

 

control allows

data conflicts from

 

 

occurring.

Yes, Tico?

134  Chapter 5

You must have studied a lot!

No, not really.

Is that

Isn't he

your

dependable,

impression?

Ruruna?

I suppose so…

Now go on!

All right, I will.

Let me get

In some cases, concurrency

back to the

control with a lock may cause

previous

a problem.

topic.

 

Suppose Andy has

And suppose Becky

has applied an

applied an exclusive

exclusive lock on

lock on the apple

the strawberry

data.

data.

 

Andy

Becky

 

For example…

Apple data

Strawberry data

Uh-huh.

Next, Andy may

and Becky may

try to apply an

try to apply an

exclusive lock on

exclusive lock

strawberry data,

on apple data.

locked

locked

 

Apple data

Strawberry

data

 

What would happen then?

Well…

Since each of them

neither one can

must wait for the

proceed with

lock applied by the

any operation.

other user to be

Is that it?

released,

 

Let me think...

They cannot do anything.

Apples

Exactly!

S trawberries

This situation,

For example, you

 

which is called

 

a deadlock,

can look for

When you cancel

cannot be

transactions that

a transaction,

solved unless

have been queued

it’s called a

one of the

for a certain length

rollback.

locks is

of time...

 

released.

 

 

C A

NC EL

and cancel them!

Rollback?

 

136  Chapter 5

You mean you can

Yes! For example, if a

cancel all the

transaction to "discount fruits

operations in a

priced less than or equal to

transaction at

150G" has failed,

once?

 

 

operations for apples and

 

strawberries must be canceled

 

altogether, right?

 

I see.

So the database

 

If anything has

behaves as if no

 

operation had

 

occurred during

been performed

 

a transaction to

at all?

 

disable finalization,

 

 

 

 

then a rollback

 

 

is performed

 

 

instead of

 

Yes, sort of.

a commit

 

operation.

 

 

Operation

Operation

That's right.

A transaction always ends with a commit or rollback operation.

All right! Now, Tico, dear, the next topic is…

There are no half measures, in other words.

Let's Operate a Database!  137