Mercurial > hg > monetdb-java
comparison src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java @ 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 | c5efd6e661e5 |
children | 0b01771d5f8a |
comparison
equal
deleted
inserted
replaced
299:1f0324072b0c | 300:8cc3b51d1984 |
---|---|
1330 // SQLState = 25005 | 1330 // SQLState = 25005 |
1331 isValid = true; | 1331 isValid = true; |
1332 } | 1332 } |
1333 /* ignore stmt errors/exceptions, we are only testing if the connection is still alive and usable */ | 1333 /* ignore stmt errors/exceptions, we are only testing if the connection is still alive and usable */ |
1334 } finally { | 1334 } finally { |
1335 if (rs != null) { | 1335 closeResultsetStatement(rs, stmt); |
1336 try { | |
1337 rs.close(); | |
1338 } catch (Exception e2) { /* ignore error */ } | |
1339 } | |
1340 if (stmt != null) { | |
1341 try { | |
1342 stmt.close(); | |
1343 } catch (Exception e2) { /* ignore error */ } | |
1344 } | |
1345 } | 1336 } |
1346 return isValid; | 1337 return isValid; |
1347 } | 1338 } |
1348 | 1339 |
1349 /** | 1340 /** |
1509 st = createStatement(); | 1500 st = createStatement(); |
1510 if (st != null) | 1501 if (st != null) |
1511 st.execute("SET SCHEMA \"" + schema + "\""); | 1502 st.execute("SET SCHEMA \"" + schema + "\""); |
1512 // do not catch any Exception, just let it propagate | 1503 // do not catch any Exception, just let it propagate |
1513 } finally { | 1504 } finally { |
1514 if (st != null) { | 1505 closeResultsetStatement(null, st); |
1515 try { | |
1516 st.close(); | |
1517 } catch (SQLException e) { /* ignore */ } | |
1518 } | |
1519 } | 1506 } |
1520 } | 1507 } |
1521 | 1508 |
1522 /** | 1509 /** |
1523 * Retrieves this Connection object's current schema name. | 1510 * Retrieves this Connection object's current schema name. |
1544 cur_schema = rs.getString(1); | 1531 cur_schema = rs.getString(1); |
1545 } | 1532 } |
1546 } | 1533 } |
1547 // do not catch any Exception, just let it propagate | 1534 // do not catch any Exception, just let it propagate |
1548 } finally { | 1535 } finally { |
1549 if (rs != null) { | 1536 closeResultsetStatement(rs, st); |
1550 try { | |
1551 rs.close(); | |
1552 } catch (SQLException e) { /* ignore */ } | |
1553 } | |
1554 if (st != null) { | |
1555 try { | |
1556 st.close(); | |
1557 } catch (SQLException e) { /* ignore */ } | |
1558 } | |
1559 } | 1537 } |
1560 if (cur_schema == null) | 1538 if (cur_schema == null) |
1561 throw new SQLException("Failed to fetch schema name", "02000"); | 1539 throw new SQLException("Failed to fetch schema name", "02000"); |
1562 return cur_schema; | 1540 return cur_schema; |
1563 } | 1541 } |
1726 } | 1704 } |
1727 } | 1705 } |
1728 } catch (SQLException se) { | 1706 } catch (SQLException se) { |
1729 /* ignore */ | 1707 /* ignore */ |
1730 } finally { | 1708 } finally { |
1731 if (rs != null) { | 1709 closeResultsetStatement(rs, stmt); |
1732 try { | |
1733 rs.close(); | |
1734 } catch (SQLException e) { /* ignore */ } | |
1735 } | |
1736 if (stmt != null) { | |
1737 try { | |
1738 stmt.close(); | |
1739 } catch (SQLException e) { /* ignore */ } | |
1740 } | |
1741 } | 1710 } |
1742 // for debug: System.out.println("testTableExists(" + tablename + ") returns: " + exists); | 1711 // for debug: System.out.println("testTableExists(" + tablename + ") returns: " + exists); |
1743 return exists; | 1712 return exists; |
1713 } | |
1714 | |
1715 /** | |
1716 * Closes a ResultSet and/or Statement object without throwing any SQLExceptions | |
1717 * It can be used in the finally clause after creating a Statement and | |
1718 * (optionally) executed a query which produced a ResultSet. | |
1719 * | |
1720 * @param rs ResultSet object to be closed. It may be null | |
1721 * @param st Statement object to be closed. It may be null | |
1722 */ | |
1723 static final void closeResultsetStatement(final ResultSet rs, final Statement st) { | |
1724 if (rs != null) { | |
1725 try { | |
1726 rs.close(); | |
1727 } catch (SQLException e) { /* ignore */ } | |
1728 } | |
1729 if (st != null) { | |
1730 try { | |
1731 st.close(); | |
1732 } catch (SQLException e) { /* ignore */ } | |
1733 } | |
1744 } | 1734 } |
1745 | 1735 |
1746 /** | 1736 /** |
1747 * Sends the given string to MonetDB as special transaction command. | 1737 * Sends the given string to MonetDB as special transaction command. |
1748 * All possible returned information is discarded. | 1738 * All possible returned information is discarded. |