From 6e97820170f6ad024ec5caa66541eabdcaafb7a4 Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Fri, 9 Jun 2023 06:17:48 +0300 Subject: [PATCH] #221: catch and rethrow better --- src/main/aspect/.gitkeep | 0 src/main/aspect/README.txt | 1 - src/main/java/com/jcabi/jdbc/JdbcSession.java | 19 +++++++++++++++---- 3 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 src/main/aspect/.gitkeep delete mode 100644 src/main/aspect/README.txt diff --git a/src/main/aspect/.gitkeep b/src/main/aspect/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/src/main/aspect/README.txt b/src/main/aspect/README.txt deleted file mode 100644 index c0f5b07f..00000000 --- a/src/main/aspect/README.txt +++ /dev/null @@ -1 +0,0 @@ -This directory is an indicator of AspectJ usage in the module. \ No newline at end of file diff --git a/src/main/java/com/jcabi/jdbc/JdbcSession.java b/src/main/java/com/jcabi/jdbc/JdbcSession.java index 912d661c..5cb14bcb 100644 --- a/src/main/java/com/jcabi/jdbc/JdbcSession.java +++ b/src/main/java/com/jcabi/jdbc/JdbcSession.java @@ -436,6 +436,7 @@ public T select(final Outcome outcome) * @throws SQLException If fails * @checkstyle ExecutableStatementCount (100 lines) */ + @SuppressWarnings({"PMD.PreserveStackTrace", "PMD.ExceptionAsFlowControl"}) private T run(final Outcome outcome, final Connect connect, final Request request) throws SQLException { @@ -443,9 +444,9 @@ private T run(final Outcome outcome, 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); @@ -463,8 +464,18 @@ private T run(final Outcome 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 { @@ -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();