Merge pull request #16364 from strapi/fix/mysql-deadlock-performance-issues

This commit is contained in:
Marc 2023-05-02 13:49:20 +02:00 committed by GitHub
commit e71fa3ec1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 8 deletions

View File

@ -51,6 +51,10 @@ class Database {
return this.entityManager.getRepository(uid);
}
inTransaction() {
return !!transactionCtx.get();
}
async transaction(cb) {
const notNestedTransaction = !transactionCtx.get();
const trx = notNestedTransaction ? await this.connection.transaction() : transactionCtx.get();

View File

@ -49,7 +49,7 @@ const createComponents = async (uid, data) => {
const components = await mapAsync(
componentValue,
(value) => createComponent(componentUID, value),
{ concurrency: isDialectMySQL() ? 1 : Infinity }
{ concurrency: isDialectMySQL() && !strapi.db.inTransaction() ? 1 : Infinity }
);
componentBody[attributeName] = components.map(({ id }) => {
@ -97,7 +97,7 @@ const createComponents = async (uid, data) => {
componentBody[attributeName] = await mapAsync(
dynamiczoneValues,
createDynamicZoneComponents,
{ concurrency: isDialectMySQL() ? 1 : Infinity }
{ concurrency: isDialectMySQL() && !strapi.db.inTransaction() ? 1 : Infinity }
);
continue;
@ -151,7 +151,7 @@ const updateComponents = async (uid, entityToUpdate, data) => {
const components = await mapAsync(
componentValue,
(value) => updateOrCreateComponent(componentUID, value),
{ concurrency: isDialectMySQL() ? 1 : Infinity }
{ concurrency: isDialectMySQL() && !strapi.db.inTransaction() ? 1 : Infinity }
);
componentBody[attributeName] = components.filter(_.negate(_.isNil)).map(({ id }) => {
@ -200,7 +200,7 @@ const updateComponents = async (uid, entityToUpdate, data) => {
},
};
},
{ concurrency: isDialectMySQL() ? 1 : Infinity }
{ concurrency: isDialectMySQL() && !strapi.db.inTransaction() ? 1 : Infinity }
);
continue;
@ -305,7 +305,7 @@ const deleteComponents = async (uid, entityToDelete, { loadComponents = true } =
const { component: componentUID } = attribute;
// MySQL/MariaDB can cause deadlocks here if concurrency higher than 1
await mapAsync(_.castArray(value), (subValue) => deleteComponent(componentUID, subValue), {
concurrency: isDialectMySQL() ? 1 : Infinity,
concurrency: isDialectMySQL() && !strapi.db.inTransaction() ? 1 : Infinity,
});
} else {
// delete dynamic zone components
@ -313,7 +313,7 @@ const deleteComponents = async (uid, entityToDelete, { loadComponents = true } =
await mapAsync(
_.castArray(value),
(subValue) => deleteComponent(subValue.__component, subValue),
{ concurrency: isDialectMySQL() ? 1 : Infinity }
{ concurrency: isDialectMySQL() && !strapi.db.inTransaction() ? 1 : Infinity }
);
}

View File

@ -41,8 +41,9 @@ const syncLocalizations = async (entry, { model }) => {
};
// MySQL/MariaDB can cause deadlocks here if concurrency higher than 1
// TODO: use a transaction to avoid deadlocks
await mapAsync(entry.localizations, (localization) => updateLocalization(localization.id), {
concurrency: isDialectMySQL() ? 1 : Infinity,
concurrency: isDialectMySQL() && !strapi.db.inTransaction() ? 1 : Infinity,
});
}
};
@ -68,8 +69,9 @@ const syncNonLocalizedAttributes = async (entry, { model }) => {
};
// MySQL/MariaDB can cause deadlocks here if concurrency higher than 1
// TODO: use a transaction to avoid deadlocks
await mapAsync(entry.localizations, (localization) => updateLocalization(localization.id), {
concurrency: isDialectMySQL() ? 1 : Infinity,
concurrency: isDialectMySQL() && !strapi.db.inTransaction() ? 1 : Infinity,
});
}
};