Showing posts with label activerecord. Show all posts
Showing posts with label activerecord. Show all posts

Friday, December 13, 2013

ActiveRecord::ConnectionTimeoutError

If one is getting or started getting error like this
ActiveRecord::ConnectionTimeoutError (could not obtain a database connection within 5.000 seconds (waited 5.000 seconds)):
  activerecord (4.0.0.rc1) lib/active_record/connection_adapters/abstract/connection_pool.rb:190:in `block in wait_poll'
  activerecord (4.0.0.rc1) lib/active_record/connection_adapters/abstract/connection_pool.rb:181:in `loop'
  activerecord (4.0.0.rc1) lib/active_record/connection_adapters/abstract/connection_pool.rb:181:in `wait_poll'
  activerecord (4.0.0.rc1) lib/active_record/connection_adapters/abstract/connection_pool.rb:136:in `block in poll'

Then it is something that is missing in your database.yml
{environment_name}:
  adapter: mysql2
  encoding: utf8
  database: {product_name}_environment_name
  username: username
  password: password
  socket: /var/run/mysqld/mysqld.sock

Add correct values for 'reaping_frequency' and 'pool' in connection parameter and it might look like
{environment_name}:
  adapter: mysql2
  encoding: utf8
  database: {product_name}_environment_name
  pool: 15
  reaping_frequency: 3
  username: username
  password: password
  socket: /var/run/mysqld/mysqld.sock
Options descriptions
  • pool: number indicating size of connection pool (default 5)
  • checkout_timeout: number of seconds to block and wait for a connection before giving up and raising a timeout error (default 5 seconds).
  • reaping_frequency: frequency in seconds to periodically run the Reaper, which attempts to find and close dead connections, which can occur if a programmer forgets to close a connection at the end of a thread or a thread dies unexpectedly. (Default nil, which means don't run the Reaper).
  • dead_connection_timeout: number of seconds from last checkout after which the Reaper will consider a connection reapable. (default 5 seconds).

Knowledge base taken from http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionPool.html

Saturday, May 2, 2009

Pretty urls with name or title in ruby on rails

Hi folks,

Something on which is very old.. might not be very interesting for many of you who are into rails development. I am talking about the pretty-urls the SEO stuff(forget all this, it is for good readability of url).

So what my basic funda that i have gathered from last three years is that, every resource is identified by an identifier in rails(generally called id, in terms of database), but displaying the id doesn't sounds legal to me.. so to hide the ids and to have a nice looking readable

we can have title or name fields in the database's relation(table) and to make use of pretty urls, we make corresponding column like stripped_title or stripped_name.
create a file "make_pretty_url.rb" in initializers in config and append the following code.


puts "===================================================="
puts "Adding engine to make pretty url(s)"
puts "===================================================="
module ActiveRecord
class Base
after_create :make_or_update_pretty_url_name

def make_or_update_pretty_url_name
parent_column = nil
stripped_column = self.attributes.collect {|x| x.to_s.include?("stripped_") ? x : nil}.flatten.compact.first
if stripped_column
parent_column = stripped_column.gsub("stripped_","")
if self.send(parent_column)
self.send("#{stripped_column}=",stripp_it(self.send(parent_column)))
if self.class.find(:all, :conditions => ["#{stripped_column} = ?",self.send(stripped_column)]).length > 0
self.send("#{stripped_column}=","#{self.send(stripped_column)}-#{self.id.to_s}")
end
self.send("save")
end
end
end

def stripp_it(str)
str.strip.downcase.gsub(/[^a-z0-9]/,"-").gsub(/(-)+/,"-").gsub(/(-)+$/,"")
end
end
end


it will automatically create stripped_{column_name} and will take care of duplicates and instead of using find_by_id(), find_by_stripped_{column_name} will be used to get the resource and "make_or_update_pretty_url_name" method will also be available for any manual work too.

Hope this just adds in the knowledge base of stuff, and if there is anything about it, let me know through comments..

Honda Civic & A/C problems.

Hello Friends, Got a post again on to Honda Civic (The good old favorite commuter of mine). This car has been doing great except for so...