Skip to content

Commit

Permalink
Auto-abort transactions on close, support call chaining when beginnin…
Browse files Browse the repository at this point in the history
…g a transaction.
  • Loading branch information
danielperano committed Mar 4, 2024
1 parent c884795 commit c3dc4b8
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/main/java/myworld/spruce/DBContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ public <T extends DBModule> T getModule(Class<T> moduleType){
return module;
}

public void beginTransaction() throws SQLException {
public DBContext beginTransaction() throws SQLException {
conn.setAutoCommit(false);
inTransaction = true;
return this;
}

public void abortTransaction() throws SQLException {
Expand Down Expand Up @@ -148,7 +149,7 @@ public void close() throws SQLException {
}
}
if(inTransaction){
throw new SQLException("Context cannot be closed with an active transaction. Abort or commit before closing.");
abortTransaction();
}
conn.close();
}
Expand Down

0 comments on commit c3dc4b8

Please sign in to comment.