Reputation: 1393
i am doing some work in irb. This error is intermittent.
Let me say right away. I am using activerecord without rails
If i run active record like this..
ActiveRecord::Base.connection.exec_query('select * from pp_ota_lang where code = "ZH"')
I get the:ActiveRecord::ConnectionNotEstablished (No connection pool with 'primary' found.) error
But it is intermittent!
If i create a model like this...
class PP_OTA_INFO < ActiveRecord::Base
establish_connection(
adapter: "mysql2",
host: "localhost",
username: "root",
password: "password",
database: "ota4user"
)
self.table_name = "pp_ota_info"
end
PP_OTA_INFO.connection.select_all('select * from pp_ota_lang where code = "ZH"') this works without fail.
Am I missing something in mysql setup? I setup mysql with brew and used all the defaults. I can connect to mysql from the command prompt and run whatever.
Here is a session in irb
colmac@Xazoola-2:~ $ irb
irb(main):001:0> **require 'active_record'**
=> true
irb(main):002:0> ActiveRecord::Base.clear_active_connections!
=> []
irb(main):003:0> ActiveRecord::Base.connection.exec_query('select * from pp_ota_lang where code = "ZH"')
Traceback (most recent call last):
7: from /usr/local/opt/ruby/bin/irb:27:in `<main>'
6: from /usr/local/opt/ruby/bin/irb:27:in `load'
5: from /usr/local/Cellar/ruby/2.6.3/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
4: from (irb):3
3: from /usr/local/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_handling.rb:90:in `connection'
2: from /usr/local/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_handling.rb:118:in `retrieve_connection'
1: from /usr/local/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:1013:in `retrieve_connection'
**ActiveRecord::ConnectionNotEstablished (No connection pool with 'primary' found.)**
irb(main):004:0> **ActiveRecord::Base.clear_active_connections!**
=> []
irb(main):005:0> **require 'mysql2'**
=> true
irb(main):006:0> ActiveRecord::Base.connection.exec_query('select * from pp_ota_lang where code = "ZH"')
Traceback (most recent call last):
7: from /usr/local/opt/ruby/bin/irb:27:in `<main>'
6: from /usr/local/opt/ruby/bin/irb:27:in `load'
5: from /usr/local/Cellar/ruby/2.6.3/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
4: from (irb):6
3: from /usr/local/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_handling.rb:90:in `connection'
2: from /usr/local/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_handling.rb:118:in `retrieve_connection'
1: from /usr/local/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:1013:in `retrieve_connection'
ActiveRecord::ConnectionNotEstablished (No connection pool with 'primary' found.)
irb(main):007:0> **ActiveRecord::Base.clear_active_connections!**
=> []
irb(main):008:0> **ActiveRecord::Base.clear_all_connections!**
=> []
irb(main):009:0> class PP_OTA_INFO < ActiveRecord::Base
irb(main):010:1> establish_connection(
irb(main):011:2* adapter: "mysql2",
irb(main):012:2* host: "localhost",
irb(main):013:2* username: "root",
irb(main):014:2* password: "password",
irb(main):015:2* database: "ota4user"
irb(main):016:2> )
irb(main):017:1> self.table_name = "pp_ota_info"
irb(main):018:1> end
=> "pp_ota_info"
irb(main):019:0> **PP_OTA_INFO.connection.select_all('select * from pp_ota_lang where code = "ZH"')**
=> #<ActiveRecord::Result:0x00007fd54c297ad8 @columns=["id", "name", "code"], @rows=[***[27, "Chinese Simplified", "ZH"]***], @hash_rows=nil, @column_types={}>
irb(main):020:0> **ActiveRecord::Base.clear_all_connections!**
as you can see i had a connection...
=> [#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x00007fd54c888f50 @mon_mutex=#<Thread::Mutex:0x00007fd54c888e60>, @mon_mutex_owner_object_id=70277044389800, @mon_owner=nil, @mon_count=0, @query_cache_enabled=#<Concurrent::Map:0x00007fd54c888e38 entries=0 default_proc=#<Proc:0x00007fd54c888d98@/usr/local/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/query_cache.rb:31>>, @spec=#<ActiveRecord::ConnectionAdapters::ConnectionSpecification:0x00007fd54c889220 @name="PP_OTA_INFO", @config={:adapter=>"mysql2", :host=>"localhost", :username=>"root", :password=>"password", :database=>"ota4user"}, @adapter_method="mysql2_connection">, @checkout_timeout=5, @idle_timeout=300.0, @size=5, @thread_cached_conns=#<Concurrent::Map:0x00007fd54c888d48 entries=0 default_proc=nil>, @connections=[], @automatic_reconnect=true, @now_connecting=0, @threads_blocking_new_connections=0, @available=#<ActiveRecord::ConnectionAdapters::ConnectionPool::ConnectionLeasingQueue:0x00007fd54c888ca8 @lock=#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x00007fd54c888f50 ...>, @cond=#<MonitorMixin::ConditionVariable:0x00007fd54c888c58 @monitor=#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x00007fd54c888f50 ...>, @cond=#<Thread::ConditionVariable:0x00007fd54c888c30>>, @num_waiting=0, @queue=[]>, @lock_thread=false, @reaper=#<ActiveRecord::ConnectionAdapters::ConnectionPool::Reaper:0x00007fd54c888be0 @pool=#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x00007fd54c888f50 ...>, @frequency=60.0>>]
**Now with direct sql**
irb(main):021:0> ActiveRecord::Base.connection.exec_query('select * from pp_ota_lang where code = "ZH"')
Traceback (most recent call last):
7: from /usr/local/opt/ruby/bin/irb:27:in `<main>'
6: from /usr/local/opt/ruby/bin/irb:27:in `load'
5: from /usr/local/Cellar/ruby/2.6.3/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
4: from (irb):21
3: from /usr/local/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_handling.rb:90:in `connection'
2: from /usr/local/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_handling.rb:118:in `retrieve_connection'
1: from /usr/local/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:1013:in `retrieve_connection'
**ActiveRecord::ConnectionNotEstablished (No connection pool with 'primary' found.)**
irb(main):022:0>
Any ideas ? Is there something i have to configure on mysql?
Thanks to all of you that have suggestions :)
Upvotes: 3
Views: 5888
Reputation: 24337
By establishing the connection only in the PP_OTA_INFO class, it does not establish a connection for ActiveRecord::Base.
Setup your class with just the table name and then establish the connection with ActiveRecord::Base:
class PP_OTA_INFO < ActiveRecord::Base
self.table_name = "pp_ota_info"
end
ActiveRecord::Base.establish_connection(
adapter: "mysql2",
host: "localhost",
username: "root",
password: "password",
database: "ota4user"
)
Upvotes: 1