From 5f6bbecd6166f1e80ed87d7e6c2c181fe463bdef Mon Sep 17 00:00:00 2001 From: Jurriaan BW <12056384+jjhbw@users.noreply.github.com> Date: Mon, 26 Oct 2020 13:40:42 +0100 Subject: [PATCH] fix: check if the connection is closed before executing a query. This prevents SQLITE_MISUSE errors (https://sqlite.org/rescode.html#misuse) originating from sqlite itself (#6975) --- src/driver/sqlite/SqliteQueryRunner.ts | 5 +++++ .../sqlite/error-on-query-after-close.ts | 21 +++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 test/functional/sqlite/error-on-query-after-close.ts diff --git a/src/driver/sqlite/SqliteQueryRunner.ts b/src/driver/sqlite/SqliteQueryRunner.ts index 308bb72855..9e61786659 100644 --- a/src/driver/sqlite/SqliteQueryRunner.ts +++ b/src/driver/sqlite/SqliteQueryRunner.ts @@ -4,6 +4,7 @@ import {AbstractSqliteQueryRunner} from "../sqlite-abstract/AbstractSqliteQueryR import {SqliteConnectionOptions} from "./SqliteConnectionOptions"; import {SqliteDriver} from "./SqliteDriver"; import {Broadcaster} from "../../subscriber/Broadcaster"; +import { ConnectionIsNotSetError } from '../../error/ConnectionIsNotSetError'; /** * Runs queries on a single sqlite database connection. @@ -39,6 +40,10 @@ export class SqliteQueryRunner extends AbstractSqliteQueryRunner { const connection = this.driver.connection; const options = connection.options as SqliteConnectionOptions; + if (!connection.isConnected){ + throw new ConnectionIsNotSetError('sqlite') + } + return new Promise(async (ok, fail) => { const databaseConnection = await this.connect(); diff --git a/test/functional/sqlite/error-on-query-after-close.ts b/test/functional/sqlite/error-on-query-after-close.ts new file mode 100644 index 0000000000..21126ecc9f --- /dev/null +++ b/test/functional/sqlite/error-on-query-after-close.ts @@ -0,0 +1,21 @@ +import "reflect-metadata"; +import {expect} from "chai"; +import {Connection} from "../../../src/connection/Connection"; +import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../utils/test-utils"; + +describe("sqlite driver > throws an error when queried after closing connection", () => { + let connections: Connection[]; + before(async () => connections = await createTestingConnections({ + entities: [], + enabledDrivers: ["sqlite"], + })); + beforeEach(() => reloadTestingDatabases(connections)); + after(() => closeTestingConnections(connections)); + + it("should throw", () => Promise.all(connections.map(async connection => { + await connection.close() + await expect(connection.query('select * from sqlite_master;')).to.rejectedWith( + 'Connection with sqlite database is not established. Check connection configuration.' + ); + }))); +});