Block database access for encryption

David Chase 681 Reputation points
2021-01-19T20:41:40.587+00:00

We are planning to add Always Encrypted to our SQL 2016 database. It is used by thousands of web users. We are blocking new access to db at a specific time but not sure who may be already in web app when we do this. Is there a way to best block access to the db when we perform the column encryptions?

SQL Server | Other
0 comments No comments
{count} votes

Accepted answer
  1. Erland Sommarskog 121.8K Reputation points MVP Volunteer Moderator
    2021-01-19T22:47:12.783+00:00

    You can do

    ALTER DATABASE db SET RESTRICTED_USER WITH ROLLBACK IMMEDIATE
    

    and that will only permit users in the db_owner role to connect to the database. (I hope that the web server does not use an application login in that role!)

    When you are done, you do:

    ALTER DATABASE db SET MULTI_USER
    

    However, I would rather expect that you put a page saying "Maintenance in progress" instead of brutally letting database connections fail.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Cris Zhan-MSFT 6,661 Reputation points
    2021-01-20T07:27:55.043+00:00

    Hi,

    Full agree with Erland. You can put the database into Restricted User state.

    When the Restrict Access property of the database is set to RESTRICTED_USER, this will allow only members of the db_owner fixed database role and dbcreator and sysadmin fixed server roles to connect to the database. RESTRICTED_USER doesn't limit their number. Disconnect all connections to the database using the timeframe specified by the ALTER DATABASE statement's termination clause. After the database has transitioned to the RESTRICTED_USER state, connection attempts by unqualified users are refused.
    https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-database-transact-sql-set-options?view=sql-server-ver15

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.