Merge pull request #2112 from ebean-orm/FOCONIS-bug-ebean/sql_update_batch_flush

2110 - Fix and test for executeBatch() on SqlUpdate
This commit is contained in:
Rob Bygrave
2020-11-25 20:40:02 +13:00
committed by GitHub
8 changed files with 196 additions and 100 deletions
@@ -1,6 +1,7 @@
package org.tests.cascade;
import io.ebean.BaseTestCase;
import io.ebean.DB;
import io.ebean.Ebean;
import io.ebean.Transaction;
import org.ebeantest.LoggedSqlCollector;
@@ -49,6 +50,12 @@ public class TestMultiCascadeBatch extends BaseTestCase {
final List<String> sql = LoggedSqlCollector.stop();
final List<Site> list = DB.find(Site.class).where()
.idIn(grandparent.getId(), parent.getId(), child.getId())
.findList();
assertThat(list).hasSize(3);
assertThat(sql).hasSize(5);
assertSql(sql.get(0)).contains("insert into site (id, name");
assertSql(sql.get(1)).contains("insert into site (id, name");
@@ -0,0 +1,67 @@
package org.tests.update;
import io.ebean.BaseTestCase;
import io.ebean.DB;
import io.ebean.SqlUpdate;
import io.ebean.Transaction;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* Testclass that ensures the correct behaviour of SqlUpdate in combination with batching.
*
* @author Jonas P&ouml;hler, FOCONIS AG
*/
public class TestSqlUpdateBatch extends BaseTestCase {
/**
* This test ensures the correct behaviour of update batching in the {@code BatchedPstmtHolder} after a flush. Two batch updates
* are executed with each a batchsize of 21, which is the default limit for automatic flushing on {@code addBatch()}. After that
* the statements are each executed with {@code executeBatch()}. The desired behaviour is, that each execution succeeds although
* technically the batch was already flushed.
*/
@Test
public void testTwoParallelBatches() {
try (Transaction txn = DB.beginTransaction()) {
// Dummy updates, that effectively do nothing, but ebean doesn't need to know this.
final SqlUpdate update = DB.sqlUpdate("update uuone set name = ? where 0=1");
final SqlUpdate delete = DB.sqlUpdate("delete from uuone where ?=-1");
// txn.setBatchSize(40);
for (int i = 0; i <= 20; i++) {
update
.setParameter(1, String.valueOf(i))
.addBatch();
delete
.setParameter(1, String.valueOf(i))
.addBatch();
}
delete.executeBatch();
update.executeBatch();
txn.commit();
}
}
/**
* This test checks that for a batch update a correct array with update counts is returned. If a update with 40 entries is
* executed, it is expected, that {@code executeBatch()} returns a array with 40 elements each containing the update count for
* the given parameters.
*/
@Test
public void testBatchReturnArrayLength() {
try (Transaction txn = DB.beginTransaction()) {
txn.setBatchSize(100); // something bigger than 40
// Dummy update, that effectively does nothing, but ebean doesn't need to know this.
final SqlUpdate update = DB.sqlUpdate("update uuone set name = ? where 0=1");
for (int i = 0; i < 40; i++) {
update
.setParameter(1, String.valueOf(i))
.addBatch();
}
assertEquals(40, update.executeBatch().length);
}
}
}