Changeset: 8cc3b51d1984 for monetdb-java
URL: https://dev.monetdb.org/hg/monetdb-java?cmd=changeset;node=8cc3b51d1984
Modified Files:
        src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java
        src/main/java/nl/cwi/monetdb/jdbc/MonetDatabaseMetaData.java
Branch: default
Log Message:

Add a utility method to close objects ignoring any possible SQLExceptions 
thrown.
Use it in finally clauses to reduce object code.


diffs (149 lines):

diff --git a/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java 
b/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java
--- 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.
diff --git a/src/main/java/nl/cwi/monetdb/jdbc/MonetDatabaseMetaData.java 
b/src/main/java/nl/cwi/monetdb/jdbc/MonetDatabaseMetaData.java
--- 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);
                        }
                }
 
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to