Hi
I am trying to use sql server 2005 management stodio to restore a database. But I got this error message:
Exclusive access could not be obtained because the database is in use
Anyone know how to solve this problem?
Thanks
Li
Technology Tips and News
Hi
I am trying to use sql server 2005 management stodio to restore a database. But I got this error message:
Exclusive access could not be obtained because the database is in use
Anyone know how to solve this problem?
Thanks
Li
when u restoer the database system should have Exculusive lock. in your case some process is using this database. you should kill those process which are using database. otherwise use this command
use Master
ALTER DATABASE yourdatabasename SET MULTI_USER WITH ROLLBACK IMMEDIATE;
Drop database yourdatabasename
BTW, though Management studio is good and it support all the feature, you must try all these command (Resotre/Backup/Kill /Single_user Mode) etc from the Query Analyser. It gives u a better understanding and it will clear your basics
Madhu
I'm having the same issue. I do not see activity manager under the management tree. Am I looking in the right place. A screen shot would be helpful if possible.
Thanks
Victor
Use any script view (such as query analyzer) to execute the following script. This will put your db in single user mode and execute the restore:
Code Snippet
Use Master
Alter DatabaseYOURDB
SET SINGLE_USER With ROLLBACK IMMEDIATE
RESTORE DATABASE YOURDB
FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\YOURDB.bak'
Aaron Smith
DiaMed - Diabetes Management Software
http://www.chirondata.com
Error 3101
Severity Level 16
Message Text
Exclusive access could not be obtained because the database is in use.
Explanation
This error occurs when you attempt to load a backup while users are accessing the database. This error can occur with RESTORE DATABASE or RESTORE LOG. You cannot use the RESTORE DATABASE statement while the database is in use by any user, including yourself.
Action
Use the ALTER DATABASE SET SINGLE_USER to remove users from the database.
Or, wait until all users have finished using the database, and then use the RESTORE DATABASE statement. Make sure that you are not using the database being loaded when you issue the RESTORE DATABASE statement. Although not required, it is best to run the RESTORE DATABASE statement from the master database.
MAKE USE THAT YOU ARE RUNNING THE QUERY FROM THE MASTER DATABASE!
- If you invoke the query under the database you are trying to restore, you will continue to receive the Error 3101.
hello,
Where is this option Activity Monitor?
Thanks,
Dom
Aaronator wrote: | |
|
As I am putting my DB in Single User Mode how do I return to the normal mode?
Thanks
Felyjos wrote: | |
|
When you restore a database, it will be restored in the mode in which it was when the backup was made, probably MULTI USER. In this case a restore will suffice.
To put a database to MULTI USER yourself:
Code Snippet
USE master;
GO
ALTER DATABASE << db name>>
SET MULTI_USER;
GO
Regards,
Jeroen
Jason Love wrote: | |
|
From BOL
the termination option WITH ROLLBACK IMMEDIATE
in the ALTER DATABASE
statement. All incomplete transactions will be rolled back and any other connections to the db
will be immediately disconnected. If any users are connected to the db and some command need exclusive lock of db then that command can not be run . So to terminate the connections we uses this command
Madhu
Worked perfectly!!
By the principle that no one is suppose to be attached while a restore is being done, just detach and reattach the database. This will kick all sessions off and allow the restore to take place.
No its not very clever way of resolving these issues, but its effective with out being drastic.
Sometimes rogue sessions stay connected for whatever reason and are a nuisance to stop. So the above method just eliminates this time consuming process.
The same effect could possibly be had be stopping the agent.
The full methods to attach and detach is
Right click on the database in question.
Go to tasks and detach.
To reattach
Right click on Databases
Go to Attach
Browse to the Mdf and Ldf files on the relevant drives.
Now try your restore.
We are having the same problem here.
I have a database, created by my app,over which I wish to restore a backup taken from another database that is controlled by the app (the underlying structures are exactly the same).
I have set the database to single user and can see this reflected in SSMS
I have run sp_who and can see absolutely no other connections to the database.
I am receiving the "Exclusive access..." message
I am going to take the scripted approach posted above, but have concerns about the Full Text elements of the restore. When performing a manual restore, I redirect these elements to another folder structure created specifically (we don't need them for the purposes of this restore).
The originally intend restoring to "sysft.EMGR_Item1_FullText", etc.
Pardon what may be complete ignorance, but if I attempt the restoreusing my backup (taken 2 days ago) using the scripted approach, will the FullText elements (which I can't therefore redirect) cause any problems for the existing live database (from which the backup was taken)
many thanks for any and all replies.
Steven
Li556035 wrote: | |
|
If you don not have any process running on your pc that uses your database like Enterprise Manager or any other third party application, use
SqlConnection.ClearAllPools();
in C# code before restoring. Like the following code:
Code Snippet public void BackupAndRestore(string Query) { SqlConnection.ClearAllPools();
SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["ConnectToMasterDB"]); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = new SqlCommand(); da.SelectCommand.Connection = connection; da.SelectCommand.CommandText = Query; da.SelectCommand.ExecuteNonQuery();
Ensure you are in the [master] database, not the database you are trying to RESTORE, otherwise you become the connection that is preventing exclusive access to the DB.
Regards,
-Darius
This worked perfectly for me and was an easy fix.detach and reattach the database. This will kick all sessions off and allow the restore to take place.
No its not very clever way of resolving these issues, but its effective with out being drastic.
Thank you Darius
it worked
Regards,
Nabi
Thank you Darius !
It worked very good !
Thanks for all the replies - they were helpful.
If anyone wants to know - I had to stop server - try backup - unable to connect to server - Start server - backup and it worked.
Then I went to log in but it reported failure to load database and after about 3 goes or 2 mins it was all restored.
I had the same problem. If you use management studio, choose the option in the restore window which says "Restrict access to the restored database" and it would resolve the issue.
After the restore is complete reset the database back to multi_user mode.
bru,
select database , bring it offline and bring it online and try to restore database
that worked for me
Tiki
Following How to Get Exclusive Access to SQL Server Database article will be of help to you.
Hope This Helps!
Thanks
Ashish Kumar Mehta
The way we fixed it was a bit more simpler then all these:
(using SQL Server 2005 as an example)
1. Go to the Activity Monitor within Management dropdown
2. On the left pane under "Select a page" click on "Locks by Object".
3. Scroll over to the right & sort by "Database".
4. Find the database that is appears to be locked (and trying to restore) and take note of the "Process ID" on the left.
5. Click back to "Process Info" under "Select a page" and find the Process ID # from step #4.
6. KILL IT WITH SWIFT PREJUDICE!
7. Activate restore.
8. Snicker as your restore is happening & the offending person is trying to reconnect.
This error is occurring since some one is trying to access the database while the restore is trying to do a restore. Go to the Management -> activity monitor and see if some one is accessing the DB if so kill the process and then run the restore.HEy, a mi me ha venido de lujo esta solucion.
with )s
Santhosh
http://sqlspy.blogspot.com
i had this problem too, but i resolved it.
u should:
because of u are using ur db so u can not restore it by itself, therefor u should login with another db like master then u can restore ur db
public void RestoreBackup(string DBName, string Path, string BackupFileName)
{
SqlConnection con = new SqlConnection("data source=Bahman-pc;Initial Catalog=Master;User ID=sa;Password=123;Integrated Security=True; Trusted_Connection=true;");
SqlCommand com = new SqlCommand();
string UseMaster = "USE master";
string restore = "RESTORE DATABASE Battalion FROM DISK ='" + Path+BackupFileName + "' WITH FILE = 1 , REPLACE";
string Alter1 = @"ALTER DATABASE [" + DBName + "] SET Single_User WITH Rollback Immediate";
string Alter2 = @"ALTER DATABASE [" + DBName + "] SET Multi_User";
try
{
con.Open();
com.Connection = con;
com.CommandText = UseMaster;
com.ExecuteNonQuery();
com.CommandText = Alter1;
com.ExecuteNonQuery();
com.CommandText = restore;
com.ExecuteNonQuery();
com.CommandText = Alter2;
com.ExecuteNonQuery();
}
catch (Exception ex)
{
com.CommandText = Alter2;
com.ExecuteNonQuery();
}
finally
{
com.Clone();
}
}
Hi,
I am having the same issue on a SQL 2008 database. The website is not live yet, so other than me, nobody would be trying to connect to it. The database is on a sql only web server. The website is on another web server. I have several identical sites with their respective databases all running on these machines with no issue.
When I try to run:
Use Master Alter Database myDatabaseName SET SINGLE_USER With ROLLBACK IMMEDIATE
(of course putting my actual database name there :)
I get this error:
Msg 5061, Level 16, State 1, Line 5
ALTER DATABASE failed because a lock could not be placed on database 'myDatabaseName'. Try again later.
Msg 5069, Level 16, State 1, Line 5
ALTER DATABASE statement failed.
Any ideas what's up?
thanks
David
use Master
ALTER DATABASE yourdatabasename SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
restore database databasename
from ...
Thanks Madhu
Open SQL Management Studio and press F8 to open Object Explorer.
There you can find Management Click on Management to view Activity Monitor.
Very Tanks for:
SqlConnection.ClearAllPools();
I wrote VB.net code to unlock the database. I was tired of always doing this manually, so I wrote a command-line tool in VB.Net to kill database locks . Replace 'test' with your database name and replace my connection string with your own. Connection strings are a different topic all together.
Module Module1