views:

593

answers:

3

I want to know if the mysqlplus gem is a better database driver than the common Ruby mysql gem? I used to have some problems in my Rails application, like: ActiveRecord::StatementInvalid: Mysql::Error: MySQL server has gone away

+1  A: 

MySQL server has gone away means either the mysql server has crashed running your query or (more commonly) you sent it a quert that is larger than max_allowed_packet. see http://dev.mysql.com/doc/refman/5.1/en/packet-too-large.html

longneck
the value of max_allowed_packet in my server is 16MB, and I guess that I didn't have big and complex queries.
Lucas Renan
A: 

mysql reaps the connections after a period of inactivity. this is defined in 'wait_timeout'.
Can see this in mysql by:
mysql> show variables like 'wait_timeout'

by default it is 8 hours. you are getting this error as you have established a connection and there have been no queries executed over this connection for this period.

Activerecord has ActiveRecord::Base#verify_active_connections! for this usecase. if you specify reconnect: true in database.yml it will do this automatically.

The above method is executed when we checkout a connection from the connection-pool, it guards against inactivity.

It will not help you if you are running a long-running query and it exceeds the wait_timeout period, Then you may have to increase the timeout variable in mysql. You may also try setting the patch in: http://gist.github.com/238999 This will retry the query on such a error, circumstances may have changed, but the patch is not robust as it does not have a retry count.

deepak
+1  A: 

if you want to only check for 'mysql server has gone away' errors then activerecord is more than sufficient. it is a mature codebase, good enough for most usecases.

checkout http://blog.new-bamboo.co.uk/2010/4/11/automatic-reconnection-of-mysql-connections-in-active-record for more details.

mysqlplus is better when you need concurrency, all the cool boys recommend it :-) but i am not sure if it is production ready.

deepak
DataObjects (used by DataMapper ORM) is also an option, it has the noteworthy objective of providing a consistent api across databases and ruby implementations. it's mysql driver is also async, and is said to be better than mysqlplus. another benefit as i see it is that it is integrated with datamapper and has a good test suite and an active community.
deepak