From 4aef0ad459cf0ccfa4c01969d3d3bf96320fcfd2 Mon Sep 17 00:00:00 2001 From: rbygrave Date: Wed, 28 Jul 2021 17:23:37 +1200 Subject: [PATCH] #2275 - Change Query to Query on ExtendedServer exists() method --- .../main/java/io/ebean/ExtendedServer.java | 2 +- .../server/core/DefaultServer.java | 8 ++--- .../ebeaninternal/api/TDSpiEbeanServer.java | 2 +- .../query/cancel/SqlQueryCancelTest.java | 34 +++++++++---------- 4 files changed, 22 insertions(+), 24 deletions(-) diff --git a/ebean-api/src/main/java/io/ebean/ExtendedServer.java b/ebean-api/src/main/java/io/ebean/ExtendedServer.java index 9a7e62e0c..59b1c678e 100644 --- a/ebean-api/src/main/java/io/ebean/ExtendedServer.java +++ b/ebean-api/src/main/java/io/ebean/ExtendedServer.java @@ -67,7 +67,7 @@ public interface ExtendedServer { * * @return True if the query finds a matching row in the database */ - boolean exists(Query ormQuery, Transaction transaction); + boolean exists(Query ormQuery, Transaction transaction); /** * Return the number of 'top level' or 'root' entities this query should return. diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/core/DefaultServer.java b/ebean-core/src/main/java/io/ebeaninternal/server/core/DefaultServer.java index 87580f90b..4aa90ad13 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/core/DefaultServer.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/core/DefaultServer.java @@ -1283,14 +1283,12 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer { } @Override - public boolean exists(Query ormQuery, Transaction transaction) { - Query ormQueryCopy = ormQuery.copy(); - ormQueryCopy.setMaxRows(1); + public boolean exists(Query ormQuery, Transaction transaction) { + Query ormQueryCopy = ormQuery.copy().setMaxRows(1); SpiOrmQueryRequest request = createQueryRequest(Type.ID_LIST, ormQueryCopy, transaction); try { request.initTransIfRequired(); - List ids = request.findIds(); - return !ids.isEmpty(); + return !request.findIds().isEmpty(); } finally { request.endTransIfRequired(); } diff --git a/ebean-core/src/test/java/io/ebeaninternal/api/TDSpiEbeanServer.java b/ebean-core/src/test/java/io/ebeaninternal/api/TDSpiEbeanServer.java index f0f0c0779..b09a0f54e 100644 --- a/ebean-core/src/test/java/io/ebeaninternal/api/TDSpiEbeanServer.java +++ b/ebean-core/src/test/java/io/ebeaninternal/api/TDSpiEbeanServer.java @@ -650,7 +650,7 @@ public class TDSpiEbeanServer implements SpiEbeanServer { } @Override - public boolean exists(Query ormQuery, Transaction transaction) { + public boolean exists(Query ormQuery, Transaction transaction) { return false; } diff --git a/ebean-core/src/test/java/org/tests/query/cancel/SqlQueryCancelTest.java b/ebean-core/src/test/java/org/tests/query/cancel/SqlQueryCancelTest.java index d3dda7443..9fc3d6b57 100644 --- a/ebean-core/src/test/java/org/tests/query/cancel/SqlQueryCancelTest.java +++ b/ebean-core/src/test/java/org/tests/query/cancel/SqlQueryCancelTest.java @@ -30,18 +30,18 @@ import io.ebean.annotation.Platform; * Tests, if all kind of queries are cancelable. There are two ways how to * cancel a query:
* At begin: - * + * *
  * query = DB.find(...)
  * query.cancel();
  * query.findList();
  * 
- * + * * The query was caneled before executing. In this case we do hit the DB driver *
*
* During run: - * + * *
  * // Thread 1:              Thread 2
  * query = DB.find(...)
@@ -50,26 +50,26 @@ import io.ebean.annotation.Platform;
  *     ...finding            query.cancel();
  *      ...JDBC-Exception
  * 
- * + * * The test tries to simulate a slow query by installing the * {@link SlowDownEBasic} 'SELECT' trigger. The trigger can be configured to * wait 3 * timing ms and a second thread will cancel the query in * timing ms. - * + * * in this case, we expect a JDBC exception from the driver.
*
* NOTE:
* H2 checks the cancel flag in org.h2.command.Prepared::setCurrentRowNumber * only every 128th row. So we need at least 128 models and we cannot check * queries like findCount or findOne, because they only return one row. - * + * * @author Roland Praml, FOCONIS AG * */ public class SqlQueryCancelTest extends BaseTestCase { - private int timing = 10; - + private final int timing = 20; + @BeforeClass public static void setupTestData() throws SQLException { for (int i = 0; i < 128; i++) { @@ -98,10 +98,10 @@ public class SqlQueryCancelTest extends BaseTestCase { doCancelSqlDuringRun(q -> q.findEachWhile(e -> true)); } - + @Test public void cancelOrmQueryAtBegin() throws SQLException { - doCancelOrmAtBegin(Query::findCount); + doCancelOrmAtBegin(Query::findCount); doCancelOrmAtBegin(Query::findFutureCount); // We cannot test 'findCount' due H2 restrictions doCancelOrmAtBegin(Query::findFutureIds); @@ -206,7 +206,7 @@ public class SqlQueryCancelTest extends BaseTestCase { .isInstanceOf(PersistenceException.class) .hasMessageContaining("Query was cancelled"); } - + @Test public void cancelSqlDtoQueryAtBegin() throws SQLException { @@ -290,7 +290,7 @@ public class SqlQueryCancelTest extends BaseTestCase { private void doCancelOrmFutureDuringRun(Function, Future> test) throws SQLException, InterruptedException, ExecutionException { Query warmup = DB.find(EBasic.class); test.apply(warmup).get(); - + Query query = DB.find(EBasic.class); executeDelayed(query::cancel); assertThatThrownBy(() -> { @@ -311,18 +311,18 @@ public class SqlQueryCancelTest extends BaseTestCase { .isInstanceOf(PersistenceException.class) .hasMessageContaining("Query was cancelled"); } - + private void doCancelOrmDtoDuringRun(Consumer> test) throws SQLException { DtoQuery warmup = DB.find(EBasic.class).select("id,status").asDto(EBasicDto.class); test.accept(warmup); - + DtoQuery query = DB.find(EBasic.class).select("id,status").asDto(EBasicDto.class); executeDelayed(query::cancel); assertThatThrownBy(() -> test.accept(query)) .isInstanceOf(PersistenceException.class) .hasCauseInstanceOf(org.h2.jdbc.JdbcSQLTimeoutException.class); } - + private void doCancelSqlDtoAtBegin(Consumer> test) throws SQLException { DtoQuery query = DB.findDto(EBasicDto.class, "select id, status from e_basic"); query.cancel(); @@ -334,14 +334,14 @@ public class SqlQueryCancelTest extends BaseTestCase { private void doCancelSqlDtoDuringRun(Consumer> test) throws SQLException { DtoQuery warmup = DB.findDto(EBasicDto.class, "select id, status from e_basic"); test.accept(warmup); - + DtoQuery query = DB.findDto(EBasicDto.class, "select id, status from e_basic"); executeDelayed(query::cancel); assertThatThrownBy(() -> test.accept(query)) .isInstanceOf(PersistenceException.class) .hasCauseInstanceOf(org.h2.jdbc.JdbcSQLTimeoutException.class); } - + private void executeDelayed(Runnable r) throws SQLException { // We modify the DB here. Otherwise we may hit an internal H2 cache, if the // same query is performed. Queries from the cache cannot be canceled.