> ## Documentation Index
> Fetch the complete documentation index at: https://docs.activeviam.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Connect to Synapse

> How to connect Atoti DirectQuery to Microsoft Azure Synapse using `SQLServerDataSource` delegation with a SQL login, a Microsoft Entra ID login, or a Managed Identity, and the `CONNECT` and `SELECT` permissions required

> [!NOTE]
>
> For more information on how to set up security in Azure Synapse, see the
> [official Synapse documentation](https://docs.microsoft.com/en-us/azure/synapse-analytics/security/how-to-set-up-access-control).

## Credentials

Atoti DirectQuery delegates the credentials handling to a Microsoft
[`SQLServerDataSource`](https://javadoc.io/doc/com.microsoft.sqlserver/mssql-jdbc/9.4.1.jre11/com.microsoft.sqlserver.jdbc/com/microsoft/sqlserver/jdbc/SQLServerDataSource.html).

Executing SQL in Azure Synapse requires a
[SQL user](https://docs.microsoft.com/en-us/sql/t-sql/statements/create-user-transact-sql?view=sql-server-ver16)
in the target database. As described in the Microsoft documentation, several authentication methods can be used.

The following example builds the data source with an SQL login:

```java theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
final SQLServerDataSource dataSource = new SQLServerDataSource();
// Setup authentication
// authenticationMethod = "SqlPassword", "ActiveDirectoryPassword", ... depending on which login
// are used
dataSource.setAuthentication(authenticationMethod);
dataSource.setUser(USERNAME);
dataSource.setPassword(PASSWORD);
// Set up the database to connect to
dataSource.setServerName(SERVER_NAME);
dataSource.setDatabaseName(DATABASE_NAME);
// Setup connection security parameters
dataSource.setEncrypt("true");
dataSource.setHostNameInCertificate(HOST_NAME_IN_CERTIFICATE);
dataSource.setTrustServerCertificate(false);
dataSource.setSendTimeAsDatetime(false);
// Setup number of connection attempts (first tries may fail when pool needs to warm up)
dataSource.setConnectRetryCount(CONNECT_RETRY_ATTEMPTS);
dataSource.setConnectRetryInterval(CONNECT_RETRY_INTERVAL);
dataSource.setLoginTimeout(CONNECT_RETRY_ATTEMPTS * CONNECT_RETRY_INTERVAL);
final SynapseClientSettings clientSettings =
    SynapseClientSettings.builder().dataSource(dataSource).build();
```

There are two other common ways to connect to Synapse:

* create a SQL user with a Microsoft Entra ID (formerly Azure Active Directory) login.
* create a SQL user backed by a [Managed Identity](https://docs.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/tutorial-windows-vm-access-sql).

> [!NOTE]
>
> More examples are available in the Microsoft documentation:
>
> * [Data source sample](https://docs.microsoft.com/en-us/sql/connect/jdbc/data-source-sample?view=sql-server-ver16)
> * [Synapse connection strings](https://docs.microsoft.com/en-us/azure/synapse-analytics/sql/connection-strings)

## Permissions

The SQL user used by DirectQuery needs to have the permissions to run queries in Synapse.

To be able to run queries, the `CONNECT` permission is needed on the Synapse database to be used,
then the `SELECT` permission is needed on the schemas or tables the user will use.
