Rails Database Connection with Block

I found myself needing to very quickly connect to an alternate database that was defined in my database.yml like so:

alternate_data:
    adapter: mysql
    database: alternate_database
    username: user
    password: pass
    host: localhost

I was accessing it with something similar to the following:

ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations["#{state}_data"])
results = connection.execute description_params[:extraction_sql];
#This wasn't actually hard coded, but this is just an example
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations["development"])

The problems with this was that I had to repeat this ugly code everywhere, and if there was an exception thrown while accessing the alternate database it would not revert back to the original before exiting.  So the next request to the server thought the default database was the alternate database.  What I decided would be useful would be to have an “establish_connection” method that accepted a block and handle these issues.

I am going to write about this because I planned to before realizing that in Rails 2.2 the exact same issue is solved already with a much better implementation (of course).  You can find how to solve this problem very elegantly in Rails 2.2 at http://www.igvita.com/2008/10/27/scaling-activerecord-with-mysqlplus/.

My implementation is much simpler and I hope that anybody stuck with Rails 2.1 (like me for now) or any previous version can use it temporarily until they can upgrade in the future.

I just wanted a simple block establish_connection so I could write code like the following:

   establish_temporal_database_connection("#{state}_data") do |connection|
      results = connection.execute description_params[:extraction_sql];
    end

It would reset the old database connection if something went wrong or when the block was over.

I’m just going to list a simple module that does this, if anybody wants this in plugin form that actually overrides establish_connection then it isn’t that difficult to turn this into that:

module ConnectionWithBlock  
  def self.extended(object)
    class << object
        extend ClassMethods
    end
  end
  module ClassMethods
    def establish_temporal_database_connection(configuration)
       #save the old connection
       previous_connection = ActiveRecord::Base.connection
       #establish connection handling a possible exception and going back to original connection
       begin
              ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[configuration.to_s])
              yield(ActiveRecord::Base.connection)
       rescue Exception => e
              raise e
       ensure
              ActiveRecord::Base.establish_connection(ConnectionSpecification.new(previous_connection.config, previous_connection.adapter_method))
       end   
     end
  end
end

Chase

0 comments ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment

Start uga_insert_html_once: head, Footer hooked: HTML inserted: Location is HEAD Start uga_get_option: footer_hooked uga_options: array ( 'internal_domains' => 'chase.ratchetsoftware.com', 'account_id' => 'UA-4748586-2', 'enable_tracker' => true, 'track_adm_pages' => false, 'ignore_users' => true, 'max_user_level' => '8', 'footer_hooked' => true, 'filter_content' => true, 'filter_comments' => true, 'filter_comment_authors' => true, 'track_ext_links' => true, 'prefix_ext_links' => '/outgoing/', 'track_files' => true, 'prefix_file_links' => '/downloads/', 'track_extensions' => 'gif,jpg,jpeg,bmp,png,pdf,mp3,wav,phps,zip,gz,tar,rar,jar,exe,pps,ppt,xls,doc', 'track_mail_links' => true, 'prefix_mail_links' => '/mailto/', 'debug' => true, 'check_updates' => true, 'version_sent' => '1.6.0', 'advanced_config' => false, ) Ending uga_get_option: footer_hooked (1) End uga_insert_html Ending uga_wp_head_track: Start uga_filter:

I found myself needing to very quickly connect to an alternate database that was defined in my database.yml like so:

e714a487c1f214bb19263f7027b30411000

I was accessing it with something similar to the following:

e714a487c1f214bb19263f7027b30411001

The problems with this was that I had to repeat this ugly code everywhere, and if there was an exception thrown while accessing the alternate database it would not revert back to the original before exiting.  So the next request to the server thought the default database was the alternate database.  What I decided would be useful would be to have an “establish_connection” method that accepted a block and handle these issues.

I am going to write about this because I planned to before realizing that in Rails 2.2 the exact same issue is solved already with a much better implementation (of course).  You can find how to solve this problem very elegantly in Rails 2.2 at http://www.igvita.com/2008/10/27/scaling-activerecord-with-mysqlplus/.

My implementation is much simpler and I hope that anybody stuck with Rails 2.1 (like me for now) or any previous version can use it temporarily until they can upgrade in the future.

I just wanted a simple block establish_connection so I could write code like the following:

e714a487c1f214bb19263f7027b30411002

It would reset the old database connection if something went wrong or when the block was over.

I’m just going to list a simple module that does this, if anybody wants this in plugin form that actually overrides establish_connection then it isn’t that difficult to turn this into that:

e714a487c1f214bb19263f7027b30411003

Chase

Start uga_in_feed Ending uga_in_feed: Start uga_track_user Start uga_get_option: ignore_users uga_options: array ( 'internal_domains' => 'chase.ratchetsoftware.com', 'account_id' => 'UA-4748586-2', 'enable_tracker' => true, 'track_adm_pages' => false, 'ignore_users' => true, 'max_user_level' => '8', 'footer_hooked' => true, 'filter_content' => true, 'filter_comments' => true, 'filter_comment_authors' => true, 'track_ext_links' => true, 'prefix_ext_links' => '/outgoing/', 'track_files' => true, 'prefix_file_links' => '/downloads/', 'track_extensions' => 'gif,jpg,jpeg,bmp,png,pdf,mp3,wav,phps,zip,gz,tar,rar,jar,exe,pps,ppt,xls,doc', 'track_mail_links' => true, 'prefix_mail_links' => '/mailto/', 'debug' => true, 'check_updates' => true, 'version_sent' => '1.6.0', 'advanced_config' => false, ) Ending uga_get_option: ignore_users (1) Start uga_get_option: max_user_level uga_options: array ( 'internal_domains' => 'chase.ratchetsoftware.com', 'account_id' => 'UA-4748586-2', 'enable_tracker' => true, 'track_adm_pages' => false, 'ignore_users' => true, 'max_user_level' => '8', 'footer_hooked' => true, 'filter_content' => true, 'filter_comments' => true, 'filter_comment_authors' => true, 'track_ext_links' => true, 'prefix_ext_links' => '/outgoing/', 'track_files' => true, 'prefix_file_links' => '/downloads/', 'track_extensions' => 'gif,jpg,jpeg,bmp,png,pdf,mp3,wav,phps,zip,gz,tar,rar,jar,exe,pps,ppt,xls,doc', 'track_mail_links' => true, 'prefix_mail_links' => '/mailto/', 'debug' => true, 'check_updates' => true, 'version_sent' => '1.6.0', 'advanced_config' => false, ) Ending uga_get_option: max_user_level (8) Tracking user with level Ending uga_track_user: 1 Calling preg_replace_callback: ]*?)href\s*=\s*['"](.*?)['"]([^>]*)>(.*?) Start uga_preg_callback: Array Get tracker for full url Start uga_track_full_url: www.igvita.com/2008/10/27/scaling-activerecord-with-mysqlplus/ Start uga_is_url_internal: www.igvita.com/2008/10/27/scaling-activerecord-with-mysqlplus/ Start uga_get_option: internal_domains uga_options: array ( 'internal_domains' => 'chase.ratchetsoftware.com', 'account_id' => 'UA-4748586-2', 'enable_tracker' => true, 'track_adm_pages' => false, 'ignore_users' => true, 'max_user_level' => '8', 'footer_hooked' => true, 'filter_content' => true, 'filter_comments' => true, 'filter_comment_authors' => true, 'track_ext_links' => true, 'prefix_ext_links' => '/outgoing/', 'track_files' => true, 'prefix_file_links' => '/downloads/', 'track_extensions' => 'gif,jpg,jpeg,bmp,png,pdf,mp3,wav,phps,zip,gz,tar,rar,jar,exe,pps,ppt,xls,doc', 'track_mail_links' => true, 'prefix_mail_links' => '/mailto/', 'debug' => true, 'check_updates' => true, 'version_sent' => '1.6.0', 'advanced_config' => false, ) Ending uga_get_option: internal_domains (chase.ratchetsoftware.com) Checking hostname chase.ratchetsoftware.com Ending uga_is_url_internal: Get tracker for external URL Start uga_track_external_url: www.igvita.com/2008/10/27/scaling-activerecord-with-mysqlplus/ Start uga_get_option: track_ext_links uga_options: array ( 'internal_domains' => 'chase.ratchetsoftware.com', 'account_id' => 'UA-4748586-2', 'enable_tracker' => true, 'track_adm_pages' => false, 'ignore_users' => true, 'max_user_level' => '8', 'footer_hooked' => true, 'filter_content' => true, 'filter_comments' => true, 'filter_comment_authors' => true, 'track_ext_links' => true, 'prefix_ext_links' => '/outgoing/', 'track_files' => true, 'prefix_file_links' => '/downloads/', 'track_extensions' => 'gif,jpg,jpeg,bmp,png,pdf,mp3,wav,phps,zip,gz,tar,rar,jar,exe,pps,ppt,xls,doc', 'track_mail_links' => true, 'prefix_mail_links' => '/mailto/', 'debug' => true, 'check_updates' => true, 'version_sent' => '1.6.0', 'advanced_config' => false, ) Ending uga_get_option: track_ext_links (1) Tracking external links enabled Start uga_get_option: prefix_ext_links uga_options: array ( 'internal_domains' => 'chase.ratchetsoftware.com', 'account_id' => 'UA-4748586-2', 'enable_tracker' => true, 'track_adm_pages' => false, 'ignore_users' => true, 'max_user_level' => '8', 'footer_hooked' => true, 'filter_content' => true, 'filter_comments' => true, 'filter_comment_authors' => true, 'track_ext_links' => true, 'prefix_ext_links' => '/outgoing/', 'track_files' => true, 'prefix_file_links' => '/downloads/', 'track_extensions' => 'gif,jpg,jpeg,bmp,png,pdf,mp3,wav,phps,zip,gz,tar,rar,jar,exe,pps,ppt,xls,doc', 'track_mail_links' => true, 'prefix_mail_links' => '/mailto/', 'debug' => true, 'check_updates' => true, 'version_sent' => '1.6.0', 'advanced_config' => false, ) Ending uga_get_option: prefix_ext_links (/outgoing/) Ending uga_track_external_url: www.igvita.com/2008/10/27/scaling-activerecord-with-mysqlplus/ Ending uga_track_full_url: /outgoing/www.igvita.com/2008/10/27/scaling-activerecord-with-mysqlplus/ Adding onclick attribute for /outgoing/www.igvita.com/2008/10/27/scaling-activerecord-with-mysqlplus/ Ending uga_preg_callback: http://www.igvita.com/2008/10/27/scaling-activerecord-with-mysqlplus/ Ending uga_filter:

I found myself needing to very quickly connect to an alternate database that was defined in my database.yml like so:

e714a487c1f214bb19263f7027b30411000

I was accessing it with something similar to the following:

e714a487c1f214bb19263f7027b30411001

The problems with this was that I had to repeat this ugly code everywhere, and if there was an exception thrown while accessing the alternate database it would not revert back to the original before exiting.  So the next request to the server thought the default database was the alternate database.  What I decided would be useful would be to have an “establish_connection” method that accepted a block and handle these issues.

I am going to write about this because I planned to before realizing that in Rails 2.2 the exact same issue is solved already with a much better implementation (of course).  You can find how to solve this problem very elegantly in Rails 2.2 at http://www.igvita.com/2008/10/27/scaling-activerecord-with-mysqlplus/.

My implementation is much simpler and I hope that anybody stuck with Rails 2.1 (like me for now) or any previous version can use it temporarily until they can upgrade in the future.

I just wanted a simple block establish_connection so I could write code like the following:

e714a487c1f214bb19263f7027b30411002

It would reset the old database connection if something went wrong or when the block was over.

I’m just going to list a simple module that does this, if anybody wants this in plugin form that actually overrides establish_connection then it isn’t that difficult to turn this into that:

e714a487c1f214bb19263f7027b30411003

Chase

Start uga_wp_footer_track: Start uga_get_tracker Start uga_in_feed Ending uga_in_feed: Start uga_track_user Start uga_get_option: ignore_users uga_options: array ( 'internal_domains' => 'chase.ratchetsoftware.com', 'account_id' => 'UA-4748586-2', 'enable_tracker' => true, 'track_adm_pages' => false, 'ignore_users' => true, 'max_user_level' => '8', 'footer_hooked' => true, 'filter_content' => true, 'filter_comments' => true, 'filter_comment_authors' => true, 'track_ext_links' => true, 'prefix_ext_links' => '/outgoing/', 'track_files' => true, 'prefix_file_links' => '/downloads/', 'track_extensions' => 'gif,jpg,jpeg,bmp,png,pdf,mp3,wav,phps,zip,gz,tar,rar,jar,exe,pps,ppt,xls,doc', 'track_mail_links' => true, 'prefix_mail_links' => '/mailto/', 'debug' => true, 'check_updates' => true, 'version_sent' => '1.6.0', 'advanced_config' => false, ) Ending uga_get_option: ignore_users (1) Start uga_get_option: max_user_level uga_options: array ( 'internal_domains' => 'chase.ratchetsoftware.com', 'account_id' => 'UA-4748586-2', 'enable_tracker' => true, 'track_adm_pages' => false, 'ignore_users' => true, 'max_user_level' => '8', 'footer_hooked' => true, 'filter_content' => true, 'filter_comments' => true, 'filter_comment_authors' => true, 'track_ext_links' => true, 'prefix_ext_links' => '/outgoing/', 'track_files' => true, 'prefix_file_links' => '/downloads/', 'track_extensions' => 'gif,jpg,jpeg,bmp,png,pdf,mp3,wav,phps,zip,gz,tar,rar,jar,exe,pps,ppt,xls,doc', 'track_mail_links' => true, 'prefix_mail_links' => '/mailto/', 'debug' => true, 'check_updates' => true, 'version_sent' => '1.6.0', 'advanced_config' => false, ) Ending uga_get_option: max_user_level (8) Tracking user with level Ending uga_track_user: 1 Start uga_get_option: account_id uga_options: array ( 'internal_domains' => 'chase.ratchetsoftware.com', 'account_id' => 'UA-4748586-2', 'enable_tracker' => true, 'track_adm_pages' => false, 'ignore_users' => true, 'max_user_level' => '8', 'footer_hooked' => true, 'filter_content' => true, 'filter_comments' => true, 'filter_comment_authors' => true, 'track_ext_links' => true, 'prefix_ext_links' => '/outgoing/', 'track_files' => true, 'prefix_file_links' => '/downloads/', 'track_extensions' => 'gif,jpg,jpeg,bmp,png,pdf,mp3,wav,phps,zip,gz,tar,rar,jar,exe,pps,ppt,xls,doc', 'track_mail_links' => true, 'prefix_mail_links' => '/mailto/', 'debug' => true, 'check_updates' => true, 'version_sent' => '1.6.0', 'advanced_config' => false, ) Ending uga_get_option: account_id (UA-4748586-2) Ending uga_get_tracker: Start uga_insert_html_once: footer, Footer hooked: HTML inserted: Location is FOOTER Inserting HTML End uga_insert_html Ending uga_wp_footer_track: Start uga_shutdown Start uga_in_feed Ending uga_in_feed: Start uga_track_user Start uga_get_option: ignore_users uga_options: array ( 'internal_domains' => 'chase.ratchetsoftware.com', 'account_id' => 'UA-4748586-2', 'enable_tracker' => true, 'track_adm_pages' => false, 'ignore_users' => true, 'max_user_level' => '8', 'footer_hooked' => true, 'filter_content' => true, 'filter_comments' => true, 'filter_comment_authors' => true, 'track_ext_links' => true, 'prefix_ext_links' => '/outgoing/', 'track_files' => true, 'prefix_file_links' => '/downloads/', 'track_extensions' => 'gif,jpg,jpeg,bmp,png,pdf,mp3,wav,phps,zip,gz,tar,rar,jar,exe,pps,ppt,xls,doc', 'track_mail_links' => true, 'prefix_mail_links' => '/mailto/', 'debug' => true, 'check_updates' => true, 'version_sent' => '1.6.0', 'advanced_config' => false, ) Ending uga_get_option: ignore_users (1) Start uga_get_option: max_user_level uga_options: array ( 'internal_domains' => 'chase.ratchetsoftware.com', 'account_id' => 'UA-4748586-2', 'enable_tracker' => true, 'track_adm_pages' => false, 'ignore_users' => true, 'max_user_level' => '8', 'footer_hooked' => true, 'filter_content' => true, 'filter_comments' => true, 'filter_comment_authors' => true, 'track_ext_links' => true, 'prefix_ext_links' => '/outgoing/', 'track_files' => true, 'prefix_file_links' => '/downloads/', 'track_extensions' => 'gif,jpg,jpeg,bmp,png,pdf,mp3,wav,phps,zip,gz,tar,rar,jar,exe,pps,ppt,xls,doc', 'track_mail_links' => true, 'prefix_mail_links' => '/mailto/', 'debug' => true, 'check_updates' => true, 'version_sent' => '1.6.0', 'advanced_config' => false, ) Ending uga_get_option: max_user_level (8) Tracking user with level Ending uga_track_user: 1 Footer hook was executed Start uga_get_option: footer_hooked uga_options: array ( 'internal_domains' => 'chase.ratchetsoftware.com', 'account_id' => 'UA-4748586-2', 'enable_tracker' => true, 'track_adm_pages' => false, 'ignore_users' => true, 'max_user_level' => '8', 'footer_hooked' => true, 'filter_content' => true, 'filter_comments' => true, 'filter_comment_authors' => true, 'track_ext_links' => true, 'prefix_ext_links' => '/outgoing/', 'track_files' => true, 'prefix_file_links' => '/downloads/', 'track_extensions' => 'gif,jpg,jpeg,bmp,png,pdf,mp3,wav,phps,zip,gz,tar,rar,jar,exe,pps,ppt,xls,doc', 'track_mail_links' => true, 'prefix_mail_links' => '/mailto/', 'debug' => true, 'check_updates' => true, 'version_sent' => '1.6.0', 'advanced_config' => false, ) Ending uga_get_option: footer_hooked (1) Start uga_get_option: debug uga_options: array ( 'internal_domains' => 'chase.ratchetsoftware.com', 'account_id' => 'UA-4748586-2', 'enable_tracker' => true, 'track_adm_pages' => false, 'ignore_users' => true, 'max_user_level' => '8', 'footer_hooked' => true, 'filter_content' => true, 'filter_comments' => true, 'filter_comment_authors' => true, 'track_ext_links' => true, 'prefix_ext_links' => '/outgoing/', 'track_files' => true, 'prefix_file_links' => '/downloads/', 'track_extensions' => 'gif,jpg,jpeg,bmp,png,pdf,mp3,wav,phps,zip,gz,tar,rar,jar,exe,pps,ppt,xls,doc', 'track_mail_links' => true, 'prefix_mail_links' => '/mailto/', 'debug' => true, 'check_updates' => true, 'version_sent' => '1.6.0', 'advanced_config' => false, ) Ending uga_get_option: debug (1) -->