Azure SQL – Connecting from Java Azure Function to Managed Azure SQL Instance

azurejavasql server

I have a Java Azure function that connects to an SQL Server database, and it used to work fine with "Azure SQL Service". The DB is now being migrated to Azure SQL "Managed Instance", so I changed the connection parameters like DB_SERVER below, and now when I deploy to Azure I keep getting the error below.

  • The same works fine from my laptop
  • VNet integration was made
  • tcpping for the same DB_SERVER and port 1433 is working
  • Another function written in .net using connection string is working in the same network configuration.
  • Deploying a REST triggered Spring Boot app service instead of Azure function also works.

Can anyone help please?

Sample code:

DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dataSource.setUrl(String.format("jdbc:sqlserver://%s;databaseName=%s", DB_SERVER, DB_NAME));
dataSource.setUsername(DB_USER_NAME);
dataSource.setPassword(DB_PASSWORD);
jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.query(...);

pom.xml stuff:

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>7.0.0.jre8</version>
</dependency>

<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

Exception:

SQLServerException: The TCP/IP connection to the host {our server host here}, port 1433 has failed. Error: "connect timed out. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
Stack: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host {our server host here}, port 1433 has failed. Error: "connect timed out. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:81)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:371)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:446)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:456)
    at 

Best Answer

Managed Instance can be accessed only through a private IP address so in order to access it from Azure App Service (or Azure Functions) you first need to make a connection between the application and the Managed Instance VNet. See Integrate your app with an Azure Virtual Network.

https://docs.microsoft.com/en-us/azure/sql-database/sql-database-managed-instance-connect-app#connect-an-azure-app-service-hosted-application