Activerecord connections with xmlrpc


#1

Hi folks,

Im trying to use an xmlrpc server and have it serve data using
activerecord, but every request seems to eat up a mysql database
connection, until I run out with “Too many connections”. I don’t know
enough ruby/rails (or rather ActiveRecord) to figure this one out.

ruby -v
ruby 1.8.4 (2005-12-24) [i686-linux]
rails -v
Rails 1.0.0

code below:
Here is some simplified code exhibiting this behavior:
my server code:

require ‘xmlrpc/server’
require ‘rubygems’
require_gem ‘activerecord’

a standard user table with name, email, etc

class User < ActiveRecord::Base
ActiveRecord::Base.establish_connection(
:adapter => “mysql”,
# etc. etc.
)
end

given a user id get the user name

def get_username(id)
u = User.find(id)
return u.username
end

s = XMLRPC::Server.new(8080)

s.add_handler(“get_username”) do |user_id|
get_username(user_id)
end

s.set_default_handler do |name, *args|
raise XMLRPC::FaultException.new(-99, “Method #{name} missing” +
" or wrong number of parameters!")
end

client code:

require ‘xmlrpc/client’
client = XMLRPC::Client.new(“localhost”,"/RPC2",8080)
begin
param = client.call(“get_username”,123)
puts “Hello #{param}”
rescue XMLRPC::FaultException => e
puts “Error:”
puts e.faultCode
puts “faultString = [#{e.faultString}]”
end

“PragDave” had a recent blog on a related note:
http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/Connections.rdoc
but again being a newbie to ruby/rails, my gymnastics with trying to
put the ActiveRecord::Base.establish_connection()
/ ActiveRecord.connection() method in and out of various wrapper
classes/class variables didn’t pan out.

thanks,
-Mehryar