Skip to content

Commit de3cfa7

Browse files
committed
fix: validate and invalidate sessions in Databricks adapter
1 parent 3687033 commit de3cfa7

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

.changeset/new-meals-listen.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"ansible-database-mcp": patch
3+
---
4+
5+
validate and invalidate sessions in Databricks adapter

src/services/db-connection/adapters/databricks-adapter.ts

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,21 @@ export class DatabricksAdapter extends EventEmitter {
105105
}
106106
}
107107

108+
/**
109+
* Check if session is still valid using getInfo
110+
*/
111+
private async isSessionValid(session: IDBSQLSession): Promise<boolean> {
112+
try {
113+
// Use getInfo to check if session is still active
114+
// TGetInfoType.CLI_SERVER_NAME = 1
115+
await session.getInfo(1);
116+
return true;
117+
} catch (error: any) {
118+
console.log('[Databricks] Session validation failed:', error.message);
119+
return false;
120+
}
121+
}
122+
108123
/**
109124
* Acquire a session from the pool
110125
*/
@@ -113,7 +128,19 @@ export class DatabricksAdapter extends EventEmitter {
113128
throw new Error('Connection pool has been destroyed');
114129
}
115130

116-
// If there's an available session, use it
131+
// Clean up invalid sessions from available pool
132+
const validSessions: IDBSQLSession[] = [];
133+
for (const session of this.availableSessions) {
134+
if (await this.isSessionValid(session)) {
135+
validSessions.push(session);
136+
} else {
137+
console.log('[Databricks] Removing invalid session from pool');
138+
await this.invalidateSession(session);
139+
}
140+
}
141+
this.availableSessions = validSessions;
142+
143+
// If there's an available valid session, use it
117144
if (this.availableSessions.length > 0) {
118145
const session = this.availableSessions.pop()!;
119146
this.activeSessions.add(session);
@@ -222,6 +249,30 @@ export class DatabricksAdapter extends EventEmitter {
222249
};
223250
}
224251

252+
/**
253+
* Invalidate a session and remove it from all pools
254+
*/
255+
private async invalidateSession(session: IDBSQLSession): Promise<void> {
256+
try {
257+
// Remove from all tracking arrays
258+
this.sessions = this.sessions.filter(s => s !== session);
259+
this.availableSessions = this.availableSessions.filter(s => s !== session);
260+
this.activeSessions.delete(session);
261+
262+
// Try to close the session
263+
try {
264+
await session.close();
265+
} catch (error) {
266+
// Session might already be closed, ignore error
267+
console.log('[Databricks] Error closing invalid session:', error);
268+
}
269+
270+
console.log('[Databricks] Session invalidated and removed from pool');
271+
} catch (error) {
272+
console.error('[Databricks] Error invalidating session:', error);
273+
}
274+
}
275+
225276
/**
226277
* Destroy the connection pool
227278
*/

0 commit comments

Comments
 (0)