TeamCity upgrade fails with "Can't take exclusive lock when db lock is not held"

ro flag

When upgrading to TeamCity 2023.05 (using the Docker image), I received the error "Can't take exclusive lock when db lock is not held" and the detailed instruction:

<pre>java.lang.IllegalStateException: Can't take an exclusive lock when db lock is not held
    at jetbrains.buildServer.serverSide.db.Heartbeat.tryGetExclusiveLock(
    at jetbrains.buildServer.serverSide.db.TeamCityDatabaseManager.tryGetExclusiveLock(
    at jetbrains.buildServer.maintenance.StartupProcessor.lockDatabaseExclusively(
    at jetbrains.buildServer.maintenance.StartupProcessor.doUpgradeRequired(
    at jetbrains.buildServer.maintenance.StartupProcessor.checkUpgradeRequired(
    at jetbrains.buildServer.maintenance.StartupProcessor.doDatabaseChecking(
    at jetbrains.buildServer.maintenance.StartupProcessor.access$1000(
    at jetbrains.buildServer.maintenance.StartupProcessor$
    at jetbrains.buildServer.util.NamedThreadFactory.executeWithNewThreadName(
    at jetbrains.buildServer.maintenance.StartupProcessor.processConcreteStage(
    at jetbrains.buildServer.maintenance.StartupProcessor.processConcreteStageSafe(
    at jetbrains.buildServer.maintenance.StartupProcessor.processTeamCityLifecycle(
    at jetbrains.buildServer.maintenance.StartupProcessor.access$000(
    at jetbrains.buildServer.maintenance.StartupProcessor$
    at java.base/

I spent a lot of time trying to get to the bottom of this, as I initially thought it was something wrong with the actual database, but could not find any locks.

ro flag

The solution was to drop the records in the db_heartbeat table in the teamcity database.

Check if there are any records:

select * from db_heartbeat;

Remove all records:

TRUNCATE TABLE db_heartbeat;

Verify that they were dropped:

select * from db_heartbeat;

Once done, you should be able to restart TeamCity and redo the migration.

I sit in a Tesla and translated this thread with Ai:


Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.