There is a prototype of support for the Java Beans style functionality
in WebKit. It's referred to as Cans. It'll do what you need easily.
What you would need to do is create a class that handles connection
pooling. Maybe MyPoolCan or something. Put the class in the directory
"Cans" under WebKit. The class has to be in a file with the same name
as the class itself, so a file name "MyPoolCan.py".
This class should create a couple of connections when it is
When you need a connection in a servlet, you call self.getCan():
The parameters to getCan are:
ID: the name the can will be referred to in the future. It is used to
find an instance by the next servlet looking for this class.
klass: the name of the file that the class is stored in and the name of
the class itself
storage: The place you want the Can to exist (it's scope). This can
"application": a can with this scope will be instantiated once and
will be available to all servlets
"session": this instance will be stored in the users session
"request": basically used only for this request.
args: Arguments you want to pass to the class' constructor
kwargs: keyword args to pass to the class' constructor.
The ID should be the same for every call for a particular class.
Otherwise, you'll end up with multiple instances of that class (Which
you may want, I dunno)
So, basically, with a connection pooling type Can, you'd want it to be
application scope. the first servlet that calls getCan() will create the
instance of the object and it will be stored in the application object.
getCan will also return that instance to the servlet. You use it as you
need in your servlet. Then when future servlets call getCan(), they
will have that same Can instance returned to them.
The catch here is that you will have to make the Can threadsafe. In
other words, that same global instance of your class might be in use by
a number of different servlets at the same time. So you'll need to have
the class' function that provides a connection be threadsafe.
Here's a untested thought on an implementation:
from Queue import Queue
myqueue = Queue.Queue(self.connectionCount)
for i in range(self.connectionCount):
return self.myqueue.get() #block until a connection is available
def returnConnection(self, connection)
The catch here is that your servlets will have to manually return the
So in your servlet, you'd do:
connCan = self.getCan("ConnCan", "MyConnectionPool")
conn = connCan.getConnection()
<Use the connection>
That's just off the top of my head. THere should be a way to have the
connection returned automatically when a reference to an instance of
ConnectionPool is dereferenced. I'll have to look in to that.
I haven't used Cans much, so please let me know if this works for you
and how it can be improved.
> I discovered Webware yesterday. I've just tried the samples,
> read rapidly some code and searched the mail archives.
> I found a few messages about databases, but it doesn't look
> like there is an actual "right" solution.
> Where should we make the db connection and how to reach it
> from a page ?
> I'd like to understand the whole thing (Webware). I'd like
> to read the code, but I'm rapidly lost. Where should I start
> reading to understand your design ?
> Would you have some UML of it ? (or just some good drawings)
> We could make a deal : I get the explanations I need from you
> and to thank you, I try to turn them in a "develloper guide".
> (As far as I can with my bad English, my mother language is
> French ; but someone could help with the translation).
> I would love to see Webware go on because I prefer your
> approach to Zope's one. Last year, I started to code myself
> and I had transaction, request, reply, ... classes too.
> In such a pattern, I feel like doing Python ; in Zope, I
> feel like doing Visual Basic.
> Hope to hear from you.
> Denis FRERE
> P3B : Club Free-Pytho-Linuxien Caroloregien http://www.p3b.org
> Aragne : Internet - Reseaux - Formations http://www.aragne.com
> Webware-discuss mailing list