Skip to content

Commit 15de32f

Browse files
authored
Connection error handling fixups (#433)
Fixes a connection error exception handler case. - [x] Add some backoff logic to the reconnection attempts.
1 parent 159e703 commit 15de32f

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/main/java/com/oltpbenchmark/api/Worker.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.sql.SQLException;
3131
import java.sql.SQLRecoverableException;
3232
import java.sql.Statement;
33+
import java.time.Duration;
3334
import java.util.HashMap;
3435
import java.util.Map;
3536
import java.util.Map.Entry;
@@ -400,7 +401,17 @@ protected final void doWork(DatabaseType databaseType, TransactionType transacti
400401
if (this.conn == null) {
401402
try {
402403
if (!this.configuration.getNewConnectionPerTxn()) {
403-
LOG.info("(Re)connecting to database.");
404+
if (retryCount > 0) {
405+
Duration delay = Duration.ofSeconds(Math.min(retryCount, 5));
406+
LOG.info("Backing off {} seconds before reconnecting.", delay.toSeconds());
407+
try {
408+
Thread.sleep(delay);
409+
} catch (InterruptedException ex) {
410+
// pass
411+
}
412+
} else {
413+
LOG.info("(Re)connecting to database.");
414+
}
404415
}
405416
this.conn = this.benchmark.makeConnection();
406417
this.conn.setAutoCommit(false);
@@ -598,6 +609,8 @@ protected final void doWork(DatabaseType databaseType, TransactionType transacti
598609
} catch (SQLException e) {
599610
LOG.error("Connection couldn't be closed.", e);
600611
}
612+
} else if (this.conn == null) {
613+
LOG.warn("Connection error detected.");
601614
}
602615

603616
switch (status) {

src/main/java/com/oltpbenchmark/util/SQLUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ public static boolean isConnectionErrorException(SQLException ex) {
715715
|| ex.getMessage().endsWith("connection was unexpectedly lost.")
716716
|| ex.getMessage().endsWith("Command could not be timed out. Reason: Socket closed")
717717
|| ex.getMessage().endsWith("No operations allowed after connection closed")
718-
|| ex.getMessage().endsWith("Communications link failure")) {
718+
|| ex.getMessage().contains("Communications link failure")) {
719719
return true;
720720
}
721721

0 commit comments

Comments
 (0)