From: Robert Klemme Date: 2004-02-09T18:05:00+09:00 Subject: Re: Multi-threading lesson wanted "Tim Bates" schrieb im Newsbeitrag news:4025CA70.9090202@bates.id.au... > Below is the code I have written. The idea is that other application > code calls SAMS::Database.get_handle{ |handle| handle.execute... } and > this module takes care of the rest. Also, the application's shutdown > code will call SAMS::Database.destroy to cleanly disconnect all the handles. You definitely need some kind of synchronization mechanism that makes accesses to the shared connection pool thread safe. In your case a ConditionVariable will help you with the max connection logic. See section "Logging from multiple threads" for example usage of a ConditionVariable at http://www.rubygarden.org/ruby?MultiThreading I'd implement a Semaphore using a ConditionVariable and a Mutex; that way you can initialize the semaphore with the max connection value and count the semaphore down when you take a connection from the pool and increment it when you put it back. If the semaphore is zero, the next thread trying to decrease the semaphore is put to sleep and will wake up when another thread increments the semaphore. You can as well use the semaphore implementation in the RAA: http://raa.ruby-lang.org/list.rhtml?name=semaphore As an additional note: I would not use module methods for the pool handling. Instead I'd instantiate an instance of the pool. This is IMHO better since the pool is not necessarily a singleton: Just think of an application that needs to access more than one database and hence use more than one connection configuration. Kind regards robert