Home
%3CLINGO-SUB%20id%3D%22lingo-sub-1105276%22%20slang%3D%22en-US%22%3ELesson%20Learned%20%23118%3A%20Having%20ALTER%20DATABASE%20failed%20because%20a%20lock%20could..after%20creating%20a%20MI%20database%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1105276%22%20slang%3D%22en-US%22%3E%3CP%3EToday%2C%20we%20worked%20on%20a%20new%20service%20request%20where%20our%20customer%20tried%20to%20modify%20a%20parameter%20of%20a%20new%20database%2C%20just%20a%20couple%20minutes%20after%20that%20they%20have%20created%20it.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EDuring%20aprox.%205-10%20minutes%20they%20were%20not%20able%20to%20modify%20this%20parameter%20after%20the%20database%20was%20created%20and%20our%20customer%20asked%20why.%3C%2FP%3E%0A%3CP%3EEvery%20time%20that%20they%20tried%20to%20change%20the%20value%20of%20this%20parameter%20they%20got%20the%20following%20error%20message%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EMsg%205061%2C%20Level%2016%2C%20State%201%2C%20Line%2027%3C%2FLI%3E%0A%3CLI%3EALTER%20DATABASE%20failed%20because%20a%20lock%20could%20not%20be%20placed%20on%20database%20'databasename'.%20Try%20again%20later.%3C%2FLI%3E%0A%3CLI%3EMsg%205069%2C%20Level%2016%2C%20State%201%2C%20Line%2027%3C%2FLI%3E%0A%3CLI%3EALTER%20DATABASE%20statement%20failed.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EWhy%3F%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EPlease%2C%20remember%20that%20the%20creation%20of%20the%20database%20is%20an%20asynchronous%20process%20and%20even%20when%20SQL%20SERVER%20Management%20Studio%20returned%20the%20message%20that%20the%20database%20has%20been%20created%2C%20other%20process%20may%20take%20place%20in%20the%20background%20for%20example%2C%20enabling%20TDE%2C%20running%20a%20backup%2C%20etc..%20for%20this%20new%20database.%20If%20any%20of%20this%20parameter%20needs%20to%20have%20an%20exclusive%20lock%20during%20this%20period%20of%20time%20you%20could%20see%20this%20error%20message.%26nbsp%3B%3C%2FLI%3E%0A%3CLI%3EAlso%2C%20we%20need%20to%20know%20that%20just%20only%20a%20few%20parameters%20are%20enabled%20at%20the%20moment%20of%20the%20execution%20of%26nbsp%3BCREATE%20DATABASE%20as%20TSQL%20or%20using%20SQL%20Server%20Management%20Studio%20-%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Fstatements%2Fcreate-database-transact-sql%3Fview%3Dazuresqldb-mi-current%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Fstatements%2Fcreate-database-transact-sql%3Fview%3Dazuresqldb-mi-current%26nbsp%3B%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EHow%20we%20found%20it%3A%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EDuring%20the%20execution%20of%20ALTER%20DATABASE%20and%20before%20having%20the%20error%20message%20that%20is%20not%20possible%20to%20obtain%20the%20exclusive%20lock%2C%20we%20found%20that%20some%20operations%20are%20doing%20in%20the%20background%20blocking%20any%20exclusive%20locking%20using%20this%20article%3A%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-database-support-blog%2Flesson-learned-22-how-to-identify-blocking-issues%2Fba-p%2F368865%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3Ehttps%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-database-support-blog%2Flesson-learned-22-how-to-identify-blocking-issues%2Fba-p%2F368865%3C%2FA%3E%26nbsp%3B%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EIf%20you%20want%20to%20automate%20this%20process%20using%20a%20script%2C%20for%20example%2C%20to%20change%20the%20containment%20parameter%3A%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EWe%20suggested%20to%20use%20the%20following%20script%20as%20an%20example%3A%3CUL%3E%0A%3CLI%3EFirst%2C%20create%20a%20store%20procedure%20to%20change%20the%20parameter%20of%20the%20database.%20You%20could%20parametrized%20as%20you%20wish.%26nbsp%3B%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%20style%3D%22padding-left%3A%2090px%3B%22%3ECREATE%20OR%20ALTER%20PROCEDURE%20ChangeStatus%20%3CBR%20%2F%3EAS%3CBR%20%2F%3EBEGIN%3CBR%20%2F%3EBEGIN%20TRY%20%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20ALTER%20DATABASE%20%3CDATABASENAME%3E%20set%20containment%20%3Dpartial%3CBR%20%2F%3EEND%20TRY%20%3CBR%20%2F%3EBEGIN%20CATCH%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BPRINT%20%40%40ERROR%20%3CBR%20%2F%3EEND%20CATCH%3CBR%20%2F%3EEND%3C%2FDATABASENAME%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2090px%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ESecond%2C%20create%20a%20script%20to%20retry%20the%20operation%20until%20this%20operation%20has%20been%20finished.%26nbsp%3B%26nbsp%3B%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%20style%3D%22padding-left%3A%2060px%3B%22%3EDECLARE%20%40VALUE%20AS%20INT%20%3D%20-1%20%3CBR%20%2F%3EDECLARE%20%40nTimes%20as%20INT%20%3D%200%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2090px%3B%22%3EWHILE(%40nTimes%20%26lt%3B%3D10)%3CBR%20%2F%3EBEGIN%20%3CBR%20%2F%3ESELECT%20%40Value%20%3D%20(SELECT%20Containment%20FROM%20sys.databases%20WHERE%20name%3D'databasename')%3CBR%20%2F%3E%26nbsp%3B%20IF%20%40VALUE%20%26lt%3B%26gt%3B%200%20BREAK%3B%3CBR%20%2F%3E%26nbsp%3B%20SET%20%40nTimes%20%3D%20%40ntimes%20%2B1%20%3CBR%20%2F%3E%26nbsp%3B%20IF%20%40Value%20%3D%200%3CBR%20%2F%3EBEGIN%20%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3BPRINT%20'Doing%20%23'%20%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3BEXEC%20ChangeStatus%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20WAITFOR%20DELAY%20'00%3A02%3A00'%3CBR%20%2F%3EEND%3CBR%20%2F%3EEND%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2060px%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EEnjoy!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1105276%22%20slang%3D%22en-US%22%3E%3CP%3EToday%2C%20we%20worked%20on%20a%20new%20service%20request%20where%20our%20customer%20tried%20to%20modify%20a%20parameter%20of%20a%20new%20database%2C%20just%20a%20couple%20minutes%20after%20that%20they%20have%20created%20it.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EDuring%20aprox.%205-10%20minutes%20they%20were%20not%20able%20to%20modify%20this%20parameter%20after%20the%20database%20was%20created%20and%20our%20customer%20asked%20why.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-TEASER%3E

Today, we worked on a new service request where our customer tried to modify a parameter of a new database, just a couple minutes after that they have created it. 

 

During aprox. 5-10 minutes they were not able to modify this parameter after the database was created and our customer asked why.

Every time that they tried to change the value of this parameter they got the following error message:

 

  • Msg 5061, Level 16, State 1, Line 27
  • ALTER DATABASE failed because a lock could not be placed on database 'databasename'. Try again later.
  • Msg 5069, Level 16, State 1, Line 27
  • ALTER DATABASE statement failed.

 

Why? 

  • Please, remember that the creation of the database is an asynchronous process and even when SQL SERVER Management Studio returned the message that the database has been created, other process may take place in the background for example, enabling TDE, running a backup, etc.. for this new database. If any of this parameter needs to have an exclusive lock during this period of time you could see this error message. 
  • Also, we need to know that just only a few parameters are enabled at the moment of the execution of CREATE DATABASE as TSQL or using SQL Server Management Studio - https://docs.microsoft.com/en-us/sql/t-sql/statements/create-database-transact-sql?view=azuresqldb-m...

 

How we found it:

 

 

If you want to automate this process using a script, for example, to change the containment parameter:

 

  • We suggested to use the following script as an example:
    • First, create a store procedure to change the parameter of the database. You could parametrized as you wish. 

CREATE OR ALTER PROCEDURE ChangeStatus
AS
BEGIN
BEGIN TRY
        ALTER DATABASE <databasename> set containment =partial
END TRY
BEGIN CATCH
     PRINT @@ERROR
END CATCH
END

 

  • Second, create a script to retry the operation until this operation has been finished.  

DECLARE @VALUE AS INT = -1
DECLARE @nTimes as INT = 0

WHILE(@nTimes <=10)
BEGIN
SELECT @Value = (SELECT Containment FROM sys.databases WHERE name='databasename')
  IF @VALUE <> 0 BREAK;
  SET @nTimes = @ntimes +1
  IF @Value = 0
BEGIN
   PRINT 'Doing #'
   EXEC ChangeStatus
    WAITFOR DELAY '00:02:00'
END
END

 

Enjoy!