67
1096 Understanding InterBase Transactions Bill Todd The Database Group, Inc.

1096 Understanding InterBase Transactions Bill Todd The Database Group, Inc

Embed Size (px)

Citation preview

1096Understanding InterBase

Transactions

Bill Todd

The Database Group, Inc.

What is a Transaction

Atomicity

Consistency

Isolation

Durability

Atomicity

All changes to all tables made within the transaction must succeed or fail as a single atomic unit

ConsistencyDatabase will always be left in a logically

consistent state

On restart after a crash all active transactions are automatically rolled back

The database can never contain changes made by a transaction that did not commit

Isolation

Your transaction cannot see changes made by other transactions that have not committed

Durability

After a transaction commits the changes are a permanent.

The changes cannot be lost or undone.

Transaction Isolation Level

Your transaction’s isolation level controls when it will see changes made by other transactions

SQL Standard Isolation Levels

Read uncommitted (dirty read)

Read committed

Repeatable read

Serializable

Read Uncommitted

Your transaction can see changes made by other transactions that have not committed

Most databases do not support read uncommitted

Read Committed

Your transaction can see changes made by other committed transactions

Repeatable ReadIf you execute the same SELECT more than once

within your transaction you will get the same value for each row returned the first time in each subsequent result set

You will also get any new records that were inserted after the prior execution of the SELECT

Serializable

If you execute the same SELECT more than once within your transaction you will get the same result set each time

InterBase Isolation Levels

Snapshot = ANSI serializable

Read committed

How Versioning WorksAll data access takes place within a transaction

Each transaction is assigned a unique number

Each row version has the number of the transaction that created it

Transaction Inventory Pages

TIP tracks the state of all interesting transactions

Transaction can have four states– Active– Committed– Rolled back– Limbo

Starting a Snapshot Transaction

Transaction gets a copy of the TIP

TIP copy used to determine the state of other transactions at the moment the snapshot transaction started

Starting a Read Committed Transaction

Gets pointer to the TIP cache (TPC)

TPC pointer used to determine the current state of other transactions

Updating a RowCheck the TIP to see if there are active

transactions with a lower transaction number

If yes, create a new row version

If no, update the existing row

Reading a Row - Snapshot

Most recent version

Committed at time the reading snapshot transaction started

When a snapshot transaction reads it ignores all row versions committed after it started

Tran 90 Reads Row 123

Transaction Options

Access Mode

Lock resolution

Table reservation

Access ModeTransactions can be read only or read/write

Default is read/write

Read only transactions have lower overhead

Read only read committed transactions do not stop garbage collection

Setting Access Mode With IBX

For a read only transaction add the word “read” without the quotes to the IBTransaction Params property

For a read/write transaction add “write” to the Params property

Lock ResolutionWait

– A transaction with the wait option that tries to update a row your transaction has locked will wait until your transaction ends

NoWait– The transaction will raise an exception when

it tries to update a locked row

Table Reservation

Allows your transaction to lock tables when it starts

This guarantees your transaction access to all of the tables it needs

Table ReservationsShared, lock_read

Any transaction can update. Any transaction can read.

Shared, lock_write

Snapshot or read committed can update. All can read

Protected, lock_read

No updates. Any transaction can read

Protected, lock_write

No updates. Snapshot & read committed can read

IBTransaction.Params

Snapshot transaction reserving EMPLOYEE for protected, lock_read

Table name is case sensitive

concurrency nowait protected lock_read=EMPLOYEE

IBTransaction.Params

A read committed transaction that reserves EMPLOYEE for shared, lock_read

read_committed nowait shared lock_read=EMPLOYEE

Reserving Multiple Tables

concurrency nowait protected lock_read=EMPLOYEE shared lock_read=SALARY_HISTORY

IBTransaction Params Keywords

Isolation Levelconcurrency Snapshot transaction isolation

read_committed Read committed transaction isolation

consistency Consistency (table stability) isolation

IBTransaction Params Keywords

Access Moderead Read only access

write Read/write access

IBTransaction Params Keywords

Lock Resolutionwait Wait if the record to be updated is locked by

another transaction

nowait Return an error if the record to be updated is locked by another transaction

Keywords Isolation Levelshared

lock_write=TableName

Snapshot & read committed can update. All read

shared

lock_read=TableName

Any can update.

Any can read.

protected

lock_write=TableName

No update. Snapshot & read committed read

protected

lock_read=TableName

No update. Any can read

Ending a TransactionCommit – changes the transaction’s state on the

TIP from active to committed

Rollback – can degrade perfomance– If < 100,000 changes IB undoes changes and

commits– If 100,000 changes or more IB changes state

on the TIP from active to rolled back

Next Transaction

The number that will be assigned to the next transaction that starts

OITOldest Interesting Transaction

The oldest transaction whose state is not committed– Oldest Active Transaction– Oldest rolled back transaction– Oldest limbo transaction

Normally OIT = OAT and they advance when the OAT commits

What Makes the OIT Stick

Rollback with > 100,000 changes

Rollback of transactions that were active when the server crashed

Transaction stuck in limbo

OST

Oldest Snapshot Transaction

The lowest number that appears in the Oldest_Snapshot of any active transaction

How Oldest_Snapshot Field Is Set

Read only read committed – Oldest_Snapshot is not assigned

Read/write read committed – Oldest_Snapshot = Transaction #

Snapshot – Oldest_Snapshot = the oldest active read/write transaction

When the OST MovesWhen a new transaction starts

When a commit retaining occurs

When a sweep is run

Note: commit retaining on a snapshot – Commits existing transaction– Starts new transaction whose

Oldest_Snapshot is same as the original transaction

Garbage CollectionRemoves row versions whose transaction # is less

than the OIT

Occurs automatically when a row is accessed

Occurs for all rows when a sweep is run

Sweep IntervalDefault = OAT – OIT > 20,000

Automatic sweep will only happen if the OIT gets stuck

If the OIT is stuck due to a rollback a sweep will unstick it

You can change the sweep interval using IBConsole or gfix

Sweep Interval in IBConsole

Sweep Interval Using gfixgfix -h 10000 -user sysdba -password masterkey employee.gdb

Fixing Limbo Transactions

If the OIT is stuck because a transaction is in limbo you must fix it before sweeping

You can fix limbo transactions automatically with gfix

gfix -two_phase -user sysdba -password masterkey employee.gdb

Possible Problems

OIT gets stuck

OAT gets stuck

OIT Gets StuckIf OIT is stuck garbage collection stops

Number of row versions increases

Performance suffers– Retrieving a row with many versions takes

longer– The TIP gets larger– Database size increases

Stuck OIT Rare In InterBase 7.x

Rollback a transaction with over 100,000 changes

Rollback on restart after a server crash

Crash during two_phase commit leaves a transaction in limbo

OAT Gets StuckOAT gets stuck if a transaction is left active

If OAT is stuck OIT is also stuck

Not all active transactions stick the OAT in InterBase 7.1 SP1 and later

What Sticks the OAT?Read only read committed transactions can

remain active indefinitely without sticking the OAT

Read/write read committed transactions can remain active indefinitely if you call Commit Retaining after updating the database

Snapshot transactions always stick the OAT

Savepoints

Savepoint is a named point in a transaction that you can rollback to without rolling back the transaction

Creating a Savepoint

SAVEPOINT MY_SAVEPOINT

Creates a savepoint named MY_SAVEPOINT

Releasing a Savepoint

RELEASE SAVEPOINT MY_SAVEPOINT

Releases the named savepoint and frees Its resources

Rollback to a Savepoint

ROLLBACK TO SAVEPOINT MY_SAVEPOINT

Rolls back to MY_SAVEPOINT and continues the transaction

Transactions with isql

Easy way to test transaction behavior

Supports all transaction options

Can open multiple sessions to simulate multiple users

Use performance monitor to watch transactions

See chapter 10 of the Operations Guide for information on isql

isql Command Line Options

Starting isql

isql -u sysdba -p masterkey employee.gdb

Transactions in isqlIsql starts a transaction when it connects to a

database

Use COMMIT to end current transaction

Use SET TRANSACTION to start a transaction

SET TRANSACTION options

READ WRITE or READ ONLY Transaction access mode

WAIT or NOWAIT Lock resolution mode

RESERVING <tablename> FOR [SHARED or PROTECTED] [READ or WRITE]

Table reservations

SET TRANSACTION Isolation Level

ISOLATION LEVEL

SNAPSHOT [TABLE STABILITY] or

READ COMMITTED RECORD_VERSION or

READ COMMITTED NO RECORD_VERSION

Example

SET TRANSACTION READ ONLY NOWAIT ISOLATION LEVEL READ COMMITTED;

Monitoring Transactions

A Transaction’s Attachment

Transaction Summary Info

Questions?

Thank You

Please fill out the speaker evaluation

You can contact me further at …

[email protected]