У меня есть функция Java Azure, которая подключается к базе данных SQL Server, и раньше она нормально работала с «Службой SQL Azure» . Сейчас БД переносится на «Управляемый экземпляр» SQL Azure, поэтому я изменил параметры подключения, например DB_SERVER ниже, и теперь при развертывании в Azure я продолжаю получать ошибку, указанную ниже.
Кто-нибудь может помочь?
Пример кода:
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
Microsoft предоставила решение (обходной путь для проблемы с поддержкой Azure ipv6, которую Java предпочитает по умолчанию):
Добавить AppSetting
Keyname: languageWorkers: java: arguments
Значение: -Djava.net.preferIPv4Stack = true
К управляемому экземпляру можно получить доступ только через частный IP-адрес, поэтому для доступа к нему из службы приложений Azure (или функций Azure) сначала необходимо установить соединение между приложением и управляемым экземпляром. Виртуальная сеть. См. Раздел Интеграция приложения с виртуальной сетью Azure .