3

I am working on JRuby on Rails Application having JRuby-1.6.7 and Rails 3.2.2

I have mysql database. I was trying for database adapter.

I used mysql2 and jdbcmysql database adapter. Both are working.

But if I do not access application for some time and after that when I access it, it gives error as follows

undefined method `explain' for #<ActiveRecord::ConnectionAdapters::MySQLAdapter:0x1139ba37>

Which database adapter I should prefer ? Will my aaplication work fine after deployment ?

3 Answers3

4

I got the same error before, but after some effort I have defined adapter's in my gem file as below, and things are working fine till today :

if defined?(JRUBY_VERSION)
  gem 'activerecord-jdbc-adapter', '=1.1.3'
  gem 'jdbc-mysql', :require=>false
  gem 'activerecord-jdbcmysql-adapter', '=1.1.3'
else
  gem 'mysql2'
end

hope that helps .

Mikey
  • 2,942
  • 33
  • 37
Vik
  • 5,931
  • 3
  • 31
  • 38
0

I tried @Vik's solution of downgrading activerecord-jdbc, but it did not work for me - caused more problems.

I found this on the activerecord-jdbc github issues list:

https://github.com/jruby/activerecord-jdbc-adapter/issues/159

Basically it monkey-patches the new explain method to do nothing - until its extended to support it.

HTH, Chris

Chris Kimpton
  • 5,546
  • 6
  • 45
  • 72
  • I still have some problem with it.Again got error "undefined method explain".I think jdbc-mysqladapter internally using mysql adapter from rails and mysql adapter do not have explain method but mysql2 adapter has. – Shailesh Kalamkar Apr 05 '12 at 02:55
  • Hi @shailesh_srk - have you tried the explain monkey patch mentioned in the github issue? That provides an explain method, thus getting round the issue. – Chris Kimpton Apr 05 '12 at 10:10
  • No, I have not tried it as patching is not good approach and it is patching in ActiveRecord. I am thinking to use mysql2 adapter instead of jdbcmysql in database.yml, what do you think ? Is there any other way ? – Shailesh Kalamkar Apr 05 '12 at 14:29
  • But your using JRuby, right? I think that means your best bet is to use the jdbcmysql adaptor - the mysql2 is native and problematic with JRuby - see here - http://stackoverflow.com/questions/9865450/many-errors-installing-mysql-on-jruby. The patch will be temporary until the gem is updated, its not like its core functionality. – Chris Kimpton Apr 05 '12 at 18:49
  • 1
    Yes, you are right, using mysql2 with Jruby doesn't make sense.We should wait for the gem update.As Jruby is in process of building I hope they will get rid of "undefined method explain" error soon.I think this error is only for development, check development.rb in environments, `config.active_record.auto_explain_threshold_in_seconds = 0.5` I think the error occurs when SQL query takes time more than 0.5.What say ? – Shailesh Kalamkar Apr 06 '12 at 06:47
  • Agreed - not a long term solution – Chris Kimpton Apr 07 '12 at 08:52
0

since AR-JDBC 1.2.x and esp. when using 1.3.x it's fine to keep the configuration as is for JRuby e.g. :

development:
  adapter: mysql2
  database: blog_development
  username: blog
production: 
  adapter: mysql2
  database: blog
  pool: 50
# ... etc

than in the Gemfile simply limit gems such as mysql2 to MRI and jdbc-mysql to JRuby :

source 'https://rubygems.org'

gem 'rails', '~> 3.2.17'

gem 'mysql2', :platform => :mri
gem 'activerecord-jdbc-adapter'
gem 'jdbc-mysql', :platform => :jruby

# ...

p.s. this q seems to be generating a lot of traffic for the repo, thus added an up-to-date answer.

kares
  • 7,076
  • 1
  • 28
  • 38