From da879a54ef8bade9b631cc5b801354eebaa75000 Mon Sep 17 00:00:00 2001 From: Joey Marianer Date: Tue, 31 Jul 2018 15:50:01 -0700 Subject: [PATCH 1/2] Use with_connection instead of calling release_connection directly, since the latter may release a connection that belongs to someone else --- lib/closure_tree/has_closure_tree.rb | 56 ++++++++++++++-------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/lib/closure_tree/has_closure_tree.rb b/lib/closure_tree/has_closure_tree.rb index b0bc5b1a..4ce8d300 100644 --- a/lib/closure_tree/has_closure_tree.rb +++ b/lib/closure_tree/has_closure_tree.rb @@ -1,38 +1,38 @@ module ClosureTree module HasClosureTree def has_closure_tree(options = {}) - options.assert_valid_keys( - :parent_column_name, - :dependent, - :hierarchy_class_name, - :hierarchy_table_name, - :name_column, - :order, - :dont_order_roots, - :numeric_order, - :touch, - :with_advisory_lock - ) + connection_pool.with_connection { + options.assert_valid_keys( + :parent_column_name, + :dependent, + :hierarchy_class_name, + :hierarchy_table_name, + :name_column, + :order, + :dont_order_roots, + :numeric_order, + :touch, + :with_advisory_lock + ) - class_attribute :_ct - self._ct = ClosureTree::Support.new(self, options) + class_attribute :_ct + self._ct = ClosureTree::Support.new(self, options) - # Auto-inject the hierarchy table - # See https://github.com/patshaughnessy/class_factory/blob/master/lib/class_factory/class_factory.rb - class_attribute :hierarchy_class - self.hierarchy_class = _ct.hierarchy_class_for_model + # Auto-inject the hierarchy table + # See https://github.com/patshaughnessy/class_factory/blob/master/lib/class_factory/class_factory.rb + class_attribute :hierarchy_class + self.hierarchy_class = _ct.hierarchy_class_for_model - # tests fail if you include Model before HierarchyMaintenance wtf - include ClosureTree::HierarchyMaintenance - include ClosureTree::Model - include ClosureTree::Finders - include ClosureTree::HashTree - include ClosureTree::Digraphs + # tests fail if you include Model before HierarchyMaintenance wtf + include ClosureTree::HierarchyMaintenance + include ClosureTree::Model + include ClosureTree::Finders + include ClosureTree::HashTree + include ClosureTree::Digraphs - include ClosureTree::DeterministicOrdering if _ct.order_option? - include ClosureTree::NumericDeterministicOrdering if _ct.order_is_numeric? - - connection_pool.release_connection + include ClosureTree::DeterministicOrdering if _ct.order_option? + include ClosureTree::NumericDeterministicOrdering if _ct.order_is_numeric? + } rescue StandardError => e raise e unless ClosureTree.configuration.database_less end From f65b12bdb40098474f975dafe6032f65fd092969 Mon Sep 17 00:00:00 2001 From: Joey Marianer Date: Tue, 31 Jul 2018 16:51:01 -0700 Subject: [PATCH 2/2] Fix the spec --- spec/pool_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/pool_spec.rb b/spec/pool_spec.rb index de25c4fb..89d4e066 100644 --- a/spec/pool_spec.rb +++ b/spec/pool_spec.rb @@ -1,6 +1,11 @@ require 'spec_helper' describe 'Configuration' do + before(:each) do + # Make sure we start up with no active connection + ActiveRecord::Base.connection_pool.release_connection + end + it 'returns connection to the pool after has_closure_tree setup' do class TypeDuplicate < ActiveRecord::Base self.table_name = "namespace_type#{table_name_suffix}"