diff --git a/modules/flowable-engine/src/main/java/org/flowable/engine/impl/cmd/ValidateExecutionRelatedEntityCountCfgCmd.java b/modules/flowable-engine/src/main/java/org/flowable/engine/impl/cmd/ValidateExecutionRelatedEntityCountCfgCmd.java index 9a3d6bbb907..dd8a662f005 100644 --- a/modules/flowable-engine/src/main/java/org/flowable/engine/impl/cmd/ValidateExecutionRelatedEntityCountCfgCmd.java +++ b/modules/flowable-engine/src/main/java/org/flowable/engine/impl/cmd/ValidateExecutionRelatedEntityCountCfgCmd.java @@ -1,9 +1,9 @@ /* 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. @@ -12,6 +12,7 @@ */ package org.flowable.engine.impl.cmd; +import org.flowable.common.engine.api.FlowableException; import org.flowable.common.engine.impl.interceptor.Command; import org.flowable.common.engine.impl.interceptor.CommandContext; import org.flowable.common.engine.impl.persistence.entity.PropertyEntity; @@ -35,21 +36,21 @@ public Void execute(CommandContext commandContext) { /* * If execution related entity counting is on in config | Current property in database : Result - * - * A) true | not there : write new property with value 'true' - * - * B) true | true : all good - * + * + * A) true | not there : write new property with value 'true' + * + * B) true | true : all good + * * C) true | false : the feature was disabled before, but it is enabled now. Old executions will have a * local flag with false. It is now enabled. This is fine, will be handled in logic. Update the property. - * - * D) false | not there: write new property with value 'false' - * - * E) false | true : the feature was disabled before and enabled now. - * To guarantee data consistency, we need to remove the flag from all executions. Update the property. - * + * + * D) false | not there: write new property with value 'false' + * + * E) false | true : the feature was disabled before and enabled now. + * To guarantee data consistency, we need to remove the flag from all executions. Update the property. + * * F) false | false : all good - * + * * In case A and D (not there), the property needs to be written to the db Only in case E something needs to be done explicitly, the others are okay. */ @@ -63,10 +64,41 @@ public Void execute(CommandContext commandContext) { // 'not there' case in the table above: easy, simply insert the value - PropertyEntity newPropertyEntity = propertyEntityManager.create(); - newPropertyEntity.setName(PROPERTY_EXECUTION_RELATED_ENTITY_COUNT); - newPropertyEntity.setValue(Boolean.toString(configProperty)); - propertyEntityManager.insert(newPropertyEntity); + try { + PropertyEntity newPropertyEntity = propertyEntityManager.create(); + newPropertyEntity.setName(PROPERTY_EXECUTION_RELATED_ENTITY_COUNT); + newPropertyEntity.setValue(Boolean.toString(configProperty)); + propertyEntityManager.insert(newPropertyEntity); + + if (LOGGER.isInfoEnabled()) { + LOGGER.info("Created new property '{}' with value '{}'", PROPERTY_EXECUTION_RELATED_ENTITY_COUNT, configProperty); + } + + } catch (Exception e) { + // Race condition: in a distributed scenario, another node may have already inserted + // the property between our null check and insert. Re-read to handle this gracefully. + if (LOGGER.isInfoEnabled()) { + LOGGER.info("Insert for property '{}' failed (likely race condition during distributed startup), re-reading: {}", + PROPERTY_EXECUTION_RELATED_ENTITY_COUNT, e.getMessage()); + } + + propertyEntity = propertyEntityManager.findById(PROPERTY_EXECUTION_RELATED_ENTITY_COUNT); + if (propertyEntity == null) { + throw new FlowableException("Failed to read or insert property '" + PROPERTY_EXECUTION_RELATED_ENTITY_COUNT + + "' during engine startup. This may indicate a database issue.", e); + } + + // Another node inserted it; verify consistency + boolean propertyValue = Boolean.parseBoolean(propertyEntity.getValue().toLowerCase()); + if (configProperty != propertyValue) { + if (LOGGER.isInfoEnabled()) { + LOGGER.info("Configuration mismatch after race condition: config={}, db={}. Updating to match config.", + configProperty, propertyValue); + } + propertyEntity.setValue(Boolean.toString(configProperty)); + propertyEntityManager.update(propertyEntity); + } + } } else {