Skip to content

Commit 77138f3

Browse files
committed
Implemented is_disconnect and removed the do_ping implementation keeping the default behaviour
1 parent d2a3339 commit 77138f3

3 files changed

Lines changed: 5 additions & 81 deletions

File tree

src/databricks/sqlalchemy/base.py

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -336,35 +336,6 @@ def do_rollback(self, dbapi_connection):
336336
# Databricks SQL Does not support transactions
337337
pass
338338

339-
def do_ping(self, dbapi_connection):
340-
"""Test if a database connection is alive.
341-
342-
This method is called by SQLAlchemy when pool_pre_ping=True to verify
343-
connections are still valid before using them from the pool.
344-
345-
This implementation improves upon SQLAlchemy's default do_ping() by
346-
wrapping the cursor creation in a try block, which properly handles
347-
cases where the connection is closed and cursor() itself raises an exception.
348-
349-
Args:
350-
dbapi_connection: A raw DBAPI connection (from databricks-sql-connector)
351-
352-
Returns:
353-
True if the connection is alive, False otherwise.
354-
"""
355-
try:
356-
cursor = dbapi_connection.cursor()
357-
try:
358-
cursor.execute("SELECT VERSION()")
359-
cursor.fetchone()
360-
return True
361-
finally:
362-
cursor.close()
363-
except Exception:
364-
# Any exception means the connection is dead
365-
# SQLAlchemy will discard it and create a new one
366-
return False
367-
368339
def is_disconnect(self, e, connection, cursor):
369340
"""Determine if an exception indicates the connection was lost.
370341
@@ -373,9 +344,10 @@ def is_disconnect(self, e, connection, cursor):
373344
returns True, SQLAlchemy will invalidate the connection and create a new
374345
one for the next operation.
375346
376-
This is complementary to do_ping():
377-
- do_ping() is proactive: checks connection health BEFORE queries
378-
- is_disconnect() is reactive: classifies errors AFTER they occur
347+
This method is also used by SQLAlchemy's default do_ping() implementation
348+
when pool_pre_ping=True. If do_ping() encounters an exception, it calls
349+
is_disconnect() to classify the error and determine whether to invalidate
350+
the connection.
379351
380352
Args:
381353
e: The exception that was raised

tests/test_local/e2e/test_basic.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,9 +601,8 @@ def test_pool_pre_ping_with_closed_connection(connection_details):
601601
def test_is_disconnect_handles_runtime_errors(db_engine):
602602
"""Test that is_disconnect() properly classifies disconnect errors during query execution.
603603
604-
This tests the reactive error handling (complementary to pool_pre_ping's proactive checking).
605604
When a connection fails DURING a query, is_disconnect() should recognize the error
606-
and tell SQLAlchemy to invalidate the connection.
605+
and tell SQLAlchemy to invalidate the connection so the next query gets a fresh one.
607606
"""
608607
from sqlalchemy import create_engine, text
609608
from sqlalchemy.exc import DBAPIError

tests/test_local/test_ping.py

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)