Skip to content

Commit

Permalink
Avoid use of UPDATE ... RETURNING ... on MariaDB
Browse files Browse the repository at this point in the history
  • Loading branch information
hylkevds committed Nov 16, 2024
1 parent 9fc17d3 commit 4b98b30
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ FROST-Server version 2.5 and higher requires Java 21. This is because some libra
* Fixed the resolving of references in the URL of json batch requests.
* Fixed differences in HTTP response code between GET and HEAD requests.
* Fixed json batch requests without atomicity group all running in the same group.
* Avoid use of UPDATE ... RETURNING ... on MariaDB.


## Release version 2.4.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,7 @@ public interface JooqPersistenceManager extends LiquibaseUser, PersistenceManage
*/
boolean useClientSuppliedId(Entity entity) throws IncompleteEntityException;

public default boolean hasUpdateReturning() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -880,4 +880,10 @@ public boolean doUpgrades(Writer out) throws UpgradeFailedException, IOException
.build();
return doUpgrades(LIQUIBASE_CHANGELOG_FILENAME, props, out);
}

@Override
public boolean hasUpdateReturning() {
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import org.jooq.Record;
import org.jooq.Table;
import org.jooq.TableField;
import org.jooq.exception.DataAccessException;
import org.jooq.exception.TooManyRowsException;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
Expand Down Expand Up @@ -449,16 +450,7 @@ public EntityChangedMessage updateInDatabase(JooqPersistenceManager pm, Entity e
final Set<Field> selectFields = QueryState.propertiesToFields(thisTable, propertyFields);
Record result = null;
if (!updateFields.isEmpty()) {
try {
result = dslContext.update(thisTable)
.set(updateFields)
.where(where)
.returningResult(selectFields)
.fetchOne();
} catch (TooManyRowsException e) {
LOGGER.error("Updating {} {} caused too many rows to change (more than one)!", entityType, entityId);
throw new IllegalStateException(CHANGED_MULTIPLE_ROWS, e);
}
result = executeUpdate(pm, dslContext, thisTable, updateFields, where, selectFields);
}

for (NavigationPropertyMain<EntitySet> np : entityType.getNavigationSets()) {
Expand All @@ -483,6 +475,34 @@ public EntityChangedMessage updateInDatabase(JooqPersistenceManager pm, Entity e
return message;
}

public Record executeUpdate(JooqPersistenceManager pm, DSLContext dslContext, T thisTable, Map<Field, Object> updateFields, Condition where, Set<Field> selectFields) throws DataAccessException, IllegalStateException {
try {
if (pm.hasUpdateReturning()) {
return dslContext.update(thisTable)
.set(updateFields)
.where(where)
.returningResult(selectFields)
.fetchOne();
} else {
int execute = dslContext.update(thisTable)
.set(updateFields)
.where(where)
.execute();
if (execute != 1) {
LOGGER.error("Updating {} with WHERE {} caused too many rows to change (more than one)!", getName(), where);
throw new IllegalStateException(CHANGED_MULTIPLE_ROWS);
}
return dslContext.select(selectFields)
.from(thisTable)
.where(where)
.fetchOne();
}
} catch (TooManyRowsException e) {
LOGGER.error("Updating {} with WHERE {} caused too many rows to change (more than one)!", getName(), where);
throw new IllegalStateException(CHANGED_MULTIPLE_ROWS, e);
}
}

@Override
public void delete(JooqPersistenceManager pm, PkValue entityId) throws NoSuchEntityException {
for (SortingWrapper<Double, HookPreDelete> hookWrapper : hooksPreDelete) {
Expand Down

0 comments on commit 4b98b30

Please sign in to comment.