[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.sqlserver.programming

Strange locking behaviour with ADO

Gaspar

3/7/2007 2:49:00 PM

I need to x-lock a specific record, so as to prevent other transactions
from reading or writing. This is what I'm doing:

BEGIN TRANS
SELECT Value FROM MyTable WITH (ROWLOCK, XLOCK) WHERE Id = 1000
...
...
...
...
...
...
COMMIT


I try this in SQL Server Management studio with two different connections:

CONNECTION_1:
(The above statement)

CONNECTION_2:
SELECT Value FROM MyTable WHERE Id = 1000

Of course, CONNECTION_2 is blocked until CONNECTION_1 ends.


The problem comes then executing the statement in my application through
ADO: although the X-LOCK seems to be granted (I check the Activity
Monitor in SSMS), CONNECTION_2 can execute the SELECT statement!!! (i.e.
it does not wait until my application commits the transaction).
This is the code:

MyADOConnection.BeginTrans;
MyADOCmd.CommandText := 'SELECT Value FROM MyTable WITH (ROWLOCK, XLOCK)
WHERE Id = 1000';
MyADOCmd.Execute;
....
....
.... (do some operations)
...
... (at this point, for example, Connection_2
... can perform the SELECT! why???)
...
MyADOConnection.CommitTrans;



Thanks a lot for help,
Gaspar

4 Answers

Uri Dimant

3/7/2007 2:57:00 PM

0

Gaspar
Why do you want to lock readers?
Take a look at this article

http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/06/3...


"Gaspar" <gaspar@no-reply.com> wrote in message
news:ernaMfMYHHA.3256@TK2MSFTNGP04.phx.gbl...
>I need to x-lock a specific record, so as to prevent other transactions
>from reading or writing. This is what I'm doing:
>
> BEGIN TRANS
> SELECT Value FROM MyTable WITH (ROWLOCK, XLOCK) WHERE Id = 1000
> ..
> ..
> ..
> ..
> ..
> ..
> COMMIT
>
>
> I try this in SQL Server Management studio with two different connections:
>
> CONNECTION_1:
> (The above statement)
>
> CONNECTION_2:
> SELECT Value FROM MyTable WHERE Id = 1000
>
> Of course, CONNECTION_2 is blocked until CONNECTION_1 ends.
>
>
> The problem comes then executing the statement in my application through
> ADO: although the X-LOCK seems to be granted (I check the Activity Monitor
> in SSMS), CONNECTION_2 can execute the SELECT statement!!! (i.e. it does
> not wait until my application commits the transaction).
> This is the code:
>
> MyADOConnection.BeginTrans;
> MyADOCmd.CommandText := 'SELECT Value FROM MyTable WITH (ROWLOCK, XLOCK)
> WHERE Id = 1000';
> MyADOCmd.Execute;
> ...
> ...
> ... (do some operations)
> ..
> .. (at this point, for example, Connection_2
> .. can perform the SELECT! why???)
> ..
> MyADOConnection.CommitTrans;
>
>
>
> Thanks a lot for help,
> Gaspar
>


Gaspar

3/7/2007 3:13:00 PM

0

Thanks a lot.

Uri Dimant wrote:
> Gaspar
> Why do you want to lock readers?
> Take a look at this article
>
> http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/06/3...
>
>
> "Gaspar" <gaspar@no-reply.com> wrote in message
> news:ernaMfMYHHA.3256@TK2MSFTNGP04.phx.gbl...
>> I need to x-lock a specific record, so as to prevent other transactions
>>from reading or writing. This is what I'm doing:
>> BEGIN TRANS
>> SELECT Value FROM MyTable WITH (ROWLOCK, XLOCK) WHERE Id = 1000
>> ..
>> ..
>> ..
>> ..
>> ..
>> ..
>> COMMIT
>>
>>
>> I try this in SQL Server Management studio with two different connections:
>>
>> CONNECTION_1:
>> (The above statement)
>>
>> CONNECTION_2:
>> SELECT Value FROM MyTable WHERE Id = 1000
>>
>> Of course, CONNECTION_2 is blocked until CONNECTION_1 ends.
>>
>>
>> The problem comes then executing the statement in my application through
>> ADO: although the X-LOCK seems to be granted (I check the Activity Monitor
>> in SSMS), CONNECTION_2 can execute the SELECT statement!!! (i.e. it does
>> not wait until my application commits the transaction).
>> This is the code:
>>
>> MyADOConnection.BeginTrans;
>> MyADOCmd.CommandText := 'SELECT Value FROM MyTable WITH (ROWLOCK, XLOCK)
>> WHERE Id = 1000';
>> MyADOCmd.Execute;
>> ...
>> ...
>> ... (do some operations)
>> ..
>> .. (at this point, for example, Connection_2
>> .. can perform the SELECT! why???)
>> ..
>> MyADOConnection.CommitTrans;
>>
>>
>>
>> Thanks a lot for help,
>> Gaspar
>>
>
>

Bryan Kelly

2/18/2011 9:56:00 PM

0

LOL

Mr. Ritchie, you need to play nice or we're going to send you
home. ;-)

BTW The Footies were a bust. I hope Jack's game isn't.

Bryan (CARGPB 14)
http://usergallery.myhomegameroom.com/gall...


On Feb 18, 3:43 pm, Steve <king...@aol.com> wrote:
>
> The price will be, like $400/set, right Bryan?  They NEED to be pricey
> to go with the game.  :<))- Hide quoted text -
>
> - Show quoted text -

Rick Swanson

2/18/2011 10:05:00 PM

0

On Feb 5, 11:10 am, Bryan Kelly <bskel...@aol.com> wrote:

>
> I'm thinking, they are what they are and I won't be playing with any
> other ideas.
>

The slippers that were used in the movie simply had red sequins
affixed to them. I know this because I once stood in line at the
Great Movie Ride at Walt Disney World and the slippers were on
display. I also learned about the carousel horse that Mary Poppins
rode in the movie of the same name... if you need any information
about that.


Rick Swanson
aka Mr. Helpful