Skip to content

Instantly share code, notes, and snippets.

@ColinDKelley
Created August 15, 2014 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ColinDKelley/5c5edd805a1a8776cc5e to your computer and use it in GitHub Desktop.
Save ColinDKelley/5c5edd805a1a8776cc5e to your computer and use it in GitHub Desktop.
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
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