Title: [797] trunk/activerecord-jdbc: Expose the connection retry guard to the ruby side, so it can be used by the
Revision
797
Author
nicksieger
Date
2007-11-13 13:13:46 -0500 (Tue, 13 Nov 2007)

Log Message

Expose the connection retry guard to the ruby side, so it can be used by the
#indexes method

Modified Paths

Diff

Modified: trunk/activerecord-jdbc/lib/active_record/connection_adapters/jdbc_adapter.rb (796 => 797)


--- trunk/activerecord-jdbc/lib/active_record/connection_adapters/jdbc_adapter.rb	2007-11-13 17:56:32 UTC (rev 796)
+++ trunk/activerecord-jdbc/lib/active_record/connection_adapters/jdbc_adapter.rb	2007-11-13 18:13:46 UTC (rev 797)
@@ -307,50 +307,47 @@
       #
       # TODO: fix to use reconnect correctly
       def indexes(table_name, name = nil, schema_name = nil)
-        metadata = connection.getMetaData
-        unless String === table_name
-          table_name = table_name.to_s
-        else
-          table_name = table_name.dup
-        end
-        table_name.upcase! if metadata.storesUpperCaseIdentifiers
-        table_name.downcase! if metadata.storesLowerCaseIdentifiers
-        resultset = metadata.getIndexInfo(nil, schema_name, table_name, false, false)
-        primary_keys = primary_keys(table_name)
-        indexes = []
-        current_index = nil
-        while resultset.next
-          index_name = resultset.get_string(Jdbc::IndexMetaData::INDEX_NAME)
-          next unless index_name
-          index_name.downcase!
-          column_name = resultset.get_string(Jdbc::IndexMetaData::COLUMN_NAME).downcase
-
-          next if primary_keys.include? column_name
-
-          # We are working on a new index
-          if current_index != index_name
-            current_index = index_name
-            table_name = resultset.get_string(Jdbc::IndexMetaData::TABLE_NAME).downcase
-            non_unique = resultset.get_boolean(Jdbc::IndexMetaData::NON_UNIQUE)
-
-            # empty list for column names, we'll add to that in just a bit
-            indexes << IndexDefinition.new(table_name, index_name, !non_unique, [])
+        with_connection_retry_guard do |conn|
+          metadata = conn.getMetaData
+          begin
+            unless String === table_name
+              table_name = table_name.to_s
+            else
+              table_name = table_name.dup
+            end
+            table_name.upcase! if metadata.storesUpperCaseIdentifiers
+            table_name.downcase! if metadata.storesLowerCaseIdentifiers
+            resultset = metadata.getIndexInfo(nil, schema_name, table_name, false, false)
+            primary_keys = primary_keys(table_name)
+            indexes = []
+            current_index = nil
+            while resultset.next
+              index_name = resultset.get_string(Jdbc::IndexMetaData::INDEX_NAME)
+              next unless index_name
+              index_name.downcase!
+              column_name = resultset.get_string(Jdbc::IndexMetaData::COLUMN_NAME).downcase
+              
+              next if primary_keys.include? column_name
+              
+              # We are working on a new index
+              if current_index != index_name
+                current_index = index_name
+                table_name = resultset.get_string(Jdbc::IndexMetaData::TABLE_NAME).downcase
+                non_unique = resultset.get_boolean(Jdbc::IndexMetaData::NON_UNIQUE)
+                
+                # empty list for column names, we'll add to that in just a bit
+                indexes << IndexDefinition.new(table_name, index_name, !non_unique, [])
+              end
+              
+              # One or more columns can be associated with an index
+              indexes.last.columns << column_name
+            end
+            resultset.close
+            indexes
+          ensure
+            metadata.close rescue nil
           end
-
-          # One or more columns can be associated with an index
-          indexes.last.columns << column_name
         end
-        resultset.close
-        indexes
-      rescue
-        if connection.is_closed
-          reconnect!
-          retry
-        else
-          raise
-        end
-      ensure
-        metadata.close rescue nil
       end
 
       private

Modified: trunk/activerecord-jdbc/src/java/jdbc_adapter/JdbcAdapterInternalService.java (796 => 797)


--- trunk/activerecord-jdbc/src/java/jdbc_adapter/JdbcAdapterInternalService.java	2007-11-13 17:56:32 UTC (rev 796)
+++ trunk/activerecord-jdbc/src/java/jdbc_adapter/JdbcAdapterInternalService.java	2007-11-13 18:13:46 UTC (rev 797)
@@ -69,12 +69,14 @@
 import org.jruby.util.ByteList;
 
 public class JdbcAdapterInternalService implements BasicLibraryService {
+
     public boolean basicLoad(final Ruby runtime) throws IOException {
         RubyClass cJdbcConn = ((RubyModule)(runtime.getModule("ActiveRecord").getConstant("ConnectionAdapters"))).
             defineClassUnder("JdbcConnection",runtime.getObject(),runtime.getObject().getAllocator());
 
         CallbackFactory cf = runtime.callbackFactory(JdbcAdapterInternalService.class);
         cJdbcConn.defineMethod("unmarshal_result",cf.getSingletonMethod("unmarshal_result", IRubyObject.class));
+        cJdbcConn.defineMethod("with_connection_retry_guard",cf.getSingletonMethod("with_connection_retry_guard"));
         cJdbcConn.defineFastMethod("connection",cf.getFastSingletonMethod("connection"));
         cJdbcConn.defineFastMethod("reconnect!",cf.getFastSingletonMethod("reconnect"));
         cJdbcConn.defineFastMethod("execute_update",cf.getFastSingletonMethod("execute_update", IRubyObject.class));
@@ -236,9 +238,7 @@
                 prev.close();
             } catch(Exception e) {}
         }
-        IRubyObject conn_object = Java.java_to_ruby(recv,
-                JavaObject.wrap(recv.getRuntime(), c), Block.NULL_BLOCK);
-        recv.setInstanceVariable("@connection", conn_object);
+        recv.setInstanceVariable("@connection", wrappedConnection(recv,c));
         recv.dataWrapStruct(c);
         return recv;
     }
@@ -259,6 +259,16 @@
         return recv;
     }
 
+    public static IRubyObject with_connection_retry_guard(final IRubyObject recv, final Block block) {
+        return withConnectionAndRetry(recv, new SQLBlock() {
+            public IRubyObject call(Connection c) throws SQLException {
+                return block.call(recv.getRuntime().getCurrentContext(), new IRubyObject[] {
+                    wrappedConnection(recv, c)
+                });
+            }
+        });
+    }
+
     private static IRubyObject withConnectionAndRetry(IRubyObject recv, SQLBlock block) {
         final int TRIES = 10;
         int i = 0;
@@ -1009,4 +1019,8 @@
     private static RuntimeException wrap(IRubyObject recv, Throwable exception) {
         return recv.getRuntime().newArgumentError(exception.getMessage());
     }
+
+    private static IRubyObject wrappedConnection(IRubyObject recv, Connection c) {
+        return Java.java_to_ruby(recv, JavaObject.wrap(recv.getRuntime(), c), Block.NULL_BLOCK);
+    }
 }
_______________________________________________
Jruby-extras-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/jruby-extras-devel

Reply via email to