Skip to content

Let only the thread that starts a DB session use it - #96

Merged
arybczak merged 1 commit into
masterfrom
bound-thread
Sep 28, 2026
Merged

arybczak merged 1 commit into
masterfrom
bound-thread

Conversation

@arybczak

@arybczak arybczak commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Before, any thread could use a DB session that it got from another thread, e.g. by forking. The threads then shared one connection and one transaction. A query in one thread waited for the queries in the other threads, and a query from a forked thread ran in the transaction of the parent. If the parent session ended first, the forked thread failed.

Now the thread that starts a session owns it. If another thread uses the session, the library throws ThreadMismatchError. To run queries from another thread, start a new session in that thread with withNewSession, the new name of withNewConnection. The name changed because the new session does not always acquire a connection immediately.

A thread can start its new session at any time. It does not wait for the queries of the parent session, and it works also after the parent session ended.

@arybczak
arybczak force-pushed the bound-thread branch 2 times, most recently from 70a6fc4 to 079c7e5 Compare September 21, 2026 18:16
@arybczak arybczak changed the title Bind DB sessions to the thread that started them Let only the thread that starts a DB session use it Sep 22, 2026
Before, any thread could use a DB session that it got from another
thread, e.g. by forking. The threads then shared one connection and one
transaction. A query in one thread waited for the queries in the other
threads, and a query from a forked thread ran in the transaction of the
parent. If the parent session ended first, the forked thread failed.

Now the thread that starts a session owns it. If another thread uses
the session, the library throws ThreadMismatchError. To run queries from
another thread, start a new session in that thread with withNewSession,
the new name of withNewConnection. The name changed because the new
session does not always acquire a connection immediately.

A thread can start its new session at any time. It does not wait for
the queries of the parent session, and it works also after the parent
session ended.
@arybczak
arybczak merged commit 15a3322 into master Sep 28, 2026
9 checks passed
@arybczak
arybczak deleted the bound-thread branch September 28, 2026 10:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant