You are here |
www.darwinweb.net | ||
| | | |
garrettdimon.com
|
|
| | | | A very nice and concise summary of all the lesser-known and under-appreciated benefits of using partials in Rails. There are a lot of alternatives to... | |
| | | |
tenderlovemaking.com
|
|
| | | | File.open('somefile.txt', 'wb') do |fh| # Open the file fh.write "hello world" # Do some work with the file end # Close file when block returns ActiveRecord::Base.establish_connection( :adapter => "sqlite", :database => "path/to/dbfile") connection_handle = ActiveRecord::Base.connection def retrieve_connection_pool(klass) pool = @connection_pools[klass.name] return pool if pool return nil if ActiveRecord::Base == klass retrieve_connection_pool klass.superclass end spec = ActiveRecord::Base.specificiation ActiveRecord::ConnectionPool.open(spec) do |conn| ... end spec = database_a ActiveRecord::ConnectionPool.open(spec) do |conn| User.find_all end spec = database_b ActiveRecord::ConnectionPool.open(spec) do |conn| User.find_all end # Retrieve the connection associated with the current thread, or call # #checkout to obtain one if necessary. # # #connection can be called any number of times; the connection is # held in a hash keyed by the thread id. def connection @reserved_connections[current_connection_id] ||= checkout end def current_connection_id #:nodoc: ActiveRecord::Base.connection_id ||= Thread.current.object_id end def connection_id Thread.current['ActiveRecord::Base.connection_id'] end def connection_id=(connection_id) Thread.current['ActiveRecord::Base.connection_id'] = connection_id end # Returns any connections in use by the current thread back to the pool, # and also returns connections to the pool cached by threads that are no # longer alive. def clear_active_connections! @connection_pools.each_value {|pool| pool.release_connection } end # Signal that the thread is finished with the current connection. # #release_connection releases the connection-thread association # and returns the connection to the pool. def release_connection(with_id = current_connection_id) conn = @reserved_connections.delete(with_id) checkin conn if conn end @queue.wait(@timeout) if(@checked_out.size < @connections.size) next else clear_stale_cached_connections! if @size == @checked_out.size raise ConnectionTimeoutError, "could not obtain a database connection#{" within #{@timeout} seconds" if @timeout}. The max pool size is currently #{@size}; consider increasing it." end end def clear_stale_cached_connections! keys = @reserved_connections.keys - Thread.list.find_all { |t| t.alive? }.map { |thread| thread.object_id } keys.each do |key| checkin @reserved_connections[key] @reserved_connections.delete(key) end end class ConnectionManagement def call env spec = ActiveRecord::Base.spec connection = ActiveRecord::ConnectionPool.open spec ActiveRecord::Base.connection = connection @app.call env connection.close end end Thread.new do spec = ActiveRecord::Base.spec ActiveRecord::ConnectionPool.open(spec) do |connection| ActiveRecord::Base.connection = connection # do some stuff end end | |
| | | |
postmodern.github.io
|
|
| | | | ||
| | | |
dracos.co.uk
|
|
| | My trusty 2011 Macbook Air had started to show its age - a couple of missing keys, the fan being on pretty much all the time - so I looked for a replacement. After some tedious investigation, I settled upon a Dell XPS 9370 from their Outlet store. Windows 10 can run Linux now, so it seemed like an interesting thing to spend some time with, and if it all went sour I could always fully install Linux instead. |