tursodatabase/turso · error · SQLException
database connection closed
Error message
database connection closed
What it means
checkOpen() throws this SQLException from every public operation on TursoConnection once close() has run (close() rolls back any pending transaction, calls the native _close, and sets the closed flag). It is the standard JDBC guard against use-after-close.
Solutions
- Guard usage: `if (conn.isClosed()) reopenOrSkip();` before executing
- Fix ownership: only the component that owns the connection closes it; pools must return, not close
- If using a pool, enable test-on-borrow so closed connections are evicted
- Check shutdown ordering so background workers stop before connections close
Example fix
// before
try (Connection conn = DriverManager.getConnection(url)) {
scheduleLater(() -> conn.createStatement()); // throws later: closed
}
// after
Connection conn = DriverManager.getConnection(url);
try {
use(conn);
} finally {
if (!conn.isClosed()) conn.close();
} Defensive patterns
Strategy: validation
Validate before calling
// guard any connection handoff
if (conn.isClosed()) {
conn = DriverManager.getConnection(url, props); // or skip the task
} Try / catch
try {
return conn.createStatement();
} catch (SQLException e) {
if ("database connection closed".equals(e.getMessage())) { reopen(); retry(); }
throw e;
} Prevention
- Establish single ownership: exactly one component is responsible for closing each connection
- With pools, return connections instead of closing them, and enable test-on-borrow/validation queries
- Order application shutdown so background workers stop before connection cleanup runs
- Remember try-with-resources on a Connection closes it — do not let escaped statements/lambdas outlive the block
When it happens
Trigger: Calling createStatement/prepareStatement/commit/rollback/setAutoCommit/setTransactionIsolation on a connection after close(); a connection pool handing out a connection another thread closed; cleanup code closing a shared connection that other components still use.
Common situations: Application shutdown ordering: a daemon thread still executing queries while the main thread closes connections; pooled connections closed by a validation sweep; forgetting that try-with-resources on the Connection closes it for later users.
Understand the failure class
- Connection failures: ECONNREFUSED, ECONNRESET, and friends — why connections get refused, reset, or dropped.
Related errors
- statement is closed
- Statement is closed
- Cannot change isolation level while transaction is active.
- Cannot commit in autocommit mode.
- Cannot operate on a closed connection
AI-assisted analysis of tursodatabase/turso@244cde92a7 (2026-08-20).
Data as JSON: /api/errors/708bbfc8867597cc.
Report an issue: GitHub.
Appendix: source
Thrown at bindings/java/src/main/java/tech/turso/core/TursoConnection.java:64
* Creates a connection using an existing TursoDB instance. This is useful for encrypted databases
* created with TursoDB.createWithEncryption().
*
* @param url e.g. "jdbc:turso:fileName"
* @param database an existing TursoDB instance
*/
public TursoConnection(String url, TursoDB database) throws SQLException {
this.url = url;
this.database = database;
this.connectionPtr = this.database.connect();
}
private static TursoDB open(String url, String filePath, Properties properties)
throws SQLException {
return TursoDB.create(url, filePath);
}
public void checkOpen() throws SQLException {
if (isClosed()) throw new SQLException("database connection closed");
}
public String getUrl() {
return url;
}
public void close() throws SQLException {
if (isClosed()) {
return;
}
// Roll back any pending transaction before closing
synchronized (transactionLock) {
if (inTransaction) {
try {
executeInternal("ROLLBACK");
} catch (SQLException e) {
// Log but don't throw - we're closing anywayView on GitHub (pinned to 244cde92a7)