Re: Transaction around a SELECT statement
From: Steve Kass (skass_at_drew.edu)
Date: 09/13/04
- Next message: David: "fn_get_sql issue"
- Previous message: Zach Wells: "Re: what's wrong w/ my queries?"
- In reply to: Craig Kenisston: "Re: Transaction around a SELECT statement"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 13 Sep 2004 16:25:46 -0400
Craig,
I think I addressed this. From my post:
"If you want to protect against phantom reads (from inserts) as well as
dirty reads (from updates), you can use SERIALIZABLE as the transaction
isolation level, but with a greater risk of deadlocks."
In case I wasn't clear, REPEATABLE READ blocks UPDATEs, and SERIALIZABLE
blocks both UPDATEs and INSERTs.
Steve
"Craig Kenisston" <craigkenisston@hotmail.com> wrote in message
news:uY%23kHCbmEHA.1672@TK2MSFTNGP14.phx.gbl...
>
> Steve :
>
> In your sample code, another connection is still being able to insert into
> table T.
> I think in your code you meant "Serializable", instead of "Repeatable
> Read".
>
>
> Regards,
>
>
>
>
>
> "Steve Kass" <skass@drew.edu> wrote in message
> news:uZj8g6ZmEHA.3428@TK2MSFTNGP14.phx.gbl...
>> Rachel,
>>
>> While I can't think of why you'd need to put a *single* SELECT
>> statement into a transaction, you might want to put a block including
>> more than one SELECT statement into a transaction, even if there are no
>> inserts or updates. Consider this example:
>>
>> SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
>> GO
>>
>> CREATE TABLE T (
>> i int
>> )
>> GO
>> INSERT INTO T VALUES (1)
>> GO
>>
>> SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
>> GO
>> BEGIN TRANSACTION
>> SELECT * FROM T
>> WAITFOR DELAY '00:00:08'
>> -- during this delay, an update is issued against table T
>> -- by another connection to change i to -1
>> SELECT * FROM T
>> COMMIT TRANSACTION
>> GO
>>
>> DROP TABLE T
>>
>> With the BEGIN TRANSACTION .. COMMIT TRANSACTION block in place, the
>> out-of-transaction update will be blocked until this transaction ends,
>> and the the two SELECT statements will agree. Without the transaction
>> block, the update will happen immediately when issued, and the two
>> SELECT statements will disagree.
>>
>> If you want to protect against phantom reads (from inserts) as well as
>> dirty reads (from updates), you can use SERIALIZABLE as the transaction
>> isolation level, but with a greater risk of deadlocks.
>>
>> While this example probably doesn't look like useful code, multiple
>> selects to accomplish one task are conceivable. You may need to select
>> from a table, make some calculations or verify some things, then select
>> again, based on what you find from first selection. In order for the
>> isolation level to apply between the separate queries, you must
>> enclosing them in a single transaction.
>>
>> Steve Kass
>> Drew University
>>
>> Rachel K wrote:
>>
>> >I am working with several developers who feel that it is
>> >necessary to use a transaction around a select statement
>> >in order to prevent 'dirty reads'. I believe this is
>> >handled with the isolation level, but that a transaction
>> >around a select is unnessecary. Anyone have any thoughts
>> >or proof?
>> >
>> >
>
>
- Next message: David: "fn_get_sql issue"
- Previous message: Zach Wells: "Re: what's wrong w/ my queries?"
- In reply to: Craig Kenisston: "Re: Transaction around a SELECT statement"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|