Alex Rivera | Logout

How to explicitly lock a table in Microsoft SQL Server (looking for a hack - uncooperative client)

Asked 2011-07-12T15:53:41.240
9

This was my original question:

I am trying to figure out how to enforce EXCLUSIVE table locks in SQL Server. I need to work around uncooperative readers (beyond my control, closed source stuff) which explicitly set their ISOLATION LEVEL to READ UNCOMMITTED. The effect is that no matter how many locks and what kind of isolation I specify while doing an insert/update, a client just needs to set the right isolation and is back to reading my garbage-in-progress.

The answer turned out to be quite simple -

while there is no way to trigger an explicit lock, any DDL change triggers the lock I was looking for.

While this situation is not ideal (the client blocks instead of witnessing repeatable reads), it is much better than letting the client override the isolation and reading dirty data. Here is the full example code with the dummy-trigger lock mechanism

WINNING!

#!/usr/bin/env perl

use Test::More;

use warnings;
use strict;

use DBI;

my ($dsn, $user, $pass) = @ENV{ map { "DBICTEST_MSSQL_ODBC_$_" } qw/DSN USER PASS/ };

my @coninf = ($dsn, $user, $pass, {
  AutoCommit => 1,
  LongReadLen => 1048576,
  PrintError => 0,
  RaiseError => 1,
});

if (! fork) {
  my $reader = DBI->connect(@coninf);
  $reader->do('SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED');

  warn "READER $$: waiting for table creation";
  sleep 1;

  for (1..5) {
    is_deeply (
      $reader->selectall_arrayref ('SELECT COUNT(*) FROM artist'),
      [ [ 0 ] ],
      "READER $$: does not see anything in db, sleeping for a sec " . time,
    );
    sleep 1;
  }

  exit;
}

my $writer = DBI->connect(@coninf);

eval { $writer->do('DROP TABLE artist') };
$writer->do('CREATE TABLE artist ( name VARCHAR(20) NOT NULL PRIMARY KEY )');
$writer->do(do('DISABLE TRIGGER _lock_artist ON artist');

sleep 1;

is_deeply (
  $writer->selectall_arrayref ('SELECT COUNT(*) FROM artist'),
  [ [ 0 ] ],
  'No rows to start with',
);

$
Edit
Report

2 Answers

7

Add a locking hint to your SELECT:

SELECT COUNT(*) FROM artist WITH (TABLOCKX)

and put your INSERT into a transaction.

If your initial statement is in an explicit transaction, the SELECT will wait for a lock before it processes.

answered 2011-07-12T16:00:42.740
7

One hack hack hack way to do this is to force an operation on the table which takes a SCH-M lock on the table, which will prevent reads against the table even in READ UNCOMMITTED isolation level. Eg, doing an operation like ALTER TABLE REBUILD (perhaps on a specific empty partition to reduce performance impact) as part of your operation will prevent all concurrent access to the table until you commit.

answered 2011-07-12T18:36:35.827

Your Answer