seamusabshere/upsert — pull request #44 by thbar (2014-11-19 09:07:54+00:00)
Title: Do not keep a hold on connections [WIP!]

Work in progress!

`Upsert.new(connection, ...)` currently caches an indirect reference to the connection in a class variable.

This is a problem in multi-threaded environments where the connection is temporarily taken from a pool and must be fully released, like in this code:

```ruby
ActiveRecord::Base.connection_pool.with_connection do |connection|
upsert = Upsert.new(connection, :pets)
upsert.row({name: 'Jerry'}, gender: 'male')
end
```

With the current cache, the connection is stored then reused later by another thread, after it has been given back to the pool.

This PR removes the cache completely.

Note that it hasn't been tested in production, this is very much a work in progress to improve thread-safety of upsert.

### Points to consider

* I only ran specs with mysql `DB=mysql rspec`
* in my benchmarks, the removal of the cache does not seem to cause performance issues: it usually takes less than 1ms to prepare the merge function without the cache
* the `sequel_spec.rb` is now failing, because the `column_definitions` is not properly loaded. I suspect this is more a test issue than a real issue, but to be investigated.