Nota
O acesso a esta página requer autorização. Podes tentar iniciar sessão ou mudar de diretório.
O acesso a esta página requer autorização. Podes tentar mudar de diretório.
Para garantir que as aplicações permanecem conectadas, o driver ODBC pode restaurar as ligações inativas.
Importante
A funcionalidade de resiliência de ligação é suportada nas versões do Microsoft Azure SQL Database, Fabric SQL Database e SQL Server 2014 (e posteriores).
A funcionalidade está disponível no Windows a partir do Microsoft ODBC Driver 11 para SQL Server. Está disponível para Linux a partir da versão 17.2 do Microsoft ODBC Driver 17 para SQL Server.
Para mais informações sobre a resiliência da ligação ociosa, consulte Artigo Técnico - Resiliência da Ligação Ociosa.
Para controlar o comportamento de reconexão, o Driver ODBC para SQL Server tem duas opções:
Contagem de tentativas de conexão
A contagem de tentativas de reconexão controla o número de tentativas de reconexão se houver uma falha de conexão. Os valores válidos variam de 0 a 255. Zero (0) significa que não tente reconectar. O valor padrão é uma tentativa de religação.
Pode modificar o número de tentativas de ligação quando:
Defina ou modifique uma fonte de dados que utilize o Driver ODBC para SQL Server com o controlo Connection Retry Count .
Usa a
ConnectRetryCountpalavra-chave da string de ligação.Para recuperar o número de tentativas de nova ligação, use o atributo de ligação
SQL_COPT_SS_CONNECT_RETRY_COUNT(somente leitura). Se uma aplicação se ligar a um servidor que não suporta resiliência de ligação, retornaSQL_COPT_SS_CONNECT_RETRY_COUNT0.
Intervalo de repetição da conexão.
O intervalo de repetição de ligação especifica o número de segundos entre cada tentativa de ligação. Os valores válidos são de 1 a 60. O tempo total para reconectar não pode exceder o tempo de espera da ligação (SQL_ATTR_QUERY_TIMEOUT no SQLSetStmtAttr). O valor padrão é 10 segundos.
Pode modificar o intervalo de reintento de conexão quando:
Defina ou modifique uma fonte de dados que utilize o Driver ODBC para SQL Server com o controlo Connect Retry Interval .
Utilize a palavra-chave
ConnectRetryIntervalda string de ligação.Para obter o comprimento do intervalo de nova tentativa de ligação, utilize o atributo de conexão
SQL_COPT_SS_CONNECT_RETRY_INTERVAL(apenas leitura).
Se uma aplicação estabelecer uma ligação com SQL_DRIVER_COMPLETE_REQUIRED e mais tarde tentar executar uma instrução através de uma ligação falhada, o driver ODBC não voltará a mostrar a caixa de diálogo. Além disso, durante a recuperação em progresso,
- Durante a recuperação, qualquer chamada para
SQLGetConnectAttr(SQL_COPT_SS_CONNECTION_DEAD), deve devolverSQL_CD_FALSE. - Se a recuperação falhar, qualquer chamada para
SQLGetConnectAttr(SQL_COPT_SS_CONNECTION_DEAD), deve devolverSQL_CD_TRUE.
Os seguintes códigos de estado são devolvidos por qualquer função que execute um comando no servidor:
| Estado | Message |
|---|---|
IMC01 |
The connection is broken and recovery is not possible. The client driver attempted to recover the connection one or more times and all attempts failed. Increase the value of ConnectRetryCount to increase the number of recovery attempts. |
IMC02 |
The server did not acknowledge a recovery attempt, connection recovery is not possible. |
IMC03 |
The server did not preserve the exact client TDS version requested during a recovery attempt, connection recovery is not possible. |
IMC04 |
The server did not preserve the exact server major version requested during a recovery attempt, connection recovery is not possible. |
IMC05 |
The connection is broken and recovery is not possible. The connection is marked by the server as unrecoverable. No attempt was made to restore the connection. |
IMC06 |
The connection is broken and recovery is not possible. The connection is marked by the client driver as unrecoverable. No attempt was made to restore the connection. |
Example
O exemplo seguinte contém duas funções.
func1 mostra como pode ligar-se a um nome de fonte de dados (DSN) que utiliza o ODBC Driver for SQL Server no Windows. A DSN utiliza a Autenticação SQL Server e especifica o ID do utilizador.
func1 depois recupera o número de tentativas de ligação com SQL_COPT_SS_CONNECT_RETRY_COUNT.
func2 usa SQLDriverConnect, ConnectRetryCount a palavra-chave da string de ligação e os atributos de ligação para recuperar a definição de tentativas de ligação e intervalo de tentativas.
// Connection_resiliency.cpp
// compile with: odbc32.lib
#include <windows.h>
#include <stdio.h>
#include <sqlext.h>
#include <msodbcsql.h>
void func1() {
SQLHENV henv;
SQLHDBC hdbc;
SQLHSTMT hstmt;
SQLRETURN retcode;
SQLSMALLINT i = 21;
// Allocate environment handle
retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
// Set the ODBC version environment attribute
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
// Allocate connection handle
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
// Set login timeout to 5 seconds
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER)5, 0);
// Connect to data source
retcode = SQLConnect(hdbc, (SQLCHAR*)"MyDSN", SQL_NTS, (SQLCHAR*)"userID", SQL_NTS, (SQLCHAR*)"password_for_userID", SQL_NTS);
retcode = SQLGetConnectAttr(hdbc, SQL_COPT_SS_CONNECT_RETRY_COUNT, &i, SQL_IS_INTEGER, NULL);
// Allocate statement handle
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
// Process data
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
}
SQLDisconnect(hdbc);
}
SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
}
}
SQLFreeHandle(SQL_HANDLE_ENV, henv);
}
}
void func2() {
SQLHENV henv;
SQLHDBC hdbc1;
SQLHSTMT hstmt;
SQLRETURN retcode;
SQLSMALLINT i = 21;
#define MAXBUFLEN 255
SQLCHAR ConnStrIn[MAXBUFLEN] = "DRIVER={ODBC Driver 18 for SQL Server};SERVER=server_that_supports_connection_resiliency;Encrypt=yes;UID=userID;PWD=<password>;ConnectRetryCount=2";
SQLCHAR ConnStrOut[MAXBUFLEN];
SQLSMALLINT cbConnStrOut = 0;
// Allocate environment handle
retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
// Set the ODBC version environment attribute
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER*)SQL_OV_ODBC3_80, SQL_IS_INTEGER);
// Allocate connection handle
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc1);
// Set login timeout to 5 seconds
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
// SQLSetConnectAttr(hdbc1, SQL_LOGIN_TIMEOUT, (SQLPOINTER)5, 0);
retcode = SQLDriverConnect(hdbc1, NULL, ConnStrIn, SQL_NTS, NULL, 0, NULL, SQL_DRIVER_NOPROMPT);
}
retcode = SQLGetConnectAttr(hdbc1, SQL_COPT_SS_CONNECT_RETRY_COUNT, &i, SQL_IS_INTEGER, NULL);
retcode = SQLGetConnectAttr(hdbc1, SQL_COPT_SS_CONNECT_RETRY_INTERVAL, &i, SQL_IS_INTEGER, NULL);
}
}
}
int main() {
func1();
func2();
}