Skip to content

Commit b58c44e

Browse files
authored
fix(jdbc-driver): ResourceRequest timeout (#6186)
1 parent 904556b commit b58c44e

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

packages/cubejs-jdbc-driver/src/JDBCDriver.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ const mvn = require('node-java-maven');
2929

3030
let mvnPromise: Promise<void> | null = null;
3131

32+
type JdbcStatement = {
33+
setQueryTimeout: (t: number) => any,
34+
execute: (q: string) => any,
35+
};
36+
3237
const initMvn = (customClassPath: any) => {
3338
if (!mvnPromise) {
3439
mvnPromise = new Promise((resolve, reject) => {
@@ -121,20 +126,31 @@ export class JDBCDriver extends BaseDriver {
121126
},
122127
// @ts-expect-error Promise<Function> vs Promise<void>
123128
destroy: async (connection) => promisify(connection.close.bind(connection)),
124-
validate: (connection) => {
125-
const isValid = promisify(connection.isValid.bind(connection));
126-
try {
127-
return isValid(this.testConnectionTimeout() / 1000);
128-
} catch (e) {
129-
return false;
130-
}
131-
}
129+
validate: async (connection) => (
130+
new Promise((resolve) => {
131+
const createStatement = promisify(connection.createStatement.bind(connection));
132+
createStatement().then((statement: JdbcStatement) => {
133+
const setQueryTimeout = promisify(statement.setQueryTimeout.bind(statement));
134+
const execute = promisify(statement.execute.bind(statement));
135+
setQueryTimeout(this.testConnectionTimeout() / 1000).then(() => {
136+
const timer = setTimeout(() => {
137+
resolve(false);
138+
}, this.testConnectionTimeout());
139+
execute('SELECT 1').then(() => {
140+
clearTimeout(timer);
141+
resolve(true);
142+
}).catch(() => {
143+
resolve(false);
144+
});
145+
}).catch(() => {
146+
resolve(false);
147+
});
148+
});
149+
})
150+
)
132151
}, {
133152
min: 0,
134-
max:
135-
config.maxPoolSize ||
136-
getEnv('dbMaxPoolSize', { dataSource }) ||
137-
8,
153+
max: config.maxPoolSize || getEnv('dbMaxPoolSize', { dataSource }) || 8,
138154
evictionRunIntervalMillis: 10000,
139155
softIdleTimeoutMillis: 30000,
140156
idleTimeoutMillis: 30000,

0 commit comments

Comments
 (0)