JDBC Connection pooling in Rails - Not for ActiveRecord


#1

Hi, does anyone have a recipe for implementing a pure JDBC connection
pool in Rails?

I’ve got a lot of existing Ruby classes that leverages JDBC database
connections and work on resultsets that I’d like to be able to use in a
simple Rails app. Most of these classes work on databases or directory
servers that will never go near a Rails model or ActiveRecord.

All of the howto’s I’ve seen so far seem to be to directed (quite
understandably!) at getting connection pooling working for ActiveRecord,
and it’s something I’ll also probably get running for the small amount
of activerecord based code that I’m using in this app.

What I’d love to be able to do is dip into a pool of persistent
connections and return a single JDBC connection object to pass to any of
my existing library of methods so that I don’t have the overhead of
setting up and tearing down a connection for each Rails controller
action that is accessed.

Anyone know how this could be achieved?

Cheers

John


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#2

activerecord-jdbc lets you obtain a native java connection pool via
JNDI.
i use activerecord-jdbc with commons-dbcp but modified it slightly so
that the java connection pool can be fetched via spring.

if you don’t use activerecord, just use a generic pool library
directly, maybe by writing a small ruby api wrapper around it.

j

On 1 Feb 2009, at 08:46, John S. wrote:

understandably!) at getting connection pooling working for
Anyone know how this could be achieved?


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#3

Thinking out loud here, and I now realise this is actually not really
relevant to Rails at all, I suppose one way this could be accomplished
is to wrap the jdbc connection requests that normally go to my existing
SqlLibrary class:

class SqlConnectionWrapper

def useJNDI
if defined?($servlet_context)
return true
end
false
end

def mydatabase1()
if (useJNDI)
return jndipoolconnection
else
require “libs/SqlLibrary.rb”
sqllib = SqlLibrary.new
return sqllib.connection(“mydatabase1”)
end
end

def mydatabase2()


end
end

where SqlLibrary.connection is defined as:

def connection()
require “rubygems”
require “jdbc/mysql”
require “java”
dburl = “jdbc:mysql://servername:port/database”
Java::com.mysql.jdbc.Driver
begin
conn = java.sql.DriverManager.get_connection(dburl, username,
password)
rescue
puts “Error establishing connection!”
conn = false
end
return conn
end

However, I’m having difficulty in finding examples of how to return a
connection object from a connection pool as would be setup under Tomcat
or Glassfish. My Java knowledge (the sum of which is just about
displayed above) says that it can’t be as straight forward as changing
the connection url, from being a jdbc source to jndi, that is passed to
the get_connection method… can it?

Presuming I setup one or more connection pools as per Arun G.s blog
(http://blogs.sun.com/arungupta/entry/totd_44_jdbc_connection_pooling),
could anyone point me in the right direction in how to retrieve the same
object from such a named jndi connection?

Thanks

John


#4

Thanks Jan. I’ll investigate the use of commons-dbcp.


#5

the problem with activerecord-jdbc (as I understand it) when using
JNDI is that you are still using a connection pool in JNDI to a rails
connection pool (activerecord). I dont think the current version
knows how to route around the rails connection pool. You also need to
ensure the pool: parameter in database.yml is at least the size of the
JNDI pool.

I am setup this way and dont have too many issues , but I do see a lot
of “Broken Pipe” errors that I am still trying to nail down.

Adam

On Sun, Feb 1, 2009 at 8:30 AM, Jan B. removed_email_address@domain.invalid wrote:

On 1 Feb 2009, at 08:46, John S. wrote:

understandably!) at getting connection pooling working for ActiveRecord, and


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email