Azure Synapse Scala: MSI access token cant read from SQL pool table

In an Azure Synapse Scala notebook, connecting to a dedicated SQL pool with a system-assigned managed identity failed. The notebook used mssparkutils.credentials.getConnectionStringOrCreds("samplesqllink") to get an access token and passed it as the accessToken property to spark.read.jdbc. But reading the table threw a com.microsoft.sqlserver.jdbc error and no data came back.

The token was fine, the SQL pool just didnt know the identity. You have to create a contained user for the Synapse workspace's managed identity in the database and grant it read access:

CREATE USER [jsynapse] FROM EXTERNAL PROVIDER;
ALTER ROLE db_datareader ADD MEMBER [jsynapse];
-- add db_datawriter too if the notebook writes back

(Replace jsynapse with your Synapse workspace name.) The reporter confirmed it worked right after adding the user. This step is easy to miss because the linked service and token setup all look correct without it.

Source: https://github.com/Azure-Samples/Synapse/issues/162

Source: https://github.com/Azure-Samples/Synapse/issues/162