improced commit-rollback behaviour on multi-step transactions
Build Docker Image / Docker-Build (push) Successful in 2m56s
Build Docker Image / Clean-Registry (push) Successful in 8s

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2026-07-20 00:22:06 +02:00
parent 8d93dbfc11
commit d9f28e3b6c
7 changed files with 65 additions and 10 deletions
@@ -124,13 +124,16 @@ public class SqliteDb extends BaseDb implements AccountDb {
db.setAutoCommit(false); db.setAutoCommit(false);
Query.delete().from(TABLE_TAGS_TRANSACTIONS).where(TRANSACTION_ID,equal(transaction.id())).execute(db); Query.delete().from(TABLE_TAGS_TRANSACTIONS).where(TRANSACTION_ID,equal(transaction.id())).execute(db);
Query.delete().from(TABLE_TRANSACTIONS).where(ID,equal(transaction.id())).execute(db); Query.delete().from(TABLE_TRANSACTIONS).where(ID,equal(transaction.id())).execute(db);
db.setAutoCommit(true);
return transaction; return transaction;
} catch (SQLException e){ } catch (SQLException e){
try { try {
db.rollback(); db.rollback();
} catch (SQLException ignored){}; } catch (SQLException ignored){};
throw failedToDropObject(transaction); throw failedToDropObject(transaction);
} finally {
try {
db.setAutoCommit(true);
} catch (SQLException ignored){};
} }
} }
@@ -67,9 +67,16 @@ public class SqliteDb extends BaseDb implements ContactDb{
db.setAutoCommit(false); db.setAutoCommit(false);
Query.delete().from(TABLE_CONTACTS).where(ID,equal(contact.id())).execute(db); Query.delete().from(TABLE_CONTACTS).where(ID,equal(contact.id())).execute(db);
Query.delete().from(TABLE_CONTACTS_USERS).where(CONTACT_ID,equal(contact.id())).execute(db); Query.delete().from(TABLE_CONTACTS_USERS).where(CONTACT_ID,equal(contact.id())).execute(db);
db.setAutoCommit(true);
} catch (SQLException e){ } catch (SQLException e){
try {
db.rollback();
} catch (SQLException ignored){};
throw failedToDropObject(t(CONTACT_WITH_ID, ID,contact.id())).causedBy(e); throw failedToDropObject(t(CONTACT_WITH_ID, ID,contact.id())).causedBy(e);
} finally {
try {
db.setAutoCommit(true);
} catch (SQLException ignored){};
} }
} }
@@ -69,9 +69,15 @@ CREATE TABLE IF NOT EXISTS {0} ( {1} VARCHAR(255) PRIMARY KEY, {2} VARCHAR(255)
update(table).set(STATUS).where(STATUS,equal(20)).prepare(db).apply(40).execute(); update(table).set(STATUS).where(STATUS,equal(20)).prepare(db).apply(40).execute();
update(table).set(STATUS).where(STATUS,equal(10)).prepare(db).apply(20).execute(); update(table).set(STATUS).where(STATUS,equal(10)).prepare(db).apply(20).execute();
update(table).set(STATUS).where(STATUS,equal(0)).prepare(db).apply(10).execute(); update(table).set(STATUS).where(STATUS,equal(0)).prepare(db).apply(10).execute();
db.setAutoCommit(true);
} catch (SQLException e) { } catch (SQLException e) {
try {
db.rollback();
} catch (SQLException ignored) {}
throw new RuntimeException(e); throw new RuntimeException(e);
} finally {
try {
db.setAutoCommit(true);
} catch (SQLException ignored){};
} }
} }
} }
@@ -198,11 +198,17 @@ CREATE TABLE IF NOT EXISTS {0} (
rs.close(); rs.close();
delete().from(TABLE_POSITIONS).where(DOCUMENT_ID,equal(docId)).execute(db); delete().from(TABLE_POSITIONS).where(DOCUMENT_ID,equal(docId)).execute(db);
delete().from(TABLE_DOCUMENTS).where(ID,equal(docId)).execute(db); delete().from(TABLE_DOCUMENTS).where(ID,equal(docId)).execute(db);
db.setAutoCommit(true);
if (number != null) return number; if (number != null) return number;
throw failedToDropObject(t(DOCUMENT_WITH_ID, ID,docId)); throw failedToDropObject(t(DOCUMENT_WITH_ID, ID,docId));
} catch (SQLException e){ } catch (SQLException e){
try {
db.rollback();
} catch (SQLException ignored){};
throw failedToDropObject(t(DOCUMENT_WITH_ID, ID,docId)).causedBy(e); throw failedToDropObject(t(DOCUMENT_WITH_ID, ID,docId)).causedBy(e);
} finally {
try {
db.setAutoCommit(true);
} catch (SQLException ignored){};
} }
} }
@@ -217,10 +223,16 @@ CREATE TABLE IF NOT EXISTS {0} (
stmt.setLong(2,pos); stmt.setLong(2,pos);
stmt.execute(); stmt.execute();
stmt.close(); stmt.close();
db.setAutoCommit(true);
return pos; return pos;
} catch (SQLException e) { } catch (SQLException e) {
try {
db.rollback();
} catch (SQLException ignored){};
throw failedToDropObjectFromObject(POSITION,pos,t(Text.DOCUMENT),docId).causedBy(e); throw failedToDropObjectFromObject(POSITION,pos,t(Text.DOCUMENT),docId).causedBy(e);
} finally {
try {
db.setAutoCommit(true);
} catch (SQLException ignored){};
} }
} }
@@ -582,9 +594,15 @@ CREATE TABLE IF NOT EXISTS {0} (
update(TABLE_POSITIONS).set(POS).where(DOCUMENT_ID,equal(docId)).where(POS,equal(pair.left())).prepare(db).apply(-pair.right()).close(); update(TABLE_POSITIONS).set(POS).where(DOCUMENT_ID,equal(docId)).where(POS,equal(pair.left())).prepare(db).apply(-pair.right()).close();
update(TABLE_POSITIONS).set(POS).where(DOCUMENT_ID,equal(docId)).where(POS,equal(pair.right())).prepare(db).apply(pair.left()).close(); update(TABLE_POSITIONS).set(POS).where(DOCUMENT_ID,equal(docId)).where(POS,equal(pair.right())).prepare(db).apply(pair.left()).close();
update(TABLE_POSITIONS).set(POS).where(DOCUMENT_ID,equal(docId)).where(POS,equal(-pair.right())).prepare(db).apply(pair.right()).close(); update(TABLE_POSITIONS).set(POS).where(DOCUMENT_ID,equal(docId)).where(POS,equal(-pair.right())).prepare(db).apply(pair.right()).close();
db.setAutoCommit(true);
} catch (SQLException e) { } catch (SQLException e) {
try {
db.rollback();
} catch (SQLException ignored){};
throw databaseException(FAILED_TO_SWITCH_POSITIONS,"a",pair.left(),"b",pair.right(),docId).causedBy(e); throw databaseException(FAILED_TO_SWITCH_POSITIONS,"a",pair.left(),"b",pair.right(),docId).causedBy(e);
} finally {
try {
db.setAutoCommit(true);
} catch (SQLException ignored){};
} }
return pair; return pair;
} }
@@ -63,10 +63,16 @@ public class SqliteDb extends BaseDb implements StockDb {
rs.close(); rs.close();
if (propertyId == null || propertyId == 0) throw failedToStoreObject(t(PROPERTY)); if (propertyId == null || propertyId == 0) throw failedToStoreObject(t(PROPERTY));
insertInto(TABLE_ITEM_PROPERTIES,ITEM_ID,PROPERTY_ID,VALUE).values(itemId,propertyId,value).execute(db).close(); insertInto(TABLE_ITEM_PROPERTIES,ITEM_ID,PROPERTY_ID,VALUE).values(itemId,propertyId,value).execute(db).close();
db.setAutoCommit(true);
return new Property(propertyId,name,value,unit); return new Property(propertyId,name,value,unit);
} catch (SQLException e) { } catch (SQLException e) {
try {
db.rollback();
} catch (SQLException ignored){};
throw failedToStoreObject(t(PROPERTY)).causedBy(e); throw failedToStoreObject(t(PROPERTY)).causedBy(e);
} finally {
try {
db.setAutoCommit(true);
} catch (SQLException ignored){};
} }
} }
@@ -601,13 +607,16 @@ public class SqliteDb extends BaseDb implements StockDb {
replaceLocationsTable(); replaceLocationsTable();
replaceItemsTable(); replaceItemsTable();
replaceItemPropsTable(); replaceItemPropsTable();
db.setAutoCommit(true);
} catch (Exception e) { } catch (Exception e) {
try { try {
db.rollback(); db.rollback();
} catch (SQLException ignored) { } catch (SQLException ignored) {
} }
throw databaseException(FAILED_TO_UPDATE_OBJECT, OBJECT, t(TABLE_WITH_NAME,NAME,TABLE_LOCATIONS)).causedBy(e); throw databaseException(FAILED_TO_UPDATE_OBJECT, OBJECT, t(TABLE_WITH_NAME,NAME,TABLE_LOCATIONS)).causedBy(e);
} finally {
try {
db.setAutoCommit(true);
} catch (SQLException ignored){};
} }
} }
@@ -340,7 +340,6 @@ CREATE TABLE IF NOT EXISTS {0} (
var addQuery = replaceInto(TABLE_TASK_DEPENDENCIES, TASK_ID, REQUIRED_TASK_ID); var addQuery = replaceInto(TABLE_TASK_DEPENDENCIES, TASK_ID, REQUIRED_TASK_ID);
for (var reqId : task.requiredTasksIds()) addQuery.values(task.id(), reqId); for (var reqId : task.requiredTasksIds()) addQuery.values(task.id(), reqId);
addQuery.execute(db).close(); addQuery.execute(db).close();
db.setAutoCommit(true);
} }
task.clean(REQUIRED_TASKS_IDS); task.clean(REQUIRED_TASKS_IDS);
@@ -354,7 +353,14 @@ CREATE TABLE IF NOT EXISTS {0} (
} }
return task; return task;
} catch (SQLException e){ } catch (SQLException e){
try {
db.rollback();
} catch (SQLException ignored){};
throw failedToStoreObject(task.name()).causedBy(e); throw failedToStoreObject(task.name()).causedBy(e);
} finally {
try {
db.setAutoCommit(true);
} catch (SQLException ignored){};
} }
} }
@@ -84,10 +84,16 @@ CREATE TABLE IF NOT EXISTS {0} (
db.setAutoCommit(false); db.setAutoCommit(false);
Query.delete().from(TABLE_TASK_TIMES).where(TIME_ID,equal(timeId)).execute(db); Query.delete().from(TABLE_TASK_TIMES).where(TIME_ID,equal(timeId)).execute(db);
Query.delete().from(TABLE_TIMES).where(ID,equal(timeId)).execute(db); Query.delete().from(TABLE_TIMES).where(ID,equal(timeId)).execute(db);
db.setAutoCommit(false);
return timeId; return timeId;
} catch (SQLException e) { } catch (SQLException e) {
try {
db.rollback();
} catch (SQLException ignored) {}
throw failedToDropObject(t(TIME_WITH_ID, ID,timeId)).causedBy(e); throw failedToDropObject(t(TIME_WITH_ID, ID,timeId)).causedBy(e);
} finally {
try {
db.setAutoCommit(true);
} catch (SQLException ignored){};
} }
} }