too many connection


I actually have a mysql error in my ruby script.
After several requests I have the "too many connections" error.
But I don't why because I created a queue that runs 5 threads. Every
thread execute many mysql requests.
I limited the queue to 5 threads to not overload mysql. But it seems
that doesn't work. Here is an example of my script.
I precise that I use activerecord.

threads = (1..5).map do |i| do |q|
      until ( q == (tab = q.deq))

10.times do |t|
  30.times do |s|
  threads.size.times { QUEUE.enq QUEUE}
  threads.each {|u| u.join}

I hope someone can help me