Skip to content

Commit

Permalink
#221: catch and rethrow better
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jun 9, 2023
1 parent 4cb1682 commit 6e97820
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
Empty file added src/main/aspect/.gitkeep
Empty file.
1 change: 0 additions & 1 deletion src/main/aspect/README.txt

This file was deleted.

19 changes: 15 additions & 4 deletions src/main/java/com/jcabi/jdbc/JdbcSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -436,16 +436,17 @@ public <T> T select(final Outcome<T> outcome)
* @throws SQLException If fails
* @checkstyle ExecutableStatementCount (100 lines)
*/
@SuppressWarnings({"PMD.PreserveStackTrace", "PMD.ExceptionAsFlowControl"})
private <T> T run(final Outcome<T> outcome,
final Connect connect, final Request request)
throws SQLException {
if (this.query == null) {
throw new IllegalStateException("call #sql() first");
}
final Connection conn = this.connect();
conn.setAutoCommit(this.auto);
final T result;
try {
conn.setAutoCommit(this.auto);
final PreparedStatement stmt = connect.open(conn);
try {
this.configure(stmt);
Expand All @@ -463,8 +464,18 @@ private <T> T run(final Outcome<T> outcome,
}
} catch (final SQLException ex) {
if (!this.auto) {
conn.rollback();
this.disconnect();
try {
conn.rollback();
this.disconnect();
} catch (final SQLException exc) {
throw new SQLException(
String.format(
"Failed to rollback after failure: %s",
exc.getMessage()
),
ex
);
}
}
throw new SQLException(ex);
} finally {
Expand Down Expand Up @@ -498,7 +509,7 @@ private void disconnect() throws SQLException {
final Connection conn = this.connection.getAndSet(null);
if (conn == null) {
throw new IllegalStateException(
"connection is not open, can't close"
"Connection is not open, can't close"
);
}
conn.close();
Expand Down

0 comments on commit 6e97820

Please sign in to comment.