Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

ConnectionNotEstablished exception in ActiveRecord::TestFixtures.teardown_fixtures #13812

Closed
ejstembler opened this Issue · 3 comments

2 participants

@ejstembler

I'm receiving an ActiveRecord::ConnectionNotEstablished: no connection available exception in fixtures.rb line 968. No stacktrace is logged for some reason, only the error message. This is causing my test case to fail even though it should be passing. The test itself passes, however the exception is raised in the teardown callback. I've verified that this happens in the current version and in 3.2.16 (line 880).

I have an atypical, multi-database, setup where I'm using Jruby + JDBC with Microsoft SQL Server as the main Rails database, and Netezza as a separate data warehouse. The Netezza models use establish_connection to connect using a different config. Something like this:

# app/models/netezza/sample_data.rb
class SampleData < ActiveRecord::Base
  self.primary_key = 'id'
  establish_connection Rails.configuration.database_configuration["netezza_#{Rails.env}"]
end

The class I'm testing does not use the main SQL Server database, it only uses the Netezza one. However, when I step through the code it seems like AR is trying to rollback transactions on the main database, which was never opened; thus the ConnectionNotEstablished exception. This is my best guess anyway.

I'm able to circumvent the exception if I add a check for connection.active? in the conditional statement. For example, in the current version, change:

connection.rollback_transaction if connection.transaction_open?

to

connection.rollback_transaction if connection.active? && connection.transaction_open?

In version 3.2.16, change:

if connection.open_transactions != 0

to

if connection.active? && connection.open_transactions != 0

Alternatively, monkey patch for 3.2.16:

# lib/extensions/activerecord_fixtures_extensions.rb
module ActiveRecord

  module TestFixtures

    def teardown_fixtures
      return unless defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank?

      unless run_in_transaction?
        ActiveRecord::Fixtures.reset_cache
      end

      # Rollback changes if a transaction is active.
      if run_in_transaction?
        @fixture_connections.each do |connection|
          if connection.active? && connection.open_transactions != 0
            connection.rollback_db_transaction
            connection.decrement_open_transactions
          end
        end
        @fixture_connections.clear
      end
      ActiveRecord::Base.clear_active_connections!
    end

  end

end

In any case, I'm not sure what the full ramifications of this would be. The larger issue/question is probably how to properly support multiple databases in Rails in general, including the test frameworks.

@ejstembler

I'm wondering if this might boil down to a thread safety issue between ActiveRecord and Jruby?

I watched a Garden City Ruby 2014 video on Confreaks.com, Lessons Learnt Building India's E-Commerce Supply Chain in Ruby, which briefly described one of the issues I was also encountering. Namely, where ActiveRecord create and save calls were completing, but no data was showing up in the database. The presenter mentioned that they ended up having to abandon Jruby and return to MRI.

@ejstembler ejstembler added the stale label
@rails-bot
Collaborator

This issue has been automatically marked as stale because it has not been commented on for at least
three months.

The resources of the Rails team are limited, and so we are asking for your help.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@rails-bot rails-bot closed this
@rails-bot
Collaborator

This issue has been automatically closed because of inactivity.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.