SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL transaction and lock of the table for SELECT


SQL transaction and lock of the table for SELECT

Author
Message
kiro1234
kiro1234
SSC Rookie
SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)

Group: General Forum Members
Points: 26 Visits: 40
Hi to all

I am very new in SQL Server world. I was working with Oracle more of 10 years.
Now I have to do a small DBA also on one SQL Server 2005 EE database.

I have a following problem:

when I am using in T-SQL statement with transaction or when I changed default AUTO-COMMIT mode

===== start of code ====
BEGIN Tran
update table xx set rowname = 'new name' where id_name = xx
==== end of code =====

and I am not doing
ROLLBACK or COMMIT

Open a new separate session and I Try

select * from xx ;

the session is waiting until other session do ROLLBACK or COMMIT - the entire table is locked for SELECT? In ORACLE there is no problem to read from table in other session when table was being updated in another session.

Am I wrong or SQL Server 2005 is working in this way?

When I am working with default AUTO-COMMIT mode of course there is no rpoblem.

The table I am playing with is about 6-7 rows.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87883 Visits: 45274
SQL by default uses locks for transaction isolation, while Oracle uses a form of row-versions.

In SQL, if you issue an update within a transaction, the locks taken by that update will be held until the transaction commits or rolls back. It's an exclusive lock (for data modification) and is not compatible with shared locks needed for reads. Hence the select has to wait until the transaction is committed or rolls back before it can read a locked row.

SQL 2005 and higher do support the isolation method that Oracle uses, in the Snapshot and Read_Committed_Snapshot isolation levels. They do have a large impact on TempDB, but they will allow select queries to read the old version of rows locked for modification.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


kiro1234
kiro1234
SSC Rookie
SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)

Group: General Forum Members
Points: 26 Visits: 40
Thanks for info :-)
How to enable in SQL 2005 this "Oracle like" isolation mode ? Ii it per instance or per database?
Thanks
GilaMonster
GilaMonster
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87883 Visits: 45274
I would suggest that you first get used to SQL's native locking mode, understand how it works, when there are problems with it and why. Snapshot isolation is not the default for a reason.

SQL Server is not Oracle and should not be treated like Oracle. They behave very differently.

Check out Books Online (the SQL help file), and read up on locking and isolation levels.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


kiro1234
kiro1234
SSC Rookie
SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)

Group: General Forum Members
Points: 26 Visits: 40
Yes you are right.
I will not change defaults. I am just taking of clear picture Smile
Thanks again.
sarath.snape
sarath.snape
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 0
Hi, I have a similar problem. I introduced 'transaction' so as to enable atomicity in my application , but it is locking out the entire table, even for reads. The tables locked are frequently updated. And since these are locked, it is a problem in multiple user scenarios too. However we are not encountering these issues in oralce db.

Please advice a solution.




Regards,
http://blog.autumn.co.in
alcide.burato
alcide.burato
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 4
Hi all
I had the same problem, the solution I found is

1) set the transaction isolation level to be READ UNCOMMITTED
http://msdn.microsoft.com/it-it/library/ms173763.aspx

2) use WITH (NOLOCK) when querying:

select * from MyTable WITH (NOLOCK)

This works for me.
Alcide
princa
princa
SSC Journeyman
SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)

Group: General Forum Members
Points: 87 Visits: 198
I have the same problem.
While I am not updating a single row but instead I need to truncate the whole table and re-insert from a view, so in this case, if I use with (nolock), which doesn't work, because the entire table was truncated so everyrow is locked.

Ideas?
sriharsha.kolluru
sriharsha.kolluru
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 3
This might Help.
Set Isolation level to read uncommitted. Hope this solves your problems.

http://msdn.microsoft.com/en-us/library/ms173763.aspx:-)
Dave Ballantyne
Dave Ballantyne
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2924 Visits: 8370
Using uncommitted/nolock may well 'fix' the problem but please understand that you will be reading dirty data.
If the transaction is to be rolled back then what does that be for the process that has read the dirty data ?



Clear Sky SQL
My Blog
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search