Mercurial > hg > monetdb-java
changeset 300:8cc3b51d1984
Add a utility method to close objects ignoring any possible SQLExceptions thrown.
Use it in finally clauses to reduce object code.
author | Martin van Dinther <martin.van.dinther@monetdbsolutions.com> |
---|---|
date | Thu, 15 Aug 2019 16:57:25 +0200 (2019-08-15) |
parents | 1f0324072b0c |
children | 59bc8bebbfe9 |
files | src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java src/main/java/nl/cwi/monetdb/jdbc/MonetDatabaseMetaData.java |
diffstat | 2 files changed, 28 insertions(+), 60 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java +++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java @@ -1332,16 +1332,7 @@ public class MonetConnection } /* ignore stmt errors/exceptions, we are only testing if the connection is still alive and usable */ } finally { - if (rs != null) { - try { - rs.close(); - } catch (Exception e2) { /* ignore error */ } - } - if (stmt != null) { - try { - stmt.close(); - } catch (Exception e2) { /* ignore error */ } - } + closeResultsetStatement(rs, stmt); } return isValid; } @@ -1511,11 +1502,7 @@ public class MonetConnection st.execute("SET SCHEMA \"" + schema + "\""); // do not catch any Exception, just let it propagate } finally { - if (st != null) { - try { - st.close(); - } catch (SQLException e) { /* ignore */ } - } + closeResultsetStatement(null, st); } } @@ -1546,16 +1533,7 @@ public class MonetConnection } // do not catch any Exception, just let it propagate } finally { - if (rs != null) { - try { - rs.close(); - } catch (SQLException e) { /* ignore */ } - } - if (st != null) { - try { - st.close(); - } catch (SQLException e) { /* ignore */ } - } + closeResultsetStatement(rs, st); } if (cur_schema == null) throw new SQLException("Failed to fetch schema name", "02000"); @@ -1728,22 +1706,34 @@ public class MonetConnection } catch (SQLException se) { /* ignore */ } finally { - if (rs != null) { - try { - rs.close(); - } catch (SQLException e) { /* ignore */ } - } - if (stmt != null) { - try { - stmt.close(); - } catch (SQLException e) { /* ignore */ } - } + closeResultsetStatement(rs, stmt); } // for debug: System.out.println("testTableExists(" + tablename + ") returns: " + exists); return exists; } /** + * Closes a ResultSet and/or Statement object without throwing any SQLExceptions + * It can be used in the finally clause after creating a Statement and + * (optionally) executed a query which produced a ResultSet. + * + * @param rs ResultSet object to be closed. It may be null + * @param st Statement object to be closed. It may be null + */ + static final void closeResultsetStatement(final ResultSet rs, final Statement st) { + if (rs != null) { + try { + rs.close(); + } catch (SQLException e) { /* ignore */ } + } + if (st != null) { + try { + st.close(); + } catch (SQLException e) { /* ignore */ } + } + } + + /** * Sends the given string to MonetDB as special transaction command. * All possible returned information is discarded. * Encountered errors are reported.
--- a/src/main/java/nl/cwi/monetdb/jdbc/MonetDatabaseMetaData.java +++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetDatabaseMetaData.java @@ -69,16 +69,7 @@ public class MonetDatabaseMetaData } /* do not catch SQLException here, as we want to know it when it fails */ } finally { - if (rs != null) { - try { - rs.close(); - } catch (SQLException e) { /* ignore */ } - } - if (st != null) { - try { - st.close(); - } catch (SQLException e) { /* ignore */ } - } + MonetConnection.closeResultsetStatement(rs, st); } // for debug: System.out.println("Read: env_current_user: " + env_current_user + " env_monet_version: " + env_monet_version + " env_max_clients: " + env_max_clients); } @@ -465,16 +456,7 @@ public class MonetDatabaseMetaData } catch (SQLException e) { /* ignore */ } finally { - if (rs != null) { - try { - rs.close(); - } catch (SQLException e) { /* ignore */ } - } - if (st != null) { - try { - st.close(); - } catch (SQLException e) { /* ignore */ } - } + MonetConnection.closeResultsetStatement(rs, st); } // for debug: System.out.println("SQL query: " + query + "\nResult string: " + sb.toString()); return sb.toString(); @@ -3173,11 +3155,7 @@ public class MonetDatabaseMetaData } catch (SQLException e) { // ignore } finally { - if (count != null) { - try { - count.close(); - } catch (SQLException e) { /* ignore */ } - } + MonetConnection.closeResultsetStatement(count, null); } }