Skip to content

Commit 6d0e00e

Browse files
authored
added defaultQueryTimeout for jdbc io (#35376)
1 parent fbc0045 commit 6d0e00e

File tree

1 file changed

+23
-0
lines changed
  • sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc

1 file changed

+23
-0
lines changed

sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/JdbcIO.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,9 @@ public abstract static class DataSourceConfiguration implements Serializable {
491491
@Pure
492492
abstract @Nullable ValueProvider<Integer> getMaxConnections();
493493

494+
@Pure
495+
abstract @Nullable ValueProvider<Integer> getQueryTimeout();
496+
494497
@Pure
495498
abstract @Nullable ClassLoader getDriverClassLoader();
496499

@@ -520,6 +523,8 @@ abstract Builder setConnectionInitSqls(
520523

521524
abstract Builder setMaxConnections(ValueProvider<@Nullable Integer> maxConnections);
522525

526+
abstract Builder setQueryTimeout(ValueProvider<@Nullable Integer> queryTimeout);
527+
523528
abstract Builder setDriverClassLoader(ClassLoader driverClassLoader);
524529

525530
abstract Builder setDriverJars(ValueProvider<String> driverJars);
@@ -622,6 +627,17 @@ public DataSourceConfiguration withMaxConnections(
622627
return builder().setMaxConnections(maxConnections).build();
623628
}
624629

630+
/** Sets the default query timeout that will be used for connections created by this source. */
631+
public DataSourceConfiguration withQueryTimeout(Integer queryTimeout) {
632+
checkArgument(queryTimeout != null, "queryTimeout can not be null");
633+
return withQueryTimeout(ValueProvider.StaticValueProvider.of(queryTimeout));
634+
}
635+
636+
/** Same as {@link #withQueryTimeout(Integer)} but accepting a ValueProvider. */
637+
public DataSourceConfiguration withQueryTimeout(ValueProvider<@Nullable Integer> queryTimeout) {
638+
return builder().setQueryTimeout(queryTimeout).build();
639+
}
640+
625641
/**
626642
* Sets the class loader instance to be used to load the JDBC driver. If not specified, the
627643
* default class loader is used.
@@ -657,6 +673,7 @@ void populateDisplayData(DisplayData.Builder builder) {
657673
builder.addIfNotNull(DisplayData.item("jdbcUrl", getUrl()));
658674
builder.addIfNotNull(DisplayData.item("username", getUsername()));
659675
builder.addIfNotNull(DisplayData.item("driverJars", getDriverJars()));
676+
builder.addIfNotNull(DisplayData.item("queryTimeout", getQueryTimeout()));
660677
}
661678
}
662679

@@ -701,6 +718,12 @@ public DataSource buildDatasource() {
701718
basicDataSource.setMaxTotal(maxConnections);
702719
}
703720
}
721+
if (getQueryTimeout() != null) {
722+
Integer queryTimeout = getQueryTimeout().get();
723+
if (queryTimeout != null) {
724+
basicDataSource.setDefaultQueryTimeout(queryTimeout);
725+
}
726+
}
704727
if (getDriverClassLoader() != null) {
705728
basicDataSource.setDriverClassLoader(getDriverClassLoader());
706729
}

0 commit comments

Comments
 (0)