Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@
import java.util.Optional;
import java.util.stream.Collectors;
import javax.sql.DataSource;
import org.apache.beam.sdk.util.BackOff;
import org.apache.beam.sdk.util.BackOffUtils;
import org.apache.beam.sdk.util.FluentBackoff;
import org.apache.beam.sdk.util.Sleeper;
import org.apache.beam.sdk.values.KV;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Lists;
import org.joda.time.Duration;
import org.postgresql.ds.PGSimpleDataSource;
import org.testcontainers.containers.JdbcDatabaseContainer;

Expand Down Expand Up @@ -86,9 +91,40 @@ public static void createTable(
fieldsAndTypes.stream()
.map(kv -> kv.getKey() + " " + kv.getValue())
.collect(Collectors.joining(", "));
try (Connection connection = dataSource.getConnection()) {
try (Statement statement = connection.createStatement()) {
statement.execute(String.format("create table %s (%s)", tableName, fieldsList));
SQLException exception = null;
Sleeper sleeper = Sleeper.DEFAULT;
BackOff backoff =
FluentBackoff.DEFAULT
.withInitialBackoff(Duration.standardSeconds(1))
.withMaxCumulativeBackoff(Duration.standardMinutes(5))
.withMaxRetries(4)
.backoff();
while (true) {
// This is not implemented as try-with-resources because it appears that try-with-resources is
// not correctly catching the PSQLException thrown by dataSource.getConnection()
Connection connection = null;
try {
connection = dataSource.getConnection();
try (Statement statement = connection.createStatement()) {
statement.execute(String.format("create table %s (%s)", tableName, fieldsList));
return;
}
} catch (SQLException e) {
exception = e;
} finally {
if (connection != null) {
connection.close();
Copy link
Contributor

@Abacn Abacn Oct 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably it is closing the connection caused issue. dataSource owns a connection pool and manage the connections. Closing it pre-maturely may have caused racing conditions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like it is happening in .getConnection based on the stacktrace

}
}
boolean hasNext;
try {
hasNext = BackOffUtils.next(sleeper, backoff);
} catch (Exception e) {
throw new RuntimeException(e);
}
if (!hasNext) {
// we tried the max number of times
throw exception;
}
}
}
Expand Down