{"record":{"id":"1606ad88a549086b","repo":"OrchardCMS/OrchardCore","slug":"the-typeof-t-name-could-not-be-persisted-and-cached-as-it","errorCode":null,"errorMessage":"The '{typeof(T).Name}' could not be persisted and cached as it has been changed by another process.","messagePattern":"The '(.+?)' could not be persisted and cached as it has been changed by another process\\.","errorType":"exception","errorClass":"DocumentStoreCommitException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Data.YesSql/Documents/DocumentStore.cs","lineNumber":75,"sourceCode":"            return (true, document);\n        }\n\n        return (true, await (factoryAsync?.Invoke() ?? Task.FromResult((T)null)) ?? new T());\n    }\n\n    /// <inheritdoc />\n    public async Task UpdateAsync<T>(T document, Func<T, Task> updateCache, bool checkConcurrency = false)\n    {\n        await _session.SaveAsync(document, checkConcurrency);\n\n        AfterCommitSuccess<T>(() =>\n        {\n            return updateCache(document);\n        });\n\n        AfterCommitFailure<T>(exception =>\n        {\n            throw new DocumentStoreCommitException(\n                $\"The '{typeof(T).Name}' could not be persisted and cached as it has been changed by another process.\",\n                exception);\n        });\n    }\n\n    /// <inheritdoc />\n    public async Task CancelAsync()\n    {\n        _canceled = true;\n\n        if (_session is null)\n        {\n            return;\n        }\n\n        await _session.CancelAsync();\n    }\n","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Data.YesSql/Documents/DocumentStore.cs#L57-L93","documentation":"DocumentStore.UpdateAsync wraps the persistence + cache update of a document. If the transaction commits fail because the document was concurrently modified by another process/shell instance (YesSql optimistic concurrency), AfterCommitFailure rethrows as DocumentStoreCommitException with this message. It means the in-memory document version is stale relative to what is stored.","triggerScenarios":"Calling ISession/documentStore UpdateAsync for a document whose stored version was changed by another process between load and commit — the YesSql concurrency check fails during commit.","commonSituations":"Multiple app instances or background jobs writing the same singleton document (e.g., SiteSettings, recipe state) simultaneously; a long-running request holding a document while another request saves it first; load-balanced deployments sharing one database.","solutions":["Reload the latest document and re-apply your changes, then retry UpdateAsync (last-write-wins with a fresh read).","Reduce the write window: load the document as late as possible and save immediately.","For shared singleton documents, serialize writes through a single writer or use distributed locking (e.g., a database-based lock) before updating.","Inspect the inner exception to confirm it is YesSql concurrency (version mismatch) rather than a connection failure."],"exampleFix":"// before\nvar settings = await session.Query<SiteSettings>().FirstOrDefaultAsync();\nsettings.PageSize = 50;\nawait session.SaveAsync(settings);\n// after (retry on stale document)\ntry\n{\n    settings.PageSize = 50;\n    await session.SaveAsync(settings);\n}\ncatch (DocumentStoreCommitException)\n{\n    session = await sessionFactory.CreateScopeAsync();\n    settings = await session.Query<SiteSettings>().FirstOrDefaultAsync();\n    settings.PageSize = 50;\n    await session.SaveAsync(settings);\n}","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"catch (DocumentStoreCommitException ex) when (ex.InnerException is ConcurrencyException) { // reload document and retry up to N times }","preventionTips":["Minimize the time between loading and saving shared documents","Avoid holding a loaded document across long-running operations or user input","Use locking or a single writer for hot singleton documents in multi-instance deployments"],"tags":["database","concurrency","optimistic-locking"],"backgroundTag":"database-write-failed","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}