From b63445e9c97199994ab9611ef8a7eb3f400526d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20P=C3=B6hler?= Date: Thu, 15 Jun 2023 12:04:33 +0200 Subject: [PATCH] FIX: Put the delete statements into the correct flush-queue, so they do not conflict with the following inserts --- .../main/java/io/ebeaninternal/server/core/Persister.java | 2 +- .../java/io/ebeaninternal/server/persist/BatchControl.java | 7 +++++-- .../io/ebeaninternal/server/persist/DefaultPersister.java | 6 +++--- .../java/io/ebeaninternal/server/persist/SaveManyBase.java | 2 +- .../io/ebeaninternal/server/persist/SaveManyBeans.java | 4 ++-- .../server/persist/SaveManyElementCollection.java | 2 +- .../server/persist/SaveManyElementCollectionMap.java | 2 +- 7 files changed, 14 insertions(+), 11 deletions(-) diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/core/Persister.java b/ebean-core/src/main/java/io/ebeaninternal/server/core/Persister.java index 587ce06250..46927f1122 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/core/Persister.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/core/Persister.java @@ -101,7 +101,7 @@ public interface Persister { /** * Execute or queue the update. */ - void executeOrQueue(SpiSqlUpdate update, SpiTransaction t, boolean queue); + void executeOrQueue(SpiSqlUpdate update, SpiTransaction t, boolean queue, int queuePosition); /** * Queue the SqlUpdate for execution with position 0, 1 or 2 defining diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/persist/BatchControl.java b/ebean-core/src/main/java/io/ebeaninternal/server/persist/BatchControl.java index 10cd8f5197..009a6781b6 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/persist/BatchControl.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/persist/BatchControl.java @@ -83,6 +83,9 @@ public final class BatchControl { private final Queue[] queues = new Queue[3]; + static final int DELETE_QUEUE = 1; + static final int INSERT_QUEUE = 2; + /** * Create for a given transaction, PersistExecute, default size and getGeneratedKeys. */ @@ -267,8 +270,8 @@ public void clear() { private void flushBuffer(boolean reset) throws BatchedSqlException { flushQueue(queues[0]); flushInternal(reset); - flushQueue(queues[1]); - flushQueue(queues[2]); + flushQueue(queues[DELETE_QUEUE]); + flushQueue(queues[INSERT_QUEUE]); } private void flushQueue(Queue queue) throws BatchedSqlException { diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/persist/DefaultPersister.java b/ebean-core/src/main/java/io/ebeaninternal/server/persist/DefaultPersister.java index 7f1dcc1fc2..4d3efd92e8 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/persist/DefaultPersister.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/persist/DefaultPersister.java @@ -106,9 +106,9 @@ public int[] executeBatch(SpiSqlUpdate sqlUpdate, SpiTransaction transaction) { } @Override - public void executeOrQueue(SpiSqlUpdate update, SpiTransaction t, boolean queue) { + public void executeOrQueue(SpiSqlUpdate update, SpiTransaction t, boolean queue, int queuePosition) { if (queue) { - addToFlushQueue(update, t, 2); + addToFlushQueue(update, t, queuePosition); } else { executeSqlUpdate(update, t); } @@ -907,7 +907,7 @@ private SaveManyBase saveManyRequest(boolean insertedParent, BeanPropertyAssocMa void deleteManyIntersection(EntityBean bean, BeanPropertyAssocMany many, SpiTransaction t, boolean publish, boolean queue) { SpiSqlUpdate sqlDelete = deleteAllIntersection(bean, many, publish); if (queue) { - addToFlushQueue(sqlDelete, t, 1); + addToFlushQueue(sqlDelete, t, BatchControl.DELETE_QUEUE); } else { executeSqlUpdate(sqlDelete, t); } diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/persist/SaveManyBase.java b/ebean-core/src/main/java/io/ebeaninternal/server/persist/SaveManyBase.java index b01d685a39..88c7ae6a71 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/persist/SaveManyBase.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/persist/SaveManyBase.java @@ -48,7 +48,7 @@ abstract class SaveManyBase implements SaveMany { final void preElementCollectionUpdate() { if (!insertedParent) { request.preElementCollectionUpdate(); - persister.addToFlushQueue(many.deleteByParentId(request.beanId(), null), transaction, 1); + persister.addToFlushQueue(many.deleteByParentId(request.beanId(), null), transaction, BatchControl.DELETE_QUEUE); } } diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/persist/SaveManyBeans.java b/ebean-core/src/main/java/io/ebeaninternal/server/persist/SaveManyBeans.java index 338099eedf..68c446fed9 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/persist/SaveManyBeans.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/persist/SaveManyBeans.java @@ -294,7 +294,7 @@ private void saveAssocManyIntersection(boolean queue) { // build a intersection row for 'delete' IntersectionRow intRow = many.buildManyToManyMapBean(parentBean, otherDelete, publish); SpiSqlUpdate sqlDelete = intRow.createDelete(server, DeleteMode.HARD); - persister.executeOrQueue(sqlDelete, transaction, queue); + persister.executeOrQueue(sqlDelete, transaction, queue, BatchControl.DELETE_QUEUE); } } if (additions != null && !additions.isEmpty()) { @@ -314,7 +314,7 @@ private void saveAssocManyIntersection(boolean queue) { // build a intersection row for 'insert' IntersectionRow intRow = many.buildManyToManyMapBean(parentBean, otherBean, publish); SpiSqlUpdate sqlInsert = intRow.createInsert(server); - persister.executeOrQueue(sqlInsert, transaction, queue); + persister.executeOrQueue(sqlInsert, transaction, queue, BatchControl.INSERT_QUEUE); } } } diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/persist/SaveManyElementCollection.java b/ebean-core/src/main/java/io/ebeaninternal/server/persist/SaveManyElementCollection.java index 874505b835..87abbc3f37 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/persist/SaveManyElementCollection.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/persist/SaveManyElementCollection.java @@ -48,7 +48,7 @@ private void saveCollection() { final SpiSqlUpdate sqlInsert = proto.copy(); sqlInsert.setParameter(parentId); many.bindElementValue(sqlInsert, value); - persister.addToFlushQueue(sqlInsert, transaction, 2); + persister.addToFlushQueue(sqlInsert, transaction, BatchControl.INSERT_QUEUE); } resetModifyState(); postElementCollectionUpdate(); diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/persist/SaveManyElementCollectionMap.java b/ebean-core/src/main/java/io/ebeaninternal/server/persist/SaveManyElementCollectionMap.java index f57a44d3d4..441c992d3c 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/persist/SaveManyElementCollectionMap.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/persist/SaveManyElementCollectionMap.java @@ -51,7 +51,7 @@ private void saveCollection() { sqlInsert.setParameter(parentId); sqlInsert.setParameter(entry.getKey()); many.bindElementValue(sqlInsert, entry.getValue()); - persister.addToFlushQueue(sqlInsert, transaction, 2); + persister.addToFlushQueue(sqlInsert, transaction, BatchControl.INSERT_QUEUE); } resetModifyState(); postElementCollectionUpdate();