Thursday, November 17, 2011

What the hell! Can't serialize a Mysql object with Marshal in Ruby

Okey so got this strange information late in night at about 3.50am and it took more than 2 hrs for me to surrender. uff :D
and this is something that came in picture
`dump': no marshal_dump is defined for class Mysql (TypeError

and this is a small code snippet which will give you this situation
require "rubygems"
require "mysql"

class RandomClass
def initialize
@db_instance = Mysql.real_connect("hostname", "username", "password", "database_name")
end
end

random_class = RandomClass.new
a = Marshal.dump(random_class)

and there you are, so one can't serialize a object with MySql object in it and with the set of finding i can even say that serializing even with yaml is also not possible.

So work around to this that i can suggest is to do a close of live mysql object in the method itself so that at any give time when object will be passed to serialization using Marshal.dump there would be no live MySql object to hinder with serialization :)

try this piece of snippet now

require "rubygems"
require "mysql"

class RandomClass
def initialize
db_instance = Mysql.real_connect("hostname", "username", "password", "database_name")
db_instance.close
end
end

random_class = RandomClass.new
a = Marshal.dump(random_class)


And this should work :)

8 comments:

  1. Hi,

    I am Carlos Espada from Madrid (Spain). I am looking for a developer to develop a clone of this javascript map, with all the layers:

    www.fotocasa.com

    Please, write me and say me if you could develope the map for me, or get an already developed map for me. Clone of i say.

    I know you through "Climente.com"

    Thank you very much.

    ReplyDelete
  2. Thanks Carlos,

    I can look into the requirements but as such am currently occupied with something else. May in a month from now, can look into it, if it suits you.

    -Praveen
    praveen[Dot]kumar[Dot]Sinha[at]gmail[dot]com

    ReplyDelete
  3. Hi Praveen,


    I was facing the issue "no _dump_data is defined for class Mysql2::Result"

    So while i looking for the solution i saw this blog, i think it may be helpful and i started running your codes in my rails console :)

    But i was getting this error , while running "random_class = RandomClass.new"

    Error => "NameError: uninitialized constant RandomClass::Mysql"


    So I'm getting struck at this place. If so you would have find some solution it may be some what helpful for me..

    Thanks Praveen, Have a nice day :)


    Regards,
    Vignesh

    ReplyDelete
    Replies
    1. Hi Vignesh,

      It seems you are not requiring "mysql" and before that it must be available as gem in your pwd of application.

      -Praveen

      Delete
    2. and from your code i can see you are getting error from mysql2 so instead
      of

      db_instance = Mysql.real_connect("hostname", "username", "password", "database_name")

      do

      client = Mysql2::Client.new(:host => "localhost", :username => "root")


      refer it here for more options http://rubydoc.info/gems/mysql2/0.3.14/frames

      Delete
    3. Hi Praveen,

      Thanks for your reply. Sure i will try it out and will reply you.

      Delete
    4. 2.1.0p0 :001 > require "rubygems"
      => false
      2.1.0p0 :002 > require "mysql2"
      => false
      2.1.0p0 :003 > class RandomClass
      2.1.0p0 :004?> def initialize
      2.1.0p0 :005?> client = Mysql2::Client.new(:host => "localhost", :username => "root")
      2.1.0p0 :006?> client.close
      2.1.0p0 :007?> end
      2.1.0p0 :008?> end
      => :initialize
      2.1.0p0 :009 > random_class = RandomClass.new
      Mysql2::Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
      from /home/anand/.rvm/gems/ruby-2.1.0@tomn_upgrade/gems/mysql2-0.3.14/lib/mysql2/client.rb:67:in `connect'
      from /home/anand/.rvm/gems/ruby-2.1.0@tomn_upgrade/gems/mysql2-0.3.14/lib/mysql2/client.rb:67:in `initialize'
      from (irb):5:in `new'
      from (irb):5:in `initialize'
      from (irb):9:in `new'
      from (irb):9
      from /home/anand/.rvm/gems/ruby-2.1.0@tomn_upgrade/gems/railties-4.1.0.beta1/lib/rails/commands/console.rb:90:in `start'
      from /home/anand/.rvm/gems/ruby-2.1.0@tomn_upgrade/gems/railties-4.1.0.beta1/lib/rails/commands/console.rb:9:in `start'
      from /home/anand/.rvm/gems/ruby-2.1.0@tomn_upgrade/gems/railties-4.1.0.beta1/lib/rails/commands/commands_tasks.rb:69:in `console'
      from /home/anand/.rvm/gems/ruby-2.1.0@tomn_upgrade/gems/railties-4.1.0.beta1/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
      from /home/anand/.rvm/gems/ruby-2.1.0@tomn_upgrade/gems/railties-4.1.0.beta1/lib/rails/commands.rb:17:in `'
      from script/rails:6:in `require'
      from script/rails:6:in `'

      Delete
    5. It seems you are either not providing correct host or socket file in MySQL connection parameters.

      Delete

Honda Civic & A/C problems.

Hello Friends, Got a post again on to Honda Civic (The good old favorite commuter of mine). This car has been doing great except for so...