commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r348860 - /jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/JDBCResources.java
Date Fri, 25 Nov 2005 03:25:18 GMT
Author: niallp
Date: Thu Nov 24 19:25:10 2005
New Revision: 348860

URL: http://svn.apache.org/viewcvs?rev=348860&view=rev
Log:
Change Connection from an instance variable to a local method variable and handle closing
properly.

Modified:
    jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/JDBCResources.java

Modified: jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/JDBCResources.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/JDBCResources.java?rev=348860&r1=348859&r2=348860&view=diff
==============================================================================
--- jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/JDBCResources.java
(original)
+++ jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/JDBCResources.java
Thu Nov 24 19:25:10 2005
@@ -85,8 +85,6 @@
      */
     private transient Log log = LogFactory.getLog(JDBCResources.class);
 
-    private Connection con = null;
-
     // ----------------------------------------------------------- Constructors
 
     /**
@@ -230,17 +228,44 @@
         String valColumn    = connectionProps.getProperty("jdbc.sql.val.column");
         Properties pairs = new Properties();
         
+        Connection con = null;
+        PreparedStatement stmt = null;
+        ResultSet rs = null;
+        try {
             Class.forName(driver).newInstance();
             con = DriverManager.getConnection(url, user, pass);
 
             String query = "SELECT " + keyColumn + ", " + valColumn + 
                     " FROM " + table + " where " + localeColumn + "= '" + locale + "'";
-            PreparedStatement stmt = con.prepareStatement(query);
-            ResultSet rs = stmt.executeQuery();
+            stmt = con.prepareStatement(query);
+            rs = stmt.executeQuery();
 
             while (rs.next()) {
                 pairs.put(rs.getString(keyColumn), rs.getString(valColumn));
             }
+        } finally {
+            try {
+                if (rs != null) {
+                    rs.close();
+                }
+            } catch(Exception e) {
+                getLog().warn("Error closing ResultSet: " + e);
+            }
+            try {
+                if (stmt != null) {
+                    stmt.close();
+                }
+            } catch(Exception e) {
+                getLog().warn("Error closing Statement: " + e);
+            }
+            try {
+                if (con != null) {
+                    con.close();
+                }
+            } catch(Exception e) {
+                getLog().warn("Error closing Connection: " + e);
+            }
+        }
 
         return pairs;
     }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message