Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Administration - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 6525

Slow Database Offline

$
0
0
Hello,I've battling with a bit of an odd problem and I'm hoping someone can help. I have a cloning process running via PowerShell that periodically gets stuck for a large number of minutes taking a database offline prior to the cloning of the files underneath and I cannot find out why it's doing it.The basic process for the offline steps is as follows[li]Create SQLPS Objects[/li][li]Kill all processes for that DB[/li][li]Get a list of any connections that exist on the database at that point[/li][li]Set restricted user[/li][li]Get a list of any connections that exist on the database at that point[/li][li]Set database offline[/li]What's particularly odd is that the checks show that there are not any sessions on the database to explain it getting stuck and the behaviour is identical whether you use the SMO SetOffline() function or the TSQL SET OFFLINE WITH ROLLBACK IMMEDIATE. It also doesn't happen every timePowerShell code snippet for the offline function (With lots of timespans to report where the delays are coming from)[code="other"]function Set-DatabaseOffline{ param ( $instance, $database ) try { $stepStart = Get-Date $machineName = (Get-HostFromAlias $instance) $stepDuration = (New-TimeSpan -Start $stepStart -End (Get-Date)) Write-Host "Instance alias gathered in $($stepDuration)" -ForegroundColor DarkMagenta -BackgroundColor White Write-Host "Creating SQLPS objects" -ForegroundColor Yellow $stepStart = Get-Date $serverObject = Get-Item "SQLSERVER:\SQL\$($machineName)\DEFAULT" $dbObject = Get-Item "SQLSERVER:\SQL\$($machineName)\DEFAULT\Databases\$($database)" $masterObject = Get-Item "SQLSERVER:\SQL\$($machineName)\DEFAULT\Databases\master" $stepDuration = (New-TimeSpan -Start $stepStart -End (Get-Date)) Write-Host "SQLPS Objects created in $($stepDuration)" -ForegroundColor DarkMagenta -BackgroundColor White Write-Host "Closing all open connections to $($database) on $($instance) and setting it offline" -ForegroundColor Yellow $stepStart = Get-Date $serverObject.KillAllProcesses($database) $stepDuration = (New-TimeSpan -Start $stepStart -End (Get-Date)) Write-Host "All processes killed in $($stepDuration)" -ForegroundColor DarkMagenta -BackgroundColor White # Validation - Check if we have any open connections to this DB still [Int]$connectionCount = $serverObject.GetActiveDBConnectionCount($database) Write-Host "There are now $($connectionCount) connections to $($database)" -ForegroundColor Yellow if ($connectionCount -gt 0) { # Report the list of current connections for that database $serverObject.EnumProcesses() | Where Database -ieq $database | Select Spid, Login, Host, Status, Program | FT -AutoSize } $stepStart = Get-Date $masterObject.ExecuteNonQuery("IF (SELECT user_access_desc FROM sys.databases WHERE name = '$($database)') != 'RESTRICTED_USER' BEGIN ALTER DATABASE [$($database)] SET RESTRICTED_USER WITH ROLLBACK IMMEDIATE END;") $stepDuration = (New-TimeSpan -Start $stepStart -End (Get-Date)) Write-Host "Database swapped to restricted access in $($stepDuration)" -ForegroundColor DarkMagenta -BackgroundColor White # Validation - Check if we have any open connections to this DB still [Int]$connectionCount = $serverObject.GetActiveDBConnectionCount($database) Write-Host "There are now $($connectionCount) connections to $($database)" -ForegroundColor Yellow if ($connectionCount -gt 0) { # Report the list of current connections for that database $serverObject.EnumProcesses() | Where Database -ieq $database | Select Spid, Login, Host, Status, Program | FT -AutoSize } $stepStart = Get-Date $dbObject.SetOffline() $stepDuration = (New-TimeSpan -Start $stepStart -End (Get-Date)) Write-Host "Database taken offline in $($stepDuration)" -ForegroundColor DarkMagenta -BackgroundColor White Write-Host "$($database) has successfully been taken offline" -ForegroundColor Yellow } catch { throw }}[/code]Output from a delayed run[quote]Creating SQLPS objectsSQLPS Objects created in 00:00:01.9461207Closing all open connections to somedb on SOMESERVER and setting it offlineAll processes killed in 00:00:00.2320159There are now 0 connections to somedbDatabase swapped to restricted access in 00:00:00.4600213There are now 0 connections to somedbDatabase taken offline in 00:07:59.3086630somedb has successfully been taken offlineDatabase offline step completed in 00:08:02.4678561[/quote]Thanks very much in advance for any help as this is driving me mad!

Viewing all articles
Browse latest Browse all 6525

Trending Articles