make sure the CLS entity changes are cleared after roll backed transaction, #1736

This commit is contained in:
zadam 2021-03-12 23:48:14 +01:00
parent 9fd26a9b9f
commit 88d04772c4
3 changed files with 19 additions and 10 deletions

View file

@ -44,10 +44,14 @@ function isEntityEventsDisabled() {
return !!namespace.get('disableEntityEvents');
}
function clearEntityChanges() {
namespace.set('entityChanges', []);
}
function getAndClearEntityChanges() {
const entityChanges = namespace.get('entityChanges') || [];
namespace.set('entityChanges', []);
clearEntityChanges();
return entityChanges;
}
@ -92,6 +96,7 @@ module.exports = {
disableEntityEvents,
isEntityEventsDisabled,
reset,
clearEntityChanges,
getAndClearEntityChanges,
addEntityChange,
getEntityFromCache,

View file

@ -3,6 +3,7 @@
const log = require('./log');
const Database = require('better-sqlite3');
const dataDir = require('./data_dir');
const cls = require('./cls');
const dbConnection = new Database(dataDir.DOCUMENT_PATH);
dbConnection.pragma('journal_mode = WAL');
@ -229,13 +230,20 @@ function wrap(query, func) {
}
function transactional(func) {
const ret = dbConnection.transaction(func).deferred();
try {
const ret = dbConnection.transaction(func).deferred();
if (!dbConnection.inTransaction) { // i.e. transaction was really committed (and not just savepoint released)
require('./ws.js').sendTransactionSyncsToAllClients();
if (!dbConnection.inTransaction) { // i.e. transaction was really committed (and not just savepoint released)
require('./ws.js').sendTransactionSyncsToAllClients();
}
return ret;
}
catch (e) {
cls.clearEntityChanges();
return ret;
throw e;
}
}
function fillNoteIdList(noteIds, truncate = true) {

View file

@ -106,8 +106,6 @@ function sendPing(client, entityChanges = []) {
}
}
const stats = require('./sync').stats;
sendMessage(client, {
type: 'sync',
data: entityChanges
@ -118,9 +116,7 @@ function sendTransactionSyncsToAllClients() {
if (webSocketServer) {
const entityChanges = cls.getAndClearEntityChanges();
webSocketServer.clients.forEach(function each(client) {
sendPing(client, entityChanges);
});
webSocketServer.clients.forEach(client => sendPing(client, entityChanges));
}
}