Refactor detection of deferred persist relationship (circular imported ids)

This commit is contained in:
rob bygrave
2019-10-08 21:49:22 +13:00
parent 5e7dda1ec8
commit 6621c5dfbb
13 changed files with 224 additions and 19 deletions
@@ -283,10 +283,12 @@ public final class PersistRequestBean<T> extends PersistRequest implements BeanP
* Check for batch escalation on cascade.
*/
public void checkBatchEscalationOnCascade() {
if (transaction.checkBatchEscalationOnCascade(this)) {
// we escalated to use batch mode so flush when done
// but if createdTransaction then commit will flush it
batchOnCascadeSet = !createdTransaction;
if (type != Type.INSERT || beanDescriptor.isCascadeBatchEscalateSupported()) {
if (transaction.checkBatchEscalationOnCascade(this)) {
// we escalated to use batch mode so flush when done
// but if createdTransaction then commit will flush it
batchOnCascadeSet = !createdTransaction;
}
}
persistCascade = transaction.isPersistCascade();
}
@@ -140,6 +140,7 @@ public class BeanDescriptor<T> implements BeanType<T>, STreeType {
private final short profileBeanId;
private final boolean multiValueSupported;
private boolean cascadeBatchEscalateSupported;
public enum EntityType {
ORM, EMBEDDED, VIEW, SQL, DOC
@@ -752,6 +753,24 @@ public class BeanDescriptor<T> implements BeanType<T>, STreeType {
}
}
private boolean hasCircularImportedId() {
for (BeanPropertyAssocOne<?> assocOne : propertiesOneImportedSave) {
if (assocOne.hasCircularImportedId(this)) {
return true;
}
}
return false;
}
boolean hasCircularImportedIdTo(BeanDescriptor sourceDesc) {
for (BeanPropertyAssocOne<?> assocOne : propertiesOneImportedSave) {
if (assocOne.getTargetDescriptor() == sourceDesc) {
return true;
}
}
return false;
}
void registerColumn(String dbColumn, String path) {
String key = dbColumn.toLowerCase();
// check for clash with imported OneToOne PK
@@ -812,6 +831,7 @@ public class BeanDescriptor<T> implements BeanType<T>, STreeType {
*/
@SuppressWarnings("unchecked")
void initialiseDocMapping() {
cascadeBatchEscalateSupported = supportCascadeBatch();
for (BeanPropertyAssocMany<?> many : propertiesMany) {
many.initialisePostTarget();
}
@@ -826,6 +846,18 @@ public class BeanDescriptor<T> implements BeanType<T>, STreeType {
cacheHelp.deriveNotifyFlags();
}
private boolean supportCascadeBatch() {
return idType == IdType.IDENTITY || !hasCircularImportedId();
}
/**
* Return false if JDBC batch can't be implicitly escalated to.
* This happens when we have circular import id situation (need to defer setting identity value).
*/
public boolean isCascadeBatchEscalateSupported() {
return cascadeBatchEscalateSupported;
}
void initInheritInfo() {
if (inheritInfo != null) {
// need to check every BeanDescriptor in the inheritance hierarchy
@@ -807,4 +807,8 @@ public class BeanPropertyAssocOne<T> extends BeanPropertyAssoc<T> implements STr
}
}
}
public boolean hasCircularImportedId(BeanDescriptor<?> sourceDesc) {
return targetDescriptor.hasCircularImportedIdTo(sourceDesc);
}
}
@@ -643,6 +643,7 @@ class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes {
// implicit transaction, no gain by batching where depth <= 0
return false;
}
//
return batchMode;
}