Lock could not be placed when publish from sstd sql project issue

seba stian 26 Reputation points
2022-09-22T11:29:04.237+00:00

Issue appears when I try to publish from ssdt sql project to my local database.

(47,1): SQL72014: .Net SqlClient Data Provider: Msg 5061, Level 16, State 1, Line 5 ALTER DATABASE failed because a lock could not be placed on database '*'. Try again later.
(43,0): SQL72045: Script execution error. The executed script:
IF EXISTS (SELECT 1
FROM [master].[dbo].[sysdatabases]
WHERE [name] = N'$(DatabaseName)')
BEGIN
ALTER DATABASE [$(DatabaseName)]
SET CONTAINMENT = PARTIAL
WITH ROLLBACK IMMEDIATE;
END

I tried many things:

  • setup db in single user mode
  • execute the same from sqlpackage

I have workaround - I generate script then remove lock and execute.

Anyone has the same issue and resolved it?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,691 questions
0 comments No comments
{count} votes

Accepted answer
  1. Simon Sabin 76 Reputation points Microsoft Regional Director
    2022-09-30T12:09:58.31+00:00

    @seba stian the issue I expect is that the database was created outside of ssdt, and the database settings in the ssdt project don't match that of the deployed database.

    Under the project properties on the "Project Settings" tab there is a "Database settings" section. On the Miscellaneous tab is the Containment option. Check that matches what is set for the deployed database.


5 additional answers

Sort by: Most helpful
  1. Erland Sommarskog 100.9K Reputation points MVP
    2022-09-22T21:35:00.057+00:00

    I don't work with sqlpackage, so I don't know what it's up to, or why it generates that code. Or is it your intention to make the database partially contained?

    In any case, I found that when testing that the statement will block if there is another process in the database. You could modify the above to read:

       IF EXISTS (SELECT 1  
       FROM [master].[dbo].[sysdatabases]  
       WHERE [name] = N'$(DatabaseName)')  
       BEGIN  
           ALTER DATABASE  [$(DatabaseName)] SET  SINGLE_USER WITH ROLLBACK IMMEDIATE  
         
          ALTER DATABASE [$(DatabaseName)]  
          SET CONTAINMENT = PARTIAL  
          WITH ROLLBACK IMMEDIATE;  
       END  
    
    0 comments No comments

  2. PandaPan-MSFT 1,901 Reputation points
    2022-09-23T06:37:11.757+00:00

    Hi @seba stian ,
    How about trying restarting your database may solve this error:

    Firstly you can use the following command to find your sql SPID:
    EXEC sp_who2
    244108-image.png
    Then you can use the command to shut it down
    KILL (SPID)
    At last make the database online again
    ALTER DATABASE [test] SET ONLINE
    GO


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment"

    0 comments No comments

  3. PandaPan-MSFT 1,901 Reputation points
    2022-09-26T08:23:46.207+00:00

    Hi @seba stian ,
    We have not received a response from you. Did the reply could help you? If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. By doing so, it will benefit all community members who are having this similar issue. Your contribution is highly appreciated.

    0 comments No comments

  4. seba stian 26 Reputation points
    2022-09-29T09:04:36.05+00:00

    @Erland Sommarskog , ssdt project generates script (dacpac) and this should just work by publish of it. I can edit script, by just removing locking part and it works. It should just work from "Publish".
    @PandaPan-MSFT , I killed all what I thought it is connected to db. As I also mentioned I also moved db into single user. It didn't help.