Permalink
Browse files

Attempt to detect more disconnect errors in the mysql2 adapter

Thanks to brianmario for some additional input.  If the
connection object is available, call ping on it to see if
the connection is alive, and assume disconnection if not.
In addition to checking to the exception message, also check
the sql_state, similar to what is done in the JDBC adapter.
Not all disconnect errors have similar SQL states, so continue
to use the exception message as a fallback.
  • Loading branch information...
1 parent 1cbe9bc commit c7d91d60469d5a11d1ff1e3fed79dfb67e5d3f8a @jeremyevans committed May 29, 2012
Showing with 10 additions and 1 deletion.
  1. +2 −0 CHANGELOG
  2. +8 −1 lib/sequel/adapters/mysql2.rb
View
2 CHANGELOG
@@ -1,5 +1,7 @@
=== HEAD
+* Attempt to detect more disconnect errors in the mysql2 adapter (jeremyevans)
+
* Add is_current? and check_current to the migrators, for checking/raising if there are unapplied migrations (pvh, jeremyevans) (#487)
* Add a jdbc subadapter for the Progress database (Michael Gliwinski, jeremyevans)
View
9 lib/sequel/adapters/mysql2.rb
@@ -107,8 +107,15 @@ def database_error_classes
[::Mysql2::Error]
end
+ # If a connection object is available, try pinging it. Otherwise, if the
+ # error is a Mysql2::Error, check the SQL state and exception message for
+ # disconnects.
def disconnect_error?(e, opts)
- super || (e.is_a?(::Mysql2::Error) && MYSQL_DATABASE_DISCONNECT_ERRORS.match(e.message))
+ super ||
+ ((conn = opts[:conn]) && !conn.ping) ||
+ (e.is_a?(::Mysql2::Error) &&
+ (e.sql_state =~ /\A08/ ||
+ MYSQL_DATABASE_DISCONNECT_ERRORS.match(e.message)))
end
# The database name when using the native adapter is always stored in

0 comments on commit c7d91d6

Please sign in to comment.