@Graeme Cash
Welcome to Microsoft Q&A platform and thanks for posting your question.
I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others "I'll repost your solution in case you'd like to accept the answer.
- Ask: Hello Is it possible to connect to the serverless sql pool via Spark in Synapse using managed identity? I need to run queries against the serverless pool and have tried pyodbc. I can run queries using a SQL logon but when I try managed identity I get a timeout... OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC Driver 18 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')
driver = '{ODBC Driver 18 for SQL Server}'
cnxn = pyodbc.connect(f'Driver={driver};Server={server};Database={database};Port=1433;Authentication=ActiveDirectoryMsi')
I have also configured the Spark session to run as managed identity...
- Solution: Hello, I have managed to get it working with pyodbc by passing the connection string from the linked service to the pyodbc.connect using attrs_before...
token = mssparkutils.credentials.getConnectionStringOrCreds(linkedService=linked_service)
exptoken = b""
for i in token:
exptoken += bytes(i,'utf-8');
exptoken += bytes(1);
tokenstruct = struct.pack("=i", len(exptoken)) + exptoken;
pyodbc.connect(f'Driver={DRIVER};Server={server};Database={database};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;', attrs_before = { 1256:tokenstruct })
Graeme.
If I missed anything please let me know and I'd be happy to add it to my answer, or feel free to comment below with any additional information. If you have any other questions, please let me know. Thank you again for your time and patience throughout this issue.
---Please don’t forget to Accept Answer and Yes for "was this answer helpful" wherever the information provided helps you, this can be beneficial to other community members.