Skip to content

Commit dc6c154

Browse files
authored
fix(duckdb): reuse connection (#6983)
1 parent 9e64e71 commit dc6c154

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

packages/cubejs-duckdb-driver/src/DuckDBDriver.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,49 @@ export type DuckDBDriverConfiguration = {
1818
initSql?: string,
1919
};
2020

21+
type InitPromise = {
22+
connection: Connection,
23+
db: Database;
24+
};
25+
2126
export class DuckDBDriver extends BaseDriver implements DriverInterface {
22-
protected initPromise: Promise<Database> | null = null;
27+
protected initPromise: Promise<InitPromise> | null = null;
2328

2429
public constructor(
2530
protected readonly config: DuckDBDriverConfiguration = {},
2631
) {
2732
super();
2833
}
2934

30-
protected async initDatabase(): Promise<Database> {
35+
protected async init(): Promise<InitPromise> {
3136
const token = getEnv('duckdbMotherDuckToken', this.config);
3237

3338
const db = new Database(token ? `md:?motherduck_token=${token}` : ':memory:');
34-
const conn = db.connect();
39+
const connection = db.connect();
3540

3641
try {
37-
await this.handleQuery(conn, 'INSTALL httpfs', []);
42+
await this.handleQuery(connection, 'INSTALL httpfs', []);
3843
} catch (e) {
3944
if (this.logger) {
4045
console.error('DuckDB - error on httpfs installation', {
4146
e
4247
});
4348
}
4449

45-
// DuckDB will lose connection_ref on connection on error, this will lead to broken conn object
50+
// DuckDB will lose connection_ref on connection on error, this will lead to broken connection object
4651
throw e;
4752
}
4853

4954
try {
50-
await this.handleQuery(conn, 'LOAD httpfs', []);
55+
await this.handleQuery(connection, 'LOAD httpfs', []);
5156
} catch (e) {
5257
if (this.logger) {
5358
console.error('DuckDB - error on loading httpfs', {
5459
e
5560
});
5661
}
5762

58-
// DuckDB will lose connection_ref on connection on error, this will lead to broken conn object
63+
// DuckDB will lose connection_ref on connection on error, this will lead to broken connection object
5964
throw e;
6065
}
6166

@@ -81,11 +86,11 @@ export class DuckDBDriver extends BaseDriver implements DriverInterface {
8186
value: getEnv('duckdbMemoryLimit', this.config),
8287
},
8388
];
84-
89+
8590
for (const { key, value } of configuration) {
8691
if (value) {
8792
try {
88-
await this.handleQuery(conn, `SET ${key}='${value}'`, []);
93+
await this.handleQuery(connection, `SET ${key}='${value}'`, []);
8994
} catch (e) {
9095
if (this.logger) {
9196
console.error(`DuckDB - error on configuration, key: ${key}`, {
@@ -98,7 +103,7 @@ export class DuckDBDriver extends BaseDriver implements DriverInterface {
98103

99104
if (this.config.initSql) {
100105
try {
101-
await this.handleQuery(conn, this.config.initSql, []);
106+
await this.handleQuery(connection, this.config.initSql, []);
102107
} catch (e) {
103108
if (this.logger) {
104109
console.error('DuckDB - error on init sql (skipping)', {
@@ -107,18 +112,21 @@ export class DuckDBDriver extends BaseDriver implements DriverInterface {
107112
}
108113
}
109114
}
110-
111-
return db;
115+
116+
return {
117+
connection,
118+
db
119+
};
112120
}
113121

114-
protected async getConnection() {
122+
protected async getConnection(): Promise<Connection> {
115123
if (!this.initPromise) {
116-
this.initPromise = this.initDatabase();
124+
this.initPromise = this.init();
117125
}
118126

119127
try {
120-
const db = (await this.initPromise);
121-
return db.connect();
128+
const { connection } = await this.initPromise;
129+
return connection;
122130
} catch (e) {
123131
this.initPromise = null;
124132

@@ -171,7 +179,7 @@ export class DuckDBDriver extends BaseDriver implements DriverInterface {
171179

172180
public async release(): Promise<void> {
173181
if (this.initPromise) {
174-
const db = await this.initPromise;
182+
const { db } = await this.initPromise;
175183
const close = promisify(db.close).bind(db);
176184
this.initPromise = null;
177185

0 commit comments

Comments
 (0)