Antares Trader Blog

The universe at your fingertips

Clearing the Connection Pool in DataMapper

Tuesday

Mar 23, 2010

5:00 am

This is one of those thing that I spent too long Googling for and wishing that someone else had written this post. For a number of different adapters, DataObject (the datastore abstraction layer of DataMapper) creates a pool of up to 8 connections to the database server. This pool is thread safe, but not safe across processes when using fork. In this case, one needs to empty the pool before forking. A chat an the IRC channel reviled that this also may be useful if you are getting too many connections. To empty all connection pools:

#!/ruby
DataObjects::Pooling.pools.each do {|pool| pool.dispose}

I encountered this problem when writing Updater, my Job Queue library. The workers get forked off of a master process similar to how Merb and Unicorn work. The Master process hits the database occasionally to anticipate the work load and spin up more workers if necessary. Its connection pool was getting carried over to the children who would use those connections then close them leaving the master process with half open connections. I needed to use the command above before the fork call to prevent this.

edit delete