Skip to content

Commit 7ee5ab3

Browse files
author
Joey Marianer
committed
Use with_connection instead of calling release_connection directly, since the latter may release a connection that belongs to someone else
1 parent 9babda8 commit 7ee5ab3

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

lib/closure_tree/has_closure_tree.rb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@ def has_closure_tree(options = {})
1414
:with_advisory_lock
1515
)
1616

17-
class_attribute :_ct
18-
self._ct = ClosureTree::Support.new(self, options)
17+
connection_pool.with_connection {
18+
class_attribute :_ct
19+
self._ct = ClosureTree::Support.new(self, options)
1920

20-
# Auto-inject the hierarchy table
21-
# See https://github.com/patshaughnessy/class_factory/blob/master/lib/class_factory/class_factory.rb
22-
class_attribute :hierarchy_class
23-
self.hierarchy_class = _ct.hierarchy_class_for_model
21+
# Auto-inject the hierarchy table
22+
# See https://github.com/patshaughnessy/class_factory/blob/master/lib/class_factory/class_factory.rb
23+
class_attribute :hierarchy_class
24+
self.hierarchy_class = _ct.hierarchy_class_for_model
2425

25-
# tests fail if you include Model before HierarchyMaintenance wtf
26-
include ClosureTree::HierarchyMaintenance
27-
include ClosureTree::Model
28-
include ClosureTree::Finders
29-
include ClosureTree::HashTree
30-
include ClosureTree::Digraphs
26+
# tests fail if you include Model before HierarchyMaintenance wtf
27+
include ClosureTree::HierarchyMaintenance
28+
include ClosureTree::Model
29+
include ClosureTree::Finders
30+
include ClosureTree::HashTree
31+
include ClosureTree::Digraphs
3132

32-
include ClosureTree::DeterministicOrdering if _ct.order_option?
33-
include ClosureTree::NumericDeterministicOrdering if _ct.order_is_numeric?
34-
35-
connection_pool.release_connection
33+
include ClosureTree::DeterministicOrdering if _ct.order_option?
34+
include ClosureTree::NumericDeterministicOrdering if _ct.order_is_numeric?
35+
}
3636
rescue StandardError => e
3737
raise e unless ClosureTree.configuration.database_less
3838
end

spec/pool_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
require 'spec_helper'
22

33
describe 'Configuration' do
4+
before(:each) do
5+
# Make sure we start up with no active connection
6+
ActiveRecord::Base.connection_pool.release_connection
7+
end
8+
49
it 'returns connection to the pool after has_closure_tree setup' do
510
class TypeDuplicate < ActiveRecord::Base
611
self.table_name = "namespace_type#{table_name_suffix}"

0 commit comments

Comments
 (0)