.netsql-servernhibernatedatabase-restorehilo

SQL Server: How to avoid or detect database restore while application is running


It is required to shut down the system I'm working on before the database gets restored. If a customer does not do this, he may run into serious problems, because the in-memory state of the application server does not match the database. It may take some time until the corrupt database causes errors and the system stops working and the database is unusable.

So I try to detect this situation and avoid the problem.

Did anyone already solve such a problem?

I'm using SQL Server 2005 and above, .NET (C#), SMO.


Edit:

Because of some misconceptions and discussions about application design, I need to explain where the problem comes from.

The Hi-Lo generator: The application uses NHibernate, which has a cache of Hi-Values of the Hi-Lo id generator. The Hi-Values are read from the database and allows the application to generate a specific number of primary keys until it requires to go back to the database and get another range of numbers. The Hi-Values stored in the database are only incremented, but never decremented (unless when a older version of the database is restored). This is the concept of the Hi-Lo generator, it is neither invented nor implemented by myself.

Caches: In caches, primary keys are used to identify records in the database. Primary keys do never change in normal use of the database. In case of a restore, the primary keys do not identify the same record anymore and the caches are just plain wrong. It can't even detect this. It might result, for instance, in setting wrong foreign keys in new records.

A DB restore can't be compared to any other normal use of the database. A restore breaks all the rules of data handling.

I guess that many applications keep connections open while they are running, making restore impossible. Most other applications would probably just crash when the database is restored and it tries to access the database afterwards. In my case, it may keep running for awhile, which is a problem.

I guess that most people don't even try to restore the database when the application is running.

There is a theoretical, an in my case proven, risk that something is going completely wrong when a database is restored while the application is still in memory. I don't want to be told that this is not a problem at all, just because most applications do not handle it.


Solution

  • I didn't get a useful answer, so I assume that there is no general solution to this problem.

    I only see two ways to solve it: