自己投資の一つとしてチャレンジしている事を Blog で公開しています。今回はSQL Server 接続時の「エラーハンドリング」と「リトライ」方法について紹介します。(In English: Java – Error handling & Retry in SQL Server on Ubuntu No.6)
▼1. 接続時のエラーハンドリング & リトライ の必要性について
クラウドサービスに接続する際、ネットワークの瞬断の影響を受けることはよくあるため、接続時にエラーハンドリングをし、インターバルの間隔を入れてリトライすることは必須です。
以降では SQL Server on Linux への接続時、どのようにエラーハンドリングし、インターバルの間隔を入れてリトライするのか説明しています。
▼2. 接続時のエラーハンドリング & リトライ の実装
2-1. Java コードの作成
While を使って、エラーになった場合 30 秒のインターバルで 3 回のリトライを実施しています。エラーを発生させるため、SQL Server が既定でリッスンしている 1433 ポートではなく、1435 ポートに接続しています。
import java.sql.*;
import java.util.Date;
import java.lang.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ConnectionSQLSrvRetry {
private static final Logger log = LoggerFactory.getLogger(ConnectionSQLSrvRetry.class);
public static void main(String[] args) throws InterruptedException {
Connection con = null;
boolean success = false;
int retryc = 0;
int max_retries = 3;
while (!success && retryc < max_retries) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1435;" + "databaseName=TestDB;user=sa;password=password;";
con = DriverManager.getConnection(connectionUrl);
System.out.println("Current Timestamps: " + new Timestamp(new Date().getTime()));
log.info("Connect to SQL Server successfully");
success = true;
} catch (ClassNotFoundException e) {
retryc++;
System.out.println("Current Timestamps: " + new Timestamp(new Date().getTime()));
log.error("Error Message: {}, retry count is {}", e, retryc);
// 30 seconds (30000 milliseconds) interval
Thread.sleep(30000);
} catch (SQLException se) {
retryc++;
System.out.println("Current Timestamps: " + new Timestamp(new Date().getTime()));
log.error("Error Messages: {}, retry count is {}", se, retryc);
// 30 seconds (30000 milliseconds) interval
Thread.sleep(30000);
}
}
try {
if (con != null) {
con.close();
System.out.println("Current Timestamps: " + new Timestamp(new Date().getTime()));
log.info("The connection is closed");
}else{
log.info("The connection was closed");
}
} catch (SQLException sec) {
System.out.println("Current Timestamps: " + new Timestamp(new Date().getTime()));
log.error("Error MEssages of fin ", sec);
}
}
}2-2. 実行結果
エラーを発生させるため、SQL Server が既定でリッスンしている 1433 ポートではなく、1435 ポートに接続すると以下のようなエラーになります。Microsoft JDBC Driver の接続タイムアウトの時間は 15 秒 で 30 秒のインターバルを設けているため、約 45 秒間隔でエラーが 3 回出力されています。
Current Timestamps: 2020-05-20 22:08:35.503
[main] ERROR ConnectionSQLSrvRetry – Error Messages: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1435 has failed. Error: “Connection refused (Connection refused). 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.”., retry count is 1
Current Timestamps: 2020-05-20 22:09:20.159
[main] ERROR ConnectionSQLSrvRetry – Error Messages: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1435 has failed. Error: “Connection refused (Connection refused). 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.”., retry count is 2
Current Timestamps: 2020-05-20 22:10:04.716
[main] ERROR ConnectionSQLSrvRetry – Error Messages: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1435 has failed. Error: “Connection refused (Connection refused). 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.”., retry count is 3
[main] INFO ConnectionSQLSrvRetry – The connection was closed
▼3. 参考情報
以上です。参考になりましたら幸いです。