Ruby works but not JRuby - when using MySQL Driver

V

Venks

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:in `require'
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,
 
C

Charles Oliver Nutter

Venks said:
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:in `require'
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
 
C

Charles Oliver Nutter

Charles said:
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
 
V

Venks

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
 
C

Charles Oliver Nutter

Venks said:
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
 
V

Venks

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,
 
C

Charles Oliver Nutter

Venks said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top