Skip to content

Transaction Usage

How do we want to use our custom transaction architecture? Mr. @mexis and me have elaborated three main possibilities:

Option 1: Traditional Finally

  • Pro: clean and readable
  • Con: rather long, manual cleanup
Transaction tx = TransactionManager.begin();
try {
    tx.newAttachmentGateway().getContentByID(0);
    tx.commit();
} catch (NotFoundException | TransactionException e) {
    tx.abort();
} finally {
    tx.close();
}

Option 2: Nested AutoCloseable

  • Pro: automatic cleanup
  • Con: rather long
try (Transaction tx = TransactionManager.begin()) {
    try {
        tx.newAttachmentGateway().getContentByID(0);
        tx.commit();
    } catch (NotFoundException | TransactionException e) {
        tx.abort();
    }
}

Option 3: Hacky AutoCloseable

  • Pro: the shortest solution
  • Con: hacky initialization
Transaction tx = TransactionManager.begin();
try (tx) {
    tx.newAttachmentGateway().getContentByID(0);
    tx.commit();
} catch (NotFoundException | TransactionException e) {
    tx.abort();
}

We would really appreciate your thoughts on this!