Help: SQL Server

Sharing my knowlege about SQL Server Troubleshooting Skills

  • Blog Stats

    • 2,153,605 hits
  • Select GETDATE()

    December 2025
    M T W T F S S
    1234567
    891011121314
    15161718192021
    22232425262728
    293031  

Archive for the ‘Troubleshooting’ Category

Tips and Tricks : OS error: 32(The process cannot access the file because it is being used by another process.).

Posted by blakhani on August 7, 2014


While playing with tempDB database on my machine, I have made some mistake and then was not able to start one SQL Instance. As usual, started troubleshooting and used sysinternals tool to find the cause of the problem.

First, I looked into ERRORLOG and found below messages. I have highlighted some text for clarity.

2014-08-07 05:53:44.13 spid11s     Clearing tempdb database.
2014-08-07 05:53:44.40 spid11s     Error: 5123, Severity: 16, State: 1.
2014-08-07 05:53:44.40 spid11s     CREATE FILE encountered operating system error 32(The process cannot access the file because it is being used by another process.) while attempting to open or create the physical file ‘E:\TempDB\tempdb.mdf’.

2014-08-07 05:53:45.42 spid11s     Error: 17204, Severity: 16, State: 1.
2014-08-07 05:53:45.42 spid11s     FCB::Open failed: Could not open file E:\TempDB\tempdb.mdf for file number 1.  OS error: 32(The process cannot access the file because it is being used by another process.).
2014-08-07 05:53:45.43 spid11s     Error: 5120, Severity: 16, State: 101.
2014-08-07 05:53:45.43 spid11s     Unable to open the physical file "E:\TempDB\tempdb.mdf". Operating system error 32: "32(The process cannot access the file because it is being used by another process.)".
2014-08-07 05:53:45.46 spid11s     Error: 1802, Severity: 16, State: 4.
2014-08-07 05:53:45.46 spid11s     CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
2014-08-07 05:53:45.46 spid11s     Could not create tempdb. You may not have enough disk space available. Free additional disk space by deleting other files on the tempdb drive and then restart SQL Server. Check for additional errors in the event log that may indicate why the tempdb files could not be initialized.
2014-08-07 05:53:45.46 spid11s     SQL Trace was stopped due to server shutdown. Trace ID = ‘1’. This is an informational message only; no user action is required.
2014-08-07 05:53:49.68 Logon       Error: 17188, Severity: 16, State: 1.
2014-08-07 05:53:49.68 Logon       SQL Server cannot accept new connections, because it is shutting down. The connection has been closed. [CLIENT: <local machine>]

Due to OS Error 32, SQL was not able to use files which are needed by TempDB database and unable to start.

Next task for us would be to find out which is that “another process”. If it’s an open handle by a use mode process we would be able to find out using Process Explorer. Once you download and run it, we can see something like below.

image

Then press Ctrl+F or use Menu option “Find” > “Find Handle or DLL” as shown below

image

In the find window provide file name with complete path and search. I was able to get below

image

Now, since I know that “another process” I can take the action which is suitable. This happened to me because my two instances are pointing to same location for tempdb database files. I have rectified them on non-starting instance by below steps.

1. Started SQL Server using “Net Start MSSQL$SQL2014 /mSQLCMD /f /T3608”
2. Connected to SQL via SQLCMD –S(local)\SQL2014
3. Executed below T-SQL

USE master; 
GO 
ALTER DATABASE tempdb 
MODIFY FILE (NAME = tempdev, FILENAME = 'E:\Program Files\Microsoft SQL Server\MSSQL12.SQL2014\MSSQL\DATA\tempdb.mdf'); 
GO 
ALTER DATABASE tempdb 
MODIFY FILE (NAME = templog, FILENAME = 'E:\Program Files\Microsoft SQL Server\MSSQL12.SQL2014\MSSQL\DATA\tempdb.ldf'); 
GO 

 

4. Stopped SQL via “net stop MSSQL$SQL2014”

5. Started SQL normally.

image

(Click on Image to enlarge)

Hope this would help you in troubleshooting OS Error 32 for other application as well.

  • Cheers,
  • Balmukund Lakhani
  • Twitter @blakhani
  • Author: SQL Server 2012 AlwaysOnPaperback, Kindle
  • Posted in Troubleshooting | Tagged: , , , , , , , , | 2 Comments »

    Troubleshooting : Slow Delete Database from Management Studio

    Posted by blakhani on July 10, 2014


    Not very long ago, I had a database on my SQL 2012 Instance which was getting log shipped happily at frequency of 1 minute. Long long ago I have done such configuration for a demo purpose and forgot. Today I had “Memory Recall” when space was getting filled up with log backups. Since the demo was complete, I decided to drop the database. So I broke log shipping and tried dropping the database. What you do as a DBA to drop a database? Right Click > Delete .. huh?

    image

    When I clicked on “OK” button it was taking a long time. GUI to seem like its hanging. As usual, troubleshooting started! Ran my standard troubleshooting query to find out what is going on.

     SELECT s.session_id
        ,r.STATUS
        ,r.blocking_session_id 'Blk by'
        ,r.wait_type
        ,wait_resource
        ,r.wait_time / (1000.0) 'Wait Sec'
        ,r.cpu_time
        ,r.logical_reads
        ,r.reads
        ,r.writes
        ,r.total_elapsed_time / (1000.0) 'Elaps Sec'
        ,Substring(st.TEXT, (r.statement_start_offset / 2) + 1, (
                (
                    CASE r.statement_end_offset
                        WHEN - 1
                            THEN Datalength(st.TEXT)
                        ELSE r.statement_end_offset
                        END - r.statement_start_offset
                    ) / 2
                ) + 1) AS statement_text
        ,Coalesce(Quotename(Db_name(st.dbid)) + N'.' + Quotename(Object_schema_name(st.objectid, st.dbid)) + N'.' + Quotename(Object_name(st.objectid, st.dbid)), '') AS command_text
        ,r.command
        ,s.login_name
        ,s.host_name
        ,s.program_name
        ,s.host_process_id
        ,s.last_request_end_time
        ,s.login_time
        ,r.open_transaction_count
    FROM sys.dm_exec_sessions AS s
    INNER JOIN sys.dm_exec_requests AS r ON r.session_id = s.session_id
    CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) AS st
    WHERE r.session_id != @@SPID
    ORDER BY r.cpu_time DESC
        ,r.STATUS
        ,r.blocking_session_id
        ,s.session_id

    Here was the result (I have removed few columns to avoid clutter)

    session_id status cpu_time logical_reads writes Elaps Sec statement_text command_text
    60 runnable 247734 42732577 29196 545.223 DELETE msdb.dbo.backupmediaset

    FROM msdb.dbo.backupmediaset bms

    WHERE bms.media_set_id IN (SELECT media_set_id

         FROM @media_set_id)

        AND ((SELECT COUNT(*)

      FROM msdb.dbo.backupset

      WHERE media_set_

    [msdb].[dbo].[sp_delete_database_backuphistory]

     

    Why would delete database do that? Well, it’s done by a small little checkbox which we never noticed.

    image

    That little checkbox executed this command (along with drop database). If we use “Script” button, this is the outcome

    EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = N'AdventureWorks2014'
    GO
    USE [master]
    GO
    DROP DATABASE [AdventureWorks2014]
    GO
    
    

    Now we know why it’s taking time but can this be made faster? Well, I check msdb database and there are few indexes which have been added in SQL Server 2014 which would help in this situation. Here is the quick comparison.

    Select @@version
    go
    SELECT 
         TableName = t.name,
         IndexName = ind.name,
         ColumnName = col.name
    FROM 
         sys.indexes ind 
    INNER JOIN 
         sys.index_columns ic ON  ind.object_id = ic.object_id and ind.index_id = ic.index_id 
    INNER JOIN 
         sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id 
    INNER JOIN 
         sys.tables t ON ind.object_id = t.object_id 
    WHERE 
         ind.is_primary_key = 0 
         AND t.is_ms_shipped = 1
         AND t.name in ( 'backupfile', 'backupfilegroup', 'backupmediafamily', 'backupmediaset', 'backupset', 'restorefile', 'restorefilegroup', 
    'restorehistory' ) ORDER BY t.name, ind.name, ind.index_id, ic.index_column_id

    image

    image

    If you are facing the same problem which I described on SQL 2008 or SQL 2012, you may want to try creating new indexes as advised on other blogs (search for “msdb performance tuning” in bing/google) but my only suggestion is that it might be unsupported.

    If you clean msdb backup history regularly, you might not face the issue though. There is maintenance plan to do that. Try it out!

    Hope this helps.

  • Cheers,
  • Balmukund Lakhani
  • Twitter @blakhani
  • Author: SQL Server 2012 AlwaysOnPaperback, Kindle
  • Posted in Troubleshooting | Tagged: , , , , | 4 Comments »