Skip to content

Build your personal knowledge base with Trilium Notes

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

zadam88d04772c48a
diff --git a/src/services/cls.js b/src/services/cls.js
index e4d238a2e1..7754da700c 100644
--- a/src/services/cls.js
+++ b/src/services/cls.js
@@ -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,
diff --git a/src/services/sql.js b/src/services/sql.js
index 1716011d4a..5ba56f8cce 100644
--- a/src/services/sql.js
+++ b/src/services/sql.js
@@ -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) {
diff --git a/src/services/ws.js b/src/services/ws.js
index 35d50a6f3e..a4d1b05ff4 100644
--- a/src/services/ws.js
+++ b/src/services/ws.js
@@ -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));
}
}