Skip to content

Support pinning DB instances#238

Merged
staticlibs merged 1 commit intoduckdb:mainfrom
staticlibs:persist_db
Jun 1, 2025
Merged

Support pinning DB instances#238
staticlibs merged 1 commit intoduckdb:mainfrom
staticlibs:persist_db

Conversation

@staticlibs
Copy link
Collaborator

@staticlibs staticlibs commented May 31, 2025

Currently when the last connection to a DB instance is closed, this DB instance is destroyed.

This is not a big problem with file DBs, where subsequent connection from the same process can just re-open the file (though some state, like attached DBs is lost), but this is a problem with tagged in-memory DBs (jdbc:duckdb:memory:tag1 URLs), where all DB state is lost when the DB is closed.

This does not apply to untagged :memory: DBs, which are private to a single connection.

This change adds new connection property jdbc_pin_db (boolean, false by default), when it is enabled, then the DB is pinned and is kept alive in-memory even after the last connection to it is closed.

DuckDBDriver.releaseDB(url) method is added to allow client code to release such DBs.

DBs that are left pinned are released automatically on JVM shutdown.

Testing: new test added.

edit: added shutdown hook note.

Currently when the last connection to a DB instance is closed, this DB
instance is destroyed.

This is not a big problem with file DBs, where subsequent connection
from the same process can just re-open the file (though some state,
like attached DBs is lost), but this is a problem with tagged in-memory
DBs (`jdbc:duckdb:memory:tag1` URLs), where all DB state is lost when
the DB is closed.

This does not apply to untagged `:memory:` DBs, which are private to a
single connection.

This change adds new connection property `jdbc_pin_db` (boolean,
`false` by default), when it is enabled, then the DB is pinned and is
kept alive in-memory even after the last connection to it is closed.

`DuckDBDriver.releaseDB(url)` method is added to allow client code to
release such DBs.

DBs that are left pinned are released automatically on JVM shutdown.

Testing: new test added.
@staticlibs staticlibs changed the title Support keeping DBs alive Support pinning DB instances Jun 1, 2025
@staticlibs staticlibs merged commit 66bf0e7 into duckdb:main Jun 1, 2025
10 checks passed
@staticlibs staticlibs deleted the persist_db branch June 1, 2025 14:16
staticlibs added a commit to staticlibs/duckdb-java that referenced this pull request Jun 16, 2025
This is a backport of the PR duckdb#238 to `v1.3-ossivalis` stable branch.

Currently when the last connection to a DB instance is closed, this DB
instance is destroyed.

This is not a big problem with file DBs, where subsequent connection
from the same process can just re-open the file (though some state,
like attached DBs is lost), but this is a problem with tagged in-memory
DBs (`jdbc:duckdb:memory:tag1` URLs), where all DB state is lost when
the DB is closed.

This does not apply to untagged `:memory:` DBs, which are private to a
single connection.

This change adds new connection property `jdbc_pin_db` (boolean,
`false` by default), when it is enabled, then the DB is pinned and is
kept alive in-memory even after the last connection to it is closed.

`DuckDBDriver.releaseDB(url)` method is added to allow client code to
release such DBs.

DBs that are left pinned are released automatically on JVM shutdown.

Testing: new test added.
staticlibs added a commit that referenced this pull request Jun 16, 2025
This is a backport of the PR #238 to `v1.3-ossivalis` stable branch.

Currently when the last connection to a DB instance is closed, this DB
instance is destroyed.

This is not a big problem with file DBs, where subsequent connection
from the same process can just re-open the file (though some state,
like attached DBs is lost), but this is a problem with tagged in-memory
DBs (`jdbc:duckdb:memory:tag1` URLs), where all DB state is lost when
the DB is closed.

This does not apply to untagged `:memory:` DBs, which are private to a
single connection.

This change adds new connection property `jdbc_pin_db` (boolean,
`false` by default), when it is enabled, then the DB is pinned and is
kept alive in-memory even after the last connection to it is closed.

`DuckDBDriver.releaseDB(url)` method is added to allow client code to
release such DBs.

DBs that are left pinned are released automatically on JVM shutdown.

Testing: new test added.
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