views:

55

answers:

1

I have really odd user requirement. I have tried to explain to them there are much better ways of supporting their business process and they don't want to hear it. I am tempted to walk away but I first want to see if maybe there is another.

Is there anyway that I can lock a whole database as opposed to rowlock or tablock. I know I can perhaps but the database into single user mode but that means only one person can use it at a time. I would like many people to be able to read at a time but only one person to be able to write to it at a time.

They are trying to do some really odd data migration.

+6  A: 

What do you want to achieve?

  • Do you want to make the whole database read-only? You can definitely do that
  • Do you want to prevent any new clients from connecting to the database? You can definitely do that too

But there's really no concept of a "database lock" in terms of only ever allowing one person to use the database. At least not in SQL Server, not that I'm aware of. What good would that make you, anyway?

If you want to do data migration out of this database, then setting the database into read-only mode (or creating a snapshot copy of it) will probably be sufficient and the easiest way to go.

UPDATE: for the scenario you mention (grab the data for people with laptops, and then re-syncronize), you should definitely check out ADO.NET Sync Services - that's exactly what it's made for!

Even if you can't use ADO.NET Sync Services, you should still be able to selectively and intelligently update your central database with the changes from laptops without locking the entire database. SQL Server has several methods to update rows even while the database is in use - there's really no need to completely lock the whole database just to update a few rows!

For instance: you should have a TIMESTAMP (or ROWVERSION) column on each of your data tables, which would easily allow you to see if any changes have occured at all. If the TIMESTAMP field (which is really just a counter - it has nothing to do with date or time) has not changed, the row has not changed and thus doesn't need to be considered for an update.

marc_s
They want a central database. When someone leaves with a laptop they want to pull a copy of the latest data to a local database. When they come back they want to be able to push the changes to the central database. I just want to prevent race conditions with two people updating the same data and the change is last. Or one person updating while another is reading. They don't seem to crasp that this quite a dangerous situation.
uriDium
Then check out ADO.NET Sync Framework - that's designed for EXACTLY this scenario. You can ahve "occasionally connected" client with a local database, and it handles all the messy special cases and allows you to define strategies how to deal with conflicts.
marc_s
Will do thank you.
uriDium
Just saw your updates. I have created a similar product before using a similar technique to the one you described. Using a publisher and subscriber model. Only problem is that this took a couple of months and the client wants something working in the next week. The usual story in IT I guess.
uriDium