Skip to content

Commit f374e16

Browse files
committed
Improve error handling in DbState#run
1 parent a4fb2df commit f374e16

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

core/src/main/java/fj/control/db/DbState.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,21 @@ public Unit run(final Connection c) throws SQLException {
9494
* @throws SQLException in case of a database error.
9595
*/
9696
public <A> A run(final DB<A> dba) throws SQLException {
97-
final Connection c = pc.connect();
98-
c.setAutoCommit(false);
99-
try {
100-
final A a = dba.run(c);
97+
try (Connection c = pc.connect()) {
98+
c.setAutoCommit(false);
99+
final A a;
100+
try {
101+
a = dba.run(c);
102+
} catch (RuntimeException | SQLException e) {
103+
try {
104+
c.rollback();
105+
} catch (Exception re) {
106+
e.addSuppressed(re);
107+
}
108+
throw e;
109+
}
101110
terminal.run(c);
102111
return a;
103-
} catch (SQLException e) {
104-
c.rollback();
105-
throw e;
106-
}
107-
finally {
108-
c.close();
109112
}
110113
}
111114
}

0 commit comments

Comments
 (0)