@@ -1426,26 +1426,27 @@ def test_databricks(make_config):
14261426 )
14271427
14281428
1429- def test_databricks_shared_connection (make_config ):
1430- """Databricks should use a shared connection pool to prevent OAuth CSRF races.
1429+ def test_databricks__u2m_oauth__shared_connection_pool (make_config ):
1430+ """Databricks should use a shared connection pool when using OAuth to prevent CSRF races.
14311431
14321432 When concurrent_tasks > 1, ThreadLocalConnectionPool creates one connection per
14331433 thread. For U2M OAuth, each thread triggers its own browser-based OAuth flow;
14341434 these race on the CSRF state parameter and cause MismatchingStateError.
14351435
1436- Setting shared_connection = True causes ThreadLocalSharedConnectionPool to be
1437- used instead: a single connection is created (behind a lock) and each thread
1438- gets its own cursor, so only one OAuth flow is ever initiated.
1436+ For non-U2M OAuth authentication types (e.g. access_token and M2M OAuth) then
1437+ ThreadLocalConnectionPool should still be used.
14391438
1440- See: https://github.com/tobymao/sqlmesh/issues/5646
1439+ See:
1440+ https://github.com/tobymao/sqlmesh/issues/5646
1441+ https://github.com/SQLMesh/sqlmesh/issues/5858
14411442 """
14421443 from sqlmesh .utils .connection_pool import ThreadLocalSharedConnectionPool
14431444
14441445 config = make_config (
14451446 type = "databricks" ,
14461447 server_hostname = "dbc-test.cloud.databricks.com" ,
14471448 http_path = "sql/test/foo" ,
1448- access_token = "test-token " ,
1449+ auth_type = "databricks-oauth " ,
14491450 concurrent_tasks = 4 ,
14501451 )
14511452 assert isinstance (config , DatabricksConnectionConfig )
@@ -1455,6 +1456,43 @@ def test_databricks_shared_connection(make_config):
14551456 assert isinstance (adapter ._connection_pool , ThreadLocalSharedConnectionPool )
14561457
14571458
1459+ @patch .object (DatabricksConnectionConfig , "_connection_factory_with_kwargs" )
1460+ def test_databricks__m2m_oauth__connection_pool (mock_connection_factory_with_kwargs , make_config ):
1461+ from sqlmesh .utils .connection_pool import ThreadLocalConnectionPool
1462+
1463+ config = make_config (
1464+ type = "databricks" ,
1465+ server_hostname = "dbc-test.cloud.databricks.com" ,
1466+ http_path = "sql/test/foo" ,
1467+ auth_type = "databricks-oauth" ,
1468+ oauth_client_id = "oauth_client_id" ,
1469+ oauth_client_secret = "oauth_client_secret" ,
1470+ concurrent_tasks = 4 ,
1471+ )
1472+ assert isinstance (config , DatabricksConnectionConfig )
1473+ assert config .shared_connection is False
1474+
1475+ adapter = config .create_engine_adapter ()
1476+ assert isinstance (adapter ._connection_pool , ThreadLocalConnectionPool )
1477+
1478+
1479+ def test_databricks__access_token__connection_pool (make_config ):
1480+ from sqlmesh .utils .connection_pool import ThreadLocalConnectionPool
1481+
1482+ config = make_config (
1483+ type = "databricks" ,
1484+ server_hostname = "dbc-test.cloud.databricks.com" ,
1485+ http_path = "sql/test/foo" ,
1486+ access_token = "any-token" ,
1487+ concurrent_tasks = 4 ,
1488+ )
1489+ assert isinstance (config , DatabricksConnectionConfig )
1490+ assert config .shared_connection is False
1491+
1492+ adapter = config .create_engine_adapter ()
1493+ assert isinstance (adapter ._connection_pool , ThreadLocalConnectionPool )
1494+
1495+
14581496def test_engine_import_validator ():
14591497 with pytest .raises (
14601498 ConfigError ,
0 commit comments