How to lock Sql Server 2012 and Prevent Establishing Connection and perform Data migration - sql

In My University Management System i want to Perform Data Migration on Specific Day and so that i want close all open connections and Perform Data Migration as a Db admin user and after that Allow user to Establish connections to Database
after some Searching i find that script
To Disable MultiUser
alter database dbName set single_user with rollback immediate
To Enable MultiUser
alter database dbName set multi_user with rollback immediate
Question :
how to Perform my required Operation as a db admin?
it is a good way to do this or some better way ?
what does Disable Script doing?
what does Enable Script doing?
From this Disable Scriptall open connections will close ?

What you said is about the same what we do in this situation. The first statement puts the DB in "single_user" mode. That means, only YOUR current session is connected to the db after that statement. The "with rollback immidiate" option specifies that all open transaction and current queries and connections are closed and rolled back, so the database is in consistent state afterwards.
Personally, I dislike the "single_user" mode, because it allows any single user to connect if you accidentially close your connection. I'd use "restricted_user" instead. In that mode, only members of the db_owner fixed database role and dbcreator and sysadmin fixed server roles are allowed to connect to the database, but it does not limit their number. That will work better IF your users are not db_owner.
You second example just reverses that change and opens the DB to everyone ("multi_user") again.

Related

Making a connection in Single User Mode

So I have a third party vendor software that I have to call in an SQL Agent Job to restore my databases. So my first step is to set the database in single user mode, my second step is to call the software to start the restore. The problem is, the software takes so long to actually start the restore (its doing it all across the network) that another user can swoop in and take over the database.
Is there a way to put the DB in single user mode and then grab that connection right away? I have tried with a few "Select Getdate()" but I feel that is taking it from the system, and not the database. Do I have to select from a specific table in the database to get it to work?
Thanks
Rather than place the database in single user mode, take the database offline in the context of another database:
USE tempdb;
ALTER DATABASE DatabaseToRestore
SET OFFLINE WITH ROLLBACK IMMEDIATE;
Use the "Client Application Name" option along with the "-m" option that places SQL into single user mode.
That way only an application that matches the name given can connect and take the connection. Obviously you need to ensure that your client process that's doing the restore has a distinct name!
See here for info on the various SQL Server startup options: https://msdn.microsoft.com/en-us/library/ms190737.aspx
Also, I was under the impression you had to be an admin to connect in single user mode, which of course your normal users should not be. Please someone correct me if I'm wrong on that point!

How to close all active database connections in one shot?

While trying to restore SQL Server database or do any other actions which require exclusive database access it displays following error:
Exclusive access could not be obtained because the database is in use.
This will do what you are asking for, but will terminate all open connections and will rollback uncommitted changes
ALTER DATABASE <yourDB>
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
try this..
USE master
GO
ALTER DATABASE AdventureWorksDW
SET SINGLE_USER
--This rolls back all uncommitted transactions in the db.
WITH ROLLBACK IMMEDIATE
GO
RESTORE DATABASE AdventureWorksDW
FROM ...
...
GO
One thing to do would be to put the database in single-user mode.
From MSDN
In Object Explorer, connect to an instance of the SQL Server Database
Engine, and then expand that instance. Right-click the database to
change, and then click Properties. In the Database Properties dialog
box, click the Options page. From the Restrict Access option, select
Single. If other users are connected to the database, an Open
Connections message will appear. To change the property and close all
other connections, click Yes
.
You can kill all processes that have open connections to a SQL Server database or alter database to single user mode with no_wait option
ALTER DATABASE [Works] SET SINGLE_USER WITH NO_WAIT

Killing ALL user connections to the database SQL 2005 with rollback

The issue is that there are certain users that keep certain applications open instead of shutting down their PCs overnight - this keeps locks on tables and that has an impact on other systems that rely on the tables being in an 'unlocked' state to do their updates etc.
I want to kill all USER connections (only) to a database. I will need to run this as a job on a nightly basis. I don't want to use:
ALTER DATABASE xxx SET SINGLE_USER WITH ROLLBACK IMMEDIATE
As I do not want to destroy the integrity of the database transaction logging.
Any ideas as to how this may be accomplished?
Thanks in advance,
JR

SQL Server - Keep synchronized a read-only database

I need to keep synchronized a couple of databases, one is on sql 2000 and the other on 2005. The one on 2000 should be kept in read-only mode to make sure the users does not enter data. The 2005 is the one which are going to be updated by the users.
So I could develop a script to truncate and insert into the 2000 version with data from 2005 every night. My question is if there is some way to disable the read-only mode temporaly while the script is running. Is there a better approach?
Thanks,
You can disable the read-only mode while the script is running. You may also want to set RESTRICTED_USER to keep any users from accessing the database while processing.
I would tend along the response offered by Mike Henderson.
Essentially leverage db security to prevent users from modifying data and allow the account used to synchronize data to write.
Not sure what your time constraints are, but if you are relying on the db to be read-only to prevent end-users making changes, there is the possibility that they could get in during the time period that the db is in read/write mode.
Just for future reference, this is how I proceeded,
ALTER DATABASE MyDB SET READ_WRITE WITH ROLLBACK IMMEDIATE
ALTER DATABASE MyDB SET READ_ONLY WITH ROLLBACK IMMEDIATE
Thank you,

Does merge replication lock subscriber database?

I need to configure merge replication between 2 databases. These databases have foreign key integrity, which makes the replication not work, so I resorted to:
Dropping all FKs on subscriber database,
Replicating, and
Recreating the FKs.
This however leaves the subscriber database vulnerable to FK violations.
So my questions are:
Does the replication lock the subscriber database, raise a transaction, and render the database unusable in any way during the process?
If not, could I initiate such a lock manually through TSQL?
If none of the above is possible, is there something I'm missing?
don't know about lock initiated by replication, but for the time of your maintenance you can set the whole database to single_user or restricted_user.
ALTER DATABASE SET RESTRICTED_USER
i recommend the second one as it allows access to the database to all users who are, quote:
members of the db_owner fixed database role and dbcreator and
sysadmin fixed server roles
(see here: http://msdn.microsoft.com/en-us/library/aa933082%28SQL.80%29.aspx)
, only regular users are restricted. it will wait until all regular user connections are done
ALTER DATABASE SET RESTRICTED_USER WITH ROLLBACK IMMEDIATE
will kill all such connections immediately. This
select DATABASEPROPERTYEX ('ocon_reportdb','UserAccess') DATABASEPROPERTYEX_UserAccess
reads the current status
UPDATE:
there are maintainance activities such as statistics performed by the database engine. using WITH ROLLBACK IMMEDIATE will also kill those connections, so be careful
UPDATE2: specs to have acces in restricted_user-mode