Killing ALL user connections to the database SQL 2005 with rollback - sql

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

Related

Log Shipping vs Replciation Vs Mirroring in SQL server 2012

I have a SQL Server 2012 database which currently used as a transactional database and reporting database. The application reads/writes into the same database and the reports are also generated against the same database.
Due to some performance issue, I have decided to maintain the two copies of the database. One will be a transactional database which will be accessed by the application. The other database will be the exact copy of the transactional database and it will only be used by the reporting service.
Following are the requirements:
The reporting database should be synched with transactional database in every one hour. That is, the reporting database can have stale data for maximum of 1 hour.
It must be read-only database.
The main intension is NOT recovery or availability.
I am not sure which strategy, transactional log shipping, mirroring or replication, will be best suited in my case. Also if I do the synch operation more frequently (say in every 10 minutes), will there be any impact on the transactional database or the reporting service?
Thanks
I strongly recommend you to use a standby database in readonly state. And every 15 minutes your sqlserveragent has a scheduled job to: a) generate a new .trn logfile within main db, and b) restore it into standby one(your reports db). The only issue is: using this technique your session will be disconnected while agent restores the .trn logfile. But if you can stop the restore job, run your reports and then reactivate it, there is no problem. Seems to be exactly what you need. Or if your reports are fast to run, probably will not be disconnected...if im not wrong restore job can also be configured to wait opened session to finish or to close it. I can check it this last doubt for you tomorrow if you don't find..
Once it is running in the same sql server instance, you don't have to worry about extra licensing...

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

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.

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

SQL Server: How to unlock a table / row?

We recently updated our WPF application to perform its data synchronisation (using sync framework) within a single transaction against our SQL Server 2008 database.
Almost straight away this has somehow led to a row being locked in of of the tables
preventing all other users from syncing.
The thing is that the lock does not seem to be lifting and we are not sure how to resolve the situation.
Any feedback appreciated
The only way to release a row lock is to commit or rollback the transaction in which the lock was taken.