Title: [611] trunk/activerecord-jdbc: Probable fix for JRUBY-1096, the intermittent errors about still open resultsets
Revision
611
Author
olabini
Date
2007-06-05 10:40:03 -0400 (Tue, 05 Jun 2007)

Log Message

Probable fix for JRUBY-1096, the intermittent errors about still open resultsets

Modified Paths

Diff

Modified: trunk/activerecord-jdbc/src/java/JdbcAdapterInternalService.java (610 => 611)


--- trunk/activerecord-jdbc/src/java/JdbcAdapterInternalService.java	2007-06-04 03:30:11 UTC (rev 610)
+++ trunk/activerecord-jdbc/src/java/JdbcAdapterInternalService.java	2007-06-05 14:40:03 UTC (rev 611)
@@ -133,8 +133,9 @@
         }
         while (true) {
             Connection c = (Connection) recv.dataGetStruct();
+            ResultSet rs = null;
             try {
-                ResultSet rs = c.getMetaData().getTables(catalog, schemapat, tablepat, types);
+                rs = c.getMetaData().getTables(catalog, schemapat, tablepat, types);
                 List arr = new ArrayList();
                 while (rs.next()) {
                     arr.add(runtime.newString(rs.getString(3).toLowerCase()));
@@ -146,7 +147,12 @@
                     continue;
                 }
                 throw e;
+            } finally {
+                try {
+                    rs.close();
+                } catch(Exception e) {}
             }
+
         }
     }
 
@@ -244,69 +250,70 @@
     private static final java.util.regex.Pattern HAS_SMALL = java.util.regex.Pattern.compile("[a-z]");
     private static final java.util.regex.Pattern HAS_LARGE = java.util.regex.Pattern.compile("[A-Z]");
     private static IRubyObject unmarshal_columns(IRubyObject recv, DatabaseMetaData metadata, ResultSet rs) throws SQLException, IOException {
-        List columns = new ArrayList();
-        boolean isDerby = metadata.getClass().getName().indexOf("derby") != -1;
-        Ruby runtime = recv.getRuntime();
-        ThreadContext ctx = runtime.getCurrentContext();
+        try {
+            List columns = new ArrayList();
+            boolean isDerby = metadata.getClass().getName().indexOf("derby") != -1;
+            Ruby runtime = recv.getRuntime();
+            ThreadContext ctx = runtime.getCurrentContext();
 
-        RubyHash tps = ((RubyHash)(recv.callMethod(ctx, "adapter").callMethod(ctx,"native_database_types")));
+            RubyHash tps = ((RubyHash)(recv.callMethod(ctx, "adapter").callMethod(ctx,"native_database_types")));
 
-        IRubyObject jdbcCol = ((RubyModule)(runtime.getModule("ActiveRecord").getConstant("ConnectionAdapters"))).getConstant("JdbcColumn");
+            IRubyObject jdbcCol = ((RubyModule)(runtime.getModule("ActiveRecord").getConstant("ConnectionAdapters"))).getConstant("JdbcColumn");
 
-        while(rs.next()) {
-            String column_name = rs.getString(4);
-            if(metadata.storesUpperCaseIdentifiers() && !HAS_SMALL.matcher(column_name).find()) {
-                column_name = column_name.toLowerCase();
-            }
+            while(rs.next()) {
+                String column_name = rs.getString(4);
+                if(metadata.storesUpperCaseIdentifiers() && !HAS_SMALL.matcher(column_name).find()) {
+                    column_name = column_name.toLowerCase();
+                }
 
-            String prec = rs.getString(7);
-            String scal = rs.getString(9);
-            int precision = -1;
-            int scale = -1;
-            if(prec != null) {
-                precision = Integer.parseInt(prec);
-                if(scal != null) {
-                    scale = Integer.parseInt(scal);
+                String prec = rs.getString(7);
+                String scal = rs.getString(9);
+                int precision = -1;
+                int scale = -1;
+                if(prec != null) {
+                    precision = Integer.parseInt(prec);
+                    if(scal != null) {
+                        scale = Integer.parseInt(scal);
+                    }
                 }
-            }
-            String type = rs.getString(6);
-            if(prec != null && precision > 0) {
-                type += "(" + precision;
-                if(scal != null && scale > 0) {
-                    type += "," + scale;
+                String type = rs.getString(6);
+                if(prec != null && precision > 0) {
+                    type += "(" + precision;
+                    if(scal != null && scale > 0) {
+                        type += "," + scale;
+                    }
+                    type += ")";
                 }
-                type += ")";
-            }
-            String def = rs.getString(13);
-            IRubyObject _def;
-            if(def == null) {
-                _def = runtime.getNil();
-            } else {
-                if(isDerby && def.length() > 0 && def.charAt(0) == '\'') {
-                    def = def.substring(1, def.length()-1);
+                String def = rs.getString(13);
+                IRubyObject _def;
+                if(def == null) {
+                    _def = runtime.getNil();
+                } else {
+                    if(isDerby && def.length() > 0 && def.charAt(0) == '\'') {
+                        def = def.substring(1, def.length()-1);
+                    }
+                    _def = runtime.newString(def);
                 }
-                _def = runtime.newString(def);
-            }
 
-            IRubyObject c = jdbcCol.callMethod(ctx,"new", new IRubyObject[]{recv.getInstanceVariable("@config"), runtime.newString(column_name),
-                                                                            _def, runtime.newString(type), 
-                                                                            runtime.newBoolean(!rs.getString(18).equals("NO"))});
-            columns.add(c);
+                IRubyObject c = jdbcCol.callMethod(ctx,"new", new IRubyObject[]{recv.getInstanceVariable("@config"), runtime.newString(column_name),
+                                                                                _def, runtime.newString(type), 
+                                                                                runtime.newBoolean(!rs.getString(18).equals("NO"))});
+                columns.add(c);
 
-            IRubyObject tp = (IRubyObject)tps.fastARef(c.callMethod(ctx,"type"));
-            if(tp != null && !tp.isNil() && tp.callMethod(ctx,"[]",runtime.newSymbol("limit")).isNil()) {
-                c.callMethod(ctx,"limit=", runtime.getNil());
-                if(!c.callMethod(ctx,"type").equals(runtime.newSymbol("decimal"))) {
-                    c.callMethod(ctx,"precision=", runtime.getNil());
+                IRubyObject tp = (IRubyObject)tps.fastARef(c.callMethod(ctx,"type"));
+                if(tp != null && !tp.isNil() && tp.callMethod(ctx,"[]",runtime.newSymbol("limit")).isNil()) {
+                    c.callMethod(ctx,"limit=", runtime.getNil());
+                    if(!c.callMethod(ctx,"type").equals(runtime.newSymbol("decimal"))) {
+                        c.callMethod(ctx,"precision=", runtime.getNil());
+                    }
                 }
             }
+            return runtime.newArray(columns);
+        } finally {
+            try {
+                rs.close();
+            } catch(Exception e) {}
         }
-
-        try {
-            rs.close();
-        } catch(Exception e) {}
-
-        return runtime.newArray(columns);
     }
 
     public static IRubyObject primary_keys(IRubyObject recv, IRubyObject _table_name) throws SQLException {
@@ -425,90 +432,102 @@
     }
 
     public static IRubyObject unmarshal_result_downcase(IRubyObject recv, ResultSet rs) throws SQLException, IOException {
+        List results = new ArrayList();
         Ruby runtime = recv.getRuntime();
-        ResultSetMetaData metadata = rs.getMetaData();
-        int col_count = metadata.getColumnCount();
-        IRubyObject[] col_names = new IRubyObject[col_count];
-        int[] col_types = new int[col_count];
-        int[] col_scale = new int[col_count];
+        try {
+            ResultSetMetaData metadata = rs.getMetaData();
+            int col_count = metadata.getColumnCount();
+            IRubyObject[] col_names = new IRubyObject[col_count];
+            int[] col_types = new int[col_count];
+            int[] col_scale = new int[col_count];
 
-        for(int i=0;i<col_count;i++) {
-            col_names[i] = runtime.newString(metadata.getColumnName(i+1).toLowerCase());
-            col_types[i] = metadata.getColumnType(i+1);
-            col_scale[i] = metadata.getScale(i+1);
-        }
-
-        List results = new ArrayList();
-        while(rs.next()) {
-            RubyHash row = RubyHash.newHash(runtime);
             for(int i=0;i<col_count;i++) {
-                row.aset(col_names[i], jdbc_to_ruby(runtime, i+1, col_types[i], col_scale[i], rs));
+                col_names[i] = runtime.newString(metadata.getColumnName(i+1).toLowerCase());
+                col_types[i] = metadata.getColumnType(i+1);
+                col_scale[i] = metadata.getScale(i+1);
             }
-            results.add(row);
+
+            while(rs.next()) {
+                RubyHash row = RubyHash.newHash(runtime);
+                for(int i=0;i<col_count;i++) {
+                    row.aset(col_names[i], jdbc_to_ruby(runtime, i+1, col_types[i], col_scale[i], rs));
+                }
+                results.add(row);
+            }
+        } finally {
+            try {
+                rs.close();
+            } catch(Exception e) {}
         }
  
-        try {
-            rs.close();
-        } catch(Exception e) {}
-
         return runtime.newArray(results);
     }
 
     public static IRubyObject unmarshal_result(IRubyObject recv, ResultSet rs) throws SQLException, IOException {
         Ruby runtime = recv.getRuntime();
-        ResultSetMetaData metadata = rs.getMetaData();
-        boolean storesUpper = rs.getStatement().getConnection().getMetaData().storesUpperCaseIdentifiers();
-        int col_count = metadata.getColumnCount();
-        IRubyObject[] col_names = new IRubyObject[col_count];
-        int[] col_types = new int[col_count];
-        int[] col_scale = new int[col_count];
+        List results = new ArrayList();
+        try {
+            ResultSetMetaData metadata = rs.getMetaData();
+            boolean storesUpper = rs.getStatement().getConnection().getMetaData().storesUpperCaseIdentifiers();
+            int col_count = metadata.getColumnCount();
+            IRubyObject[] col_names = new IRubyObject[col_count];
+            int[] col_types = new int[col_count];
+            int[] col_scale = new int[col_count];
 
-        for(int i=0;i<col_count;i++) {
-            String s1 = metadata.getColumnName(i+1);
-            if(storesUpper && !HAS_SMALL.matcher(s1).find()) {
-                s1 = s1.toLowerCase();
+            for(int i=0;i<col_count;i++) {
+                String s1 = metadata.getColumnName(i+1);
+                if(storesUpper && !HAS_SMALL.matcher(s1).find()) {
+                    s1 = s1.toLowerCase();
+                }
+                col_names[i] = runtime.newString(s1);
+                col_types[i] = metadata.getColumnType(i+1);
+                col_scale[i] = metadata.getScale(i+1);
             }
-            col_names[i] = runtime.newString(s1);
-            col_types[i] = metadata.getColumnType(i+1);
-            col_scale[i] = metadata.getScale(i+1);
-        }
 
-        List results = new ArrayList();
-        while(rs.next()) {
-            RubyHash row = RubyHash.newHash(runtime);
-            for(int i=0;i<col_count;i++) {
-                row.aset(col_names[i], jdbc_to_ruby(runtime, i+1, col_types[i], col_scale[i], rs));
+            while(rs.next()) {
+                RubyHash row = RubyHash.newHash(runtime);
+                for(int i=0;i<col_count;i++) {
+                    row.aset(col_names[i], jdbc_to_ruby(runtime, i+1, col_types[i], col_scale[i], rs));
+                }
+                results.add(row);
             }
-            results.add(row);
+        } finally {
+            try {
+                rs.close();
+            } catch(Exception e) {}
         }
- 
-        try {
-            rs.close();
-        } catch(Exception e) {}
-
         return runtime.newArray(results);
     }
 
     public static IRubyObject unmarshal_result(IRubyObject recv, IRubyObject resultset, Block row_filter) throws SQLException, IOException {
         Ruby runtime = recv.getRuntime();
         ResultSet rs = intoResultSet(resultset);
-        ResultSetMetaData metadata = rs.getMetaData();
-        int col_count = metadata.getColumnCount();
-        IRubyObject[] col_names = new IRubyObject[col_count];
-        int[] col_types = new int[col_count];
-        int[] col_scale = new int[col_count];
+        List results = new ArrayList();
+        try {
+            ResultSetMetaData metadata = rs.getMetaData();
+            int col_count = metadata.getColumnCount();
+            IRubyObject[] col_names = new IRubyObject[col_count];
+            int[] col_types = new int[col_count];
+            int[] col_scale = new int[col_count];
 
-        for(int i=0;i<col_count;i++) {
-            col_names[i] = runtime.newString(metadata.getColumnName(i+1));
-            col_types[i] = metadata.getColumnType(i+1);
-            col_scale[i] = metadata.getScale(i+1);
-        }
+            for(int i=0;i<col_count;i++) {
+                col_names[i] = runtime.newString(metadata.getColumnName(i+1));
+                col_types[i] = metadata.getColumnType(i+1);
+                col_scale[i] = metadata.getScale(i+1);
+            }
 
-        List results = new ArrayList();
-
-        if(row_filter.isGiven()) {
-            while(rs.next()) {
-                if(row_filter.yield(runtime.getCurrentContext(),resultset).isTrue()) {
+            if(row_filter.isGiven()) {
+                while(rs.next()) {
+                    if(row_filter.yield(runtime.getCurrentContext(),resultset).isTrue()) {
+                        RubyHash row = RubyHash.newHash(runtime);
+                        for(int i=0;i<col_count;i++) {
+                            row.aset(col_names[i], jdbc_to_ruby(runtime, i+1, col_types[i], col_scale[i], rs));
+                        }
+                        results.add(row);
+                    }
+                }
+            } else {
+                while(rs.next()) {
                     RubyHash row = RubyHash.newHash(runtime);
                     for(int i=0;i<col_count;i++) {
                         row.aset(col_names[i], jdbc_to_ruby(runtime, i+1, col_types[i], col_scale[i], rs));
@@ -516,20 +535,13 @@
                     results.add(row);
                 }
             }
-        } else {
-            while(rs.next()) {
-                RubyHash row = RubyHash.newHash(runtime);
-                for(int i=0;i<col_count;i++) {
-                    row.aset(col_names[i], jdbc_to_ruby(runtime, i+1, col_types[i], col_scale[i], rs));
-                }
-                results.add(row);
-            }
+
+        } finally {
+            try {
+                rs.close();
+            } catch(Exception e) {}
         }
  
-        try {
-            rs.close();
-        } catch(Exception e) {}
-
         return runtime.newArray(results);
     }
 

Modified: trunk/activerecord-jdbc/test/mysql_simple_test.rb (610 => 611)


--- trunk/activerecord-jdbc/test/mysql_simple_test.rb	2007-06-04 03:30:11 UTC (rev 610)
+++ trunk/activerecord-jdbc/test/mysql_simple_test.rb	2007-06-05 14:40:03 UTC (rev 611)
@@ -3,6 +3,7 @@
 #   drop database if exists weblog_development;
 #   create database weblog_development;
 #   grant all on weblog_development.* to [EMAIL PROTECTED];
+#   flush privileges;
 
 require 'jdbc_common'
 require 'db/mysql'
_______________________________________________
Jruby-extras-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/jruby-extras-devel

Reply via email to