现在的位置: 首页 > 综合 > 正文

Database-isolation levels

2013年09月13日 ⁄ 综合 ⁄ 共 2803字 ⁄ 字号 评论关闭

1What are isolation levels?

Isolation levels provide a degree of control of the effects one transaction can have on another concurrent transaction.

Table 1

Isolation level

Description

TRANSACTION_READ_UNCOMMITTED

Can read uncommitted data (dirty read) by another transaction, and nonrepeatable reads and phantom records are possible. Least restrictive of all isolation levels. No shared locks are issued and no exclusive locks are honored.

TRANSACTION_READ_COMMITTED

Can't read uncommitted data by another transaction. Shared locks are held while the data is being read to avoid dirty reads, but the data can be changed before the end of the transaction resulting in non-repeatable reads and phantom records.

TRANSACTION_REPEATABLE_READ

Locks are placed on all data that is used in a query, preventing other users from updating the data, but new phantom records can be inserted into the data set by another user and are included in later reads in the current transaction.

TRANSACTION_SERIALIZABLE

Strongest level of isolation. Places a range lock on the data set, preventing other users from updating or inserting rows into the data set until the transaction is complete. Can produce deadlocks.

 

Isolation levels are not part of the EJB specification. They can only be set on the resource manager either explicitly on the Connection (for bean managed persistence) or via the application server specific configuration.

 

The EJB specification indicates that isolation level is part of the Resource Manager.As the transaction isolation level increases, likely performance degradation follows, as additional locks are required to protect data integrity. If the underlying data does not require such a high degree of integrity, the isolation level can be lowered to improve performance.

 

2How will you control two concurrent transactions accessing a database?

You can use isolation levels. An isolation level defines how concurrent transactions accessing a relational database are isolated from each other for read purpose. These isolation levels can prevent one or more of the phenomena that happen during concurrent transactions:

Dirty reads: A transaction reads uncommitted changes from another transaction.

Nonrepeatable reads: A transaction reads a row in a database table, a second transaction changes the same row and the first transaction re-reads the row and gets a different value.

Phantom reads: A transaction executes a query, returning a set of rows that satisfies a search condition and a second transaction inserts another row and the first re-executes the same query and get an additional record returned.

Table 2

Isolation Level

Dirty read

Nonrepeatable read

Phantom read

TRANSACTION_READ_UNCOMMITED

Possible

Possible

Possible

TRANSACTION_READ_COMMITED

Not possible

Possible

Possible

TRANSACTION_REPEATABLE_READ

Not possible

Not possible

Possible

TRANSACTION_SERIALIZABLE

Not possible

Not possible

Not possible

You should use a highest possible isolation level that gives acceptable performance. It is basically a tradeoff between data integrity and performance. For example the isolation level “TRANSACTION_SERIALIZABLE” attribute guarantees the highest level of data integrity but adversely affects performance because even simple reads must wait in line.

 

 

抱歉!评论已关闭.