SQLExpress, locked files, and sp_detach

I have a situation where I want to copy a SQLExpress mdf file, after having read some metadata from it, but I find that an exclusive lock persists on the file for about 5 minutes after closing... the lock is held by "sqlservr.exe" This lock prevents me from copying the file. It seems that I may be able to fix this by calling sp_detach_db after closing the connection (does this seem reasonable?) but I am unsure what the database name (dbname) is that I need to pass to sp_detach_db.

To make this a little more concrete, here is the connection string I use... "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\testdata\DVDCollectionDatabase.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True'" so in that case what would be the dbname?

Note that I need to do this through API (in a .NET application)

[838 byte] By [AndyPhilpotts] at [2007-12-25]
# 1
You should only make sure that the connection is closed by your application, otherwise you will have to rely on the garbage collector to remove all references to the connection and implicitly closes the connection.

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

JensK.Suessmeyer at 2007-9-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
# 2
This doesn't really answer my question, I do make sure the connection is closed, but the database driver (not me) keeps an exclusive lock on the file for a few minutes after closing. My guesss is that this is to improve performance when the same file is repeatedly opened and closed, but for me this causes a problem as after closing the file I want to copy it to another part of the file system. I cannot perform the copy because of the exclusive file lock.

As I said, I need (I think) to use sp_detach_db, but I am unsure what the name of the database is to use on that call, indeed, now I think of it, how do I invoke a stored procedure on a closed connection?

AndyPhilpotts at 2007-9-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
# 3
If you are using any standard connection strign you are using the default values for every setting you did not specified, including the Connection Pooling setting. Connection pooling is enabled by default. SO if you want the "lock" or the open connection to be released right after the Close method took place, you will have to specify the Pooling=false within your connectionstring.

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

JensK.Suessmeyer at 2007-9-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
# 4

I have had the same problem. Once i open a sql express 2005 file based DB using .net i cannot close it, even when setting connection pooling to false in the connection string. Am i doing something wrong here ?.

heres my con string:

@"Data Source=.\SQLEXPRESS;AttachDbFilename=" + _dir + ";Integrated Security=True;User Instance=True;Pooling=false;MultipleActiveResultSets=True;Connect Timeout=0;"

timtam at 2007-9-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
# 5

I believe that the default time-out for a connection pool is five (5) minutes. That is five minutes after the last client access.

Once a pool has been established, changine the settings, e.g., Pooling=false, will affect new connections, it doens't remove the existing pool objects. They will evenually 'time out'. I guess you could explicitly kill off the pool objects, calling the garbage collector, but that could be 'messy'.

ArnieRowland at 2007-9-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
# 6

thanks Arnie for the explanation,

I managed to work around the problem with an 'overkill' solution;

1) I first close the System.Data.SqlClient.SqlConnection,

2) then using the 'System.ServerviceProcess.ServiceController' libary i programmatically stop the "mssql$sqlexpress" service (ie the sql 2005 express service),

3) this unlocks the sql server express .mdf file after about 2 seconds

4) then i programmatically copy the mdf file and restart the sql 2005 express service using the System.ServerviceProcess.ServiceController again.

In hindsight maybe calling the garbage collector might have been better, however this solution works programmatically and is not too many lines of code, + it is a rarely used component.

Cheers

timtam at 2007-9-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...

SQL Server

Site Classified