Ruby works but not JRuby - when using MySQL Driver

Here is the simple Ruby program that works with “Ruby” but gives an
error with “JRuby”.

file: connection.rb

================================================
#!/usr/bin/ruby -w

Use Ruby MySQL module to test connection information

require “mysql”

begin

dbh = Mysql.real_connect(“mars”, “csread”, “csread”, “csapp”)

puts "Server Info: " + dbh.get_server_info

rescue Mysql::Error => e

puts “Error code: #{e.errno}”
puts “Error message: #{e.error}”
puts “Error SQLSTATE: #{e.sqlstate}” if e.respond_to?(“sqlstate”)

ensure

dbh.close if dbh

end

Below is the error while running with JRuby:

connection.rb:4: /usr/lib/ruby/gems/1.8/gems/mysql-2.7/mysql.so:0:
Invalid char \177' in expression (SyntaxError) from connection.rb:4:inrequire’
from connection.rb:4

Is this a compatibility issue between JRuby and MySQL driver writting
in Ruby? Is this expected behaviour or can this be addressed?

Thanks,

I’m not sure if this is the case, but seems like it might be. JRuby
doesn’t support C extensions. I don’t know if it can be addressed with
that driver. You might be able to use the Java MySQL Driver, keep in
mind the free one is GPL.

If I’m wrong about anything, someone correct me.

Joe

Venks [email protected] wrote: Here is the simple Ruby
program that works with “Ruby” but gives an
error with “JRuby”.

file: connection.rb

================================================
#!/usr/bin/ruby -w

Use Ruby MySQL module to test connection information

require “mysql”

begin

dbh = Mysql.real_connect(“mars”, “csread”, “csread”, “csapp”)

puts "Server Info: " + dbh.get_server_info

rescue Mysql::Error => e

puts “Error code: #{e.errno}”
puts “Error message: #{e.error}”
puts “Error SQLSTATE: #{e.sqlstate}” if e.respond_to?(“sqlstate”)

ensure

dbh.close if dbh

end

Below is the error while running with JRuby:

connection.rb:4: /usr/lib/ruby/gems/1.8/gems/mysql-2.7/mysql.so:0:
Invalid char \177' in expression (SyntaxError) from connection.rb:4:inrequire’
from connection.rb:4

Is this a compatibility issue between JRuby and MySQL driver writting
in Ruby? Is this expected behaviour or can this be addressed?

Thanks,

Charles Oliver N. wrote:

The pure Ruby MySQL driver should be fine, but it looks like you’ve got
the non-pure-Ruby driver and it’s trying to load it. There’s a known bug
in JRuby that it will try to load .so files as Ruby source (oops), but
no amount of magic will make it load native extensions (yet).

Make sure you’re using a pure Ruby MySQL driver if you want to access SQL.

Or of course you could use JDBC directly. The API is pretty clean, a lot
cleaner than many Java APIs.

  • charlie

Hi,

I installed this driver using “gem install mysql”. I suppose this will
install MySQL/Ruby driver but I need Ruby/MySQL which is pure Ruby
MySQL driver. Is that correct?

http://dev.mysql.com/downloads/ruby.html

Venks wrote:

Below is the error while running with JRuby:

connection.rb:4: /usr/lib/ruby/gems/1.8/gems/mysql-2.7/mysql.so:0:
Invalid char \177' in expression (SyntaxError) from connection.rb:4:inrequire’
from connection.rb:4

Is this a compatibility issue between JRuby and MySQL driver writting
in Ruby? Is this expected behaviour or can this be addressed?

The pure Ruby MySQL driver should be fine, but it looks like you’ve got
the non-pure-Ruby driver and it’s trying to load it. There’s a known bug
in JRuby that it will try to load .so files as Ruby source (oops), but
no amount of magic will make it load native extensions (yet).

Make sure you’re using a pure Ruby MySQL driver if you want to access
SQL.

  • Charlie

I am not able to make the pure Ruby Mysql driver work. I am getting
the following error and couldn’t figure out why this error is showing
up. MySQL manual mentions something about old client and password but
I am using MySQL 5.1 and I have no other version installed previously.
Of course I never had any issues with the Native Ruby MySQL driver.
Any ideas?

Error message: Client does not support authentication protocol
requested by server; consider upgrading MySQL client

According to the information here, pure Ruby MySQL doesn’t work with
4.1 or 5.x. If that’s true, I am not sure if pure Ruby MySQL is of any
use.

http://wiki.rubyonrails.org/rails/pages/MySQL

Venks wrote:

According to the information here, pure Ruby MySQL doesn’t work with
4.1 or 5.x. If that’s true, I am not sure if pure Ruby MySQL is of any
use.

http://wiki.rubyonrails.org/rails/pages/MySQL

That’s unusual, since rails works fine with the pure-ruby mysql driver.
However, if you’re looking to use mysql directly, perhaps you should
look into using JDBC instead? I’m sure we can find a good example if
you’re interesed in that. JDBC provides a uniform interface to basically
any database.

  • Charlie

Yes. Please send me any examples you may have. I already got one from
this mailing-list which I haven’t tried yet. But don’t mind receiving
few from you.

At this stage, JDBC seems to like the best option given that I need to
to communicate 2 different databases and one of them has only JDBC
interface and other is MySQL for which I will use JDBC too. Since JDBC
is my only option, JRuby will be the default choice.

Thanks,

Venks wrote:

Yes. Please send me any examples you may have. I already got one from
this mailing-list which I haven’t tried yet. But don’t mind receiving
few from you.

At this stage, JDBC seems to like the best option given that I need to
to communicate 2 different databases and one of them has only JDBC
interface and other is MySQL for which I will use JDBC too. Since JDBC
is my only option, JRuby will be the default choice.

Here’s a very simple example to get you into the JDBC frame of mind.
This is basically a portion of the code shown at

http://www.cs.unc.edu/Courses/comp118/docs/lessons/java/java_jdbc/jdbcServletMysql.html

…translated to Ruby.

url = “jdbc:mysql://sparrow.cs.unc.edu/jbsdb”
query = “SELECT * FROM Person WHERE owner = ‘jbs’”

begin
Java::com.mysql.jdbc.Driver # make sure driver class is loaded

conn = java.sql.DriverManager.get_connection(
url, “your_mysgl_login”, “your_mysql_password”)
stmt = con.create_statement
rs = stmt.execute_query (query)

print_result_set(resp, rs)
ensure
rs.close
stmt.close
conn.close
end

JDBC is a pretty clean API, and even cleaner in Ruby.

  • Charlie

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs