# LOCK_HELD_BY_THIS_PROCESS

A write inside a schema change's own SQLite transaction, on another connection to the same database, waited for the lock that transaction holds, so the change stopped without trying again.

## Cause

A write inside a schema change's own SQLite transaction, on another connection to the same database, waited for the lock that transaction holds, so the change stopped without trying again.

SQLite has one write lock for the whole database. A schema change that runs in a transaction, such as a migration file, takes that lock when its transaction begins and keeps it until the transaction commits or rolls back. If code that runs inside the transaction writes to the same database through a second connection, that write waits for the lock the transaction holds, and the transaction waits for that write. No wait can end that, because this process holds the lock, not another session.

So the change stops at its first try, says this process holds the lock, and rolls its transaction back: nothing of it was applied. It is not reported as [`TABLE_BUSY`](https://nextlyhq.com/docs/cli/errors/table-busy.md), which is for a lock another session holds, and is not tried again.

The second connection comes from opening the same SQLite file twice in one process: a second adapter, or a Nextly instance booted beside a command that runs in the same process. A command run as its own process, and a development start, use one connection, so neither meets this.

## Fix

* Make the write through the transaction's own handle, the one the migration's work is given, so it runs inside the transaction instead of waiting for it.
* Or make the write after the transaction commits.
* If the command was run in-process beside a booted instance, run it as its own process instead (`nextly migrate`, `nextly migrate:down`).

Then run the command again. The change was rolled back, so it applies from its start.

## Example

```text
✗ `20261020_120000_add_posts.sql` stopped: the database is locked by this process, nothing changed.
  Why:  A write made inside its transaction, on another connection to the same SQLite database, waited for the write lock that transaction holds. The transaction lets the lock go only once that write is done, so no wait could free it: it was not tried again, and the transaction was rolled back.
  Next: Make that write through the transaction's own handle, or after it commits. A second connection to the same SQLite file in one process, such as a second adapter or an instance booted beside a command run in-process, is what makes this happen.
  Docs: https://nextlyhq.com/docs/cli/errors/lock-held-by-this-process  (LOCK_HELD_BY_THIS_PROCESS)
```
