Created
August 15, 2014 23:13
-
-
Save ColinDKelley/5c5edd805a1a8776cc5e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mysql2' | |
module Invoca | |
module Mysql2AdapterKillOnTimeout | |
def kill(thread_id) | |
@connection.query("KILL #{thread_id}") | |
end | |
def execute(sql, name = nil) | |
super | |
rescue ActiveRecord::StatementInvalid | |
ExceptionHandling.ensure_safe "canceling query for thread id #{@connection.thread_id} - query may still be running" do | |
unless active? | |
old_thread_id = @connection.thread_id | |
reconnect! | |
kill(old_thread_id) | |
end | |
end | |
raise | |
end | |
end | |
end | |
module ActiveRecord | |
module ConnectionAdapters | |
class Mysql2Adapter | |
prepend Invoca::Mysql2AdapterKillOnTimeout | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mysql2' | |
module ActiveRecord | |
module ConnectionAdapters | |
class Mysql2Adapter | |
def kill(thread_id) | |
@connection.query("KILL #{thread_id}") | |
end | |
def execute(sql, name = nil) | |
# make sure we carry over any changes to ActiveRecord::Base.default_timezone that have been | |
# made since we established the connection | |
@connection.query_options[:database_timezone] = ActiveRecord::Base.default_timezone | |
if name == :skip_logging | |
@connection.query(sql) | |
else | |
log(sql, name) { non_nil_connection.query(sql) } | |
end | |
rescue ActiveRecord::StatementInvalid => exception | |
ExceptionHandling.ensure_safe "canceling query for thread id #{@connection.thread_id} - query may still be running" do | |
unless active? | |
old_thread_id = @connection.thread_id | |
reconnect! | |
kill(old_thread_id) | |
end | |
end | |
raise | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment