better transaction handling with rollback on exception

This commit is contained in:
azivner
2017-10-29 18:50:28 -04:00
parent afadd6ec06
commit de3d1b3e39
8 changed files with 145 additions and 150 deletions

View File

@@ -4,6 +4,7 @@ module.exports = {
UPDATE_CONTENT: 'CONTENT',
UPDATE_TITLE: 'TITLE',
CHANGE_POSITION: 'POSITION',
CHANGE_EXPANDED: 'EXPANDED',
CREATE_NOTE: 'CREATE',
DELETE_NOTE: 'DELETE',
CHANGE_PARENT: 'PARENT',

View File

@@ -55,15 +55,13 @@ async function changePassword(currentPassword, newPassword, req = null) {
const newEncryptedDataKey = encrypt(decryptedDataKey);
await sql.beginTransaction();
await sql.doInTransaction(async () => {
await sql.setOption('encrypted_data_key', newEncryptedDataKey);
await sql.setOption('encrypted_data_key', newEncryptedDataKey);
await sql.setOption('password_verification_hash', newPasswordVerificationKey);
await sql.setOption('password_verification_hash', newPasswordVerificationKey);
await sql.addAudit(audit_category.CHANGE_PASSWORD, req);
await sql.commit();
await sql.addAudit(audit_category.CHANGE_PASSWORD, req);
});
return {
'success': true,

View File

@@ -42,13 +42,11 @@ async function migrate() {
try {
log.info("Attempting migration to version " + mig.dbVersion + " with script: " + migrationSql);
await sql.beginTransaction();
await sql.doInTransaction(async () => {
await sql.executeScript(migrationSql);
await sql.executeScript(migrationSql);
await sql.setOption("db_version", mig.dbVersion);
await sql.commit();
await sql.setOption("db_version", mig.dbVersion);
});
log.info("Migration to version " + mig.dbVersion + " has been successful.");

View File

@@ -109,6 +109,21 @@ async function deleteRecentAudits(category, req, noteId) {
[category, browserId, noteId, deleteCutoff])
}
async function doInTransaction(func) {
try {
await beginTransaction();
await func();
await commit();
}
catch (e) {
log.error("Error executing transaction, executing rollback. Inner exception: " + e.stack);
await rollback();
}
}
module.exports = {
insert,
getSingleResult,
@@ -118,10 +133,8 @@ module.exports = {
executeScript,
getOption,
setOption,
beginTransaction,
commit,
rollback,
addAudit,
deleteRecentAudits,
remove
remove,
doInTransaction
};

View File

@@ -34,41 +34,37 @@ async function pullSync(cookieJar, syncLog) {
}
try {
await sql.beginTransaction();
await sql.doInTransaction(async () => {
await putChanged(resp, syncLog);
await putChanged(resp, syncLog);
for (const noteId of resp.notes) {
let note;
for (const noteId of resp.notes) {
let note;
try {
note = await rp({
uri: SYNC_SERVER + "/api/sync/note/" + noteId + "/" + lastSyncedPull,
headers: {
auth: 'sync'
},
json: true,
jar: cookieJar
});
}
catch (e) {
throw new Error("Can't pull note " + noteId + ", inner exception: " + e.stack);
}
try {
note = await rp({
uri: SYNC_SERVER + "/api/sync/note/" + noteId + "/" + lastSyncedPull,
headers: {
auth: 'sync'
},
json: true,
jar: cookieJar
});
}
catch (e) {
throw new Error("Can't pull note " + noteId + ", inner exception: " + e.stack);
await putNote(note, syncLog);
}
await putNote(note, syncLog);
}
if (resp.notes.length > 0) {
await sql.addAudit(audit_category.SYNC);
}
if (resp.notes.length > 0) {
await sql.addAudit(audit_category.SYNC);
}
await sql.setOption('last_synced_pull', resp.syncTimestamp);
await sql.commit();
await sql.setOption('last_synced_pull', resp.syncTimestamp);
});
}
catch (e) {
await sql.rollback();
throw e;
}
}