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,757 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. Ben Miller (DBAduck) 951 Reputation points
    2022-09-30T16:07:46.163+00:00

    In the project that you are building and deploying do you mean to make the database Partially Contained?

    You have to have exclusive access to the database to set the PARTIAL containment on.

    What about using

    ALTER DATABASE [databasename] SET CONTAINMENT = PARTIAL WITH ROLLBACK IMMEDIATE