views:

28

answers:

2

Hi, we have a ASP.NET site with a SQL2008 DB.

I'm trying to implement transactions in our current system.

We have a method that is inserting a new row to a table (Table_A), and it works fine with transaction. If an exception is throwed, it will do the rollback.

My problem is, that another user can't do anything that invole with Table_A in the same time - probably because it's locked.

I can understand why the transaction have to lock the new row, but I don't want it to lock the whole table - forcing user_B to wait for user_A to finish.

I have tryed to set isolationlevel ReadUncommitted and Serializable on the transaction, but it didn't work.

So i'm wondering if I also need to rewrite all my select methods, so they are able to select all the rows except the new row there is in the making?

Example:

  • Table T has 10 rows(records?)
  • User A is inserting a new row into table T. This take some time.
  • At the same time, User B want to select the 10 rows, and dosn't care about the new row.

So I guess I somehow need to rewrite user B's select query from a "select all query" to a "select all rows that isnt bind to a transaction query". :)

Any tips is much appriciated, thanks.

A: 

Probably what you need is to set your isolation level to Read Committed Snapshot.

Although it needs more space for the tempdb it is great when you don't want your selects to lock the tables.

Beware of posible problems when using this isolation level.

Limo Wan Kenobi
+1  A: 

You'll need to make changes to your selects, not to the transaction that is doing the insert.

You can

  • set isolation level read uncommitted for the connection doing the select, or
  • specify the WITH (NOLOCK) hint against the table that is being locked, or
  • specify the WITH (READPAST) hint against the table being locked

As I say, all 3 of these options are things that you'd apply to the SELECT, not the INSERT.

A final option may be to enable SNAPSHOT isolation, and change the database default to use that instead (There are probably many warnings I should include here, if the application hasn't been built/tested with snapshot isolation turned on)

Damien_The_Unbeliever
It look like all I needed was to add "WITH(NOLOCK)" to my select query. I'll test some more and accept later.
radbyx
Adding "WITH(NOLOCK)" didn't do the trick completely. Now user B select all (11) rows, and not the decired 10 rows.
radbyx
So use READPAST (third option above) instead - it skips the locked rows rather than reading them
Damien_The_Unbeliever
"WITH(READPAST)" works fine it seems, ty. I was testing nolock wrong with difference combos so i never came to readpast. So do you also by any chance know if I can in some way set that hint using LINQ?
radbyx