Skip to content

Commit

Permalink
Merge pull request #165 from rafal-kowalski/164-use-explicit-limit-in…
Browse files Browse the repository at this point in the history
…-polling-query-for-postgresql

Use explicit LIMIT in scheduler task polling query for PostgreSQL
  • Loading branch information
kagkarlsson authored Jan 19, 2021
2 parents 70d32e4 + 1e971da commit 586a124
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,17 @@ public void getScheduledExecutions(ScheduledExecutionsFilter filter, String task
@Override
public List<Execution> getDue(Instant now, int limit) {
final UnresolvedFilter unresolvedFilter = new UnresolvedFilter(taskResolver.getUnresolved());

final String explicitLimit = jdbcCustomization.supportsExplicitQueryLimitPart() ? jdbcCustomization.getQueryLimitPart(limit) : "";
return jdbcRunner.query(
"select * from " + tableName + " where picked = ? and execution_time <= ? " + unresolvedFilter.andCondition() + " order by execution_time asc",
"select * from " + tableName + " where picked = ? and execution_time <= ? " + unresolvedFilter.andCondition() + " order by execution_time asc" + explicitLimit,
(PreparedStatement p) -> {
int index = 1;
p.setBoolean(index++, false);
jdbcCustomization.setInstant(p, index++, now);
unresolvedFilter.setParameters(p, index);
p.setMaxRows(limit);
if (!jdbcCustomization.supportsExplicitQueryLimitPart()) {
p.setMaxRows(limit);
}
},
new ExecutionResultSetMapper()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class AutodetectJdbcCustomization implements JdbcCustomization {

private static final Logger LOG = LoggerFactory.getLogger(AutodetectJdbcCustomization.class);
public static final String MICROSOFT_SQL_SERVER = "Microsoft SQL Server";
public static final String POSTGRESQL = "PostgreSQL";
private final JdbcCustomization jdbcCustomization;

public AutodetectJdbcCustomization(DataSource dataSource) {
Expand All @@ -42,6 +43,9 @@ public AutodetectJdbcCustomization(DataSource dataSource) {
if (databaseProductName.equals(MICROSOFT_SQL_SERVER)) {
LOG.info("Using MSSQL jdbc-overrides.");
detectedCustomization = new MssqlJdbcCustomization();
} else if (databaseProductName.equals(POSTGRESQL)) {
LOG.info("Using PostgreSQL jdbc-overrides.");
detectedCustomization = new PostgreSqlJdbcCustomization();
}

} catch (SQLException e) {
Expand All @@ -60,4 +64,14 @@ public void setInstant(PreparedStatement p, int index, Instant value) throws SQL
public Instant getInstant(ResultSet rs, String columnName) throws SQLException {
return jdbcCustomization.getInstant(rs, columnName);
}

@Override
public boolean supportsExplicitQueryLimitPart() {
return jdbcCustomization.supportsExplicitQueryLimitPart();
}

@Override
public String getQueryLimitPart(int limit) {
return jdbcCustomization.getQueryLimitPart(limit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,13 @@ public Instant getInstant(ResultSet rs, String columnName) throws SQLException {
return Optional.ofNullable(rs.getTimestamp(columnName)).map(Timestamp::toInstant).orElse(null);
}

@Override
public boolean supportsExplicitQueryLimitPart() {
return false;
}

@Override
public String getQueryLimitPart(int limit) {
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ public interface JdbcCustomization {
void setInstant(PreparedStatement p, int index, Instant value) throws SQLException;

Instant getInstant(ResultSet rs, String columnName) throws SQLException;

boolean supportsExplicitQueryLimitPart();

String getQueryLimitPart(int limit);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,13 @@ public Instant getInstant(ResultSet rs, String columnName) throws SQLException {
return Optional.ofNullable(rs.getTimestamp(columnName)).map(Timestamp::toInstant).orElse(null);
}

@Override
public boolean supportsExplicitQueryLimitPart() {
return false;
}

@Override
public String getQueryLimitPart(int limit) {
return "";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (C) Gustav Karlsson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.kagkarlsson.scheduler.jdbc;

public class PostgreSqlJdbcCustomization extends DefaultJdbcCustomization {
@Override
public boolean supportsExplicitQueryLimitPart() {
return true;
}

@Override
public String getQueryLimitPart(int limit) {
return " LIMIT " + limit;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.kagkarlsson.scheduler.jdbc;

import com.github.kagkarlsson.scheduler.EmbeddedPostgresqlExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class PostgreSqlJdbcCustomizationTest {
@RegisterExtension
public EmbeddedPostgresqlExtension postgres = new EmbeddedPostgresqlExtension();

AutodetectJdbcCustomization jdbcCustomization = new AutodetectJdbcCustomization(postgres.getDataSource());

@Test
void test() {
assertTrue(jdbcCustomization.supportsExplicitQueryLimitPart());
Arrays.asList(1, 5, 20, 100).forEach(
it -> assertEquals(jdbcCustomization.getQueryLimitPart(it), " LIMIT " + it)
);
}
}

0 comments on commit 586a124

Please sign in to comment.