Passing a pooled database connection down an object model

Graeme Matthew gsmatthew at ozemail.com.au
Mon Jun 2 08:58:48 EDT 2003


Hi all, im at a bit of a freeze at the moment as I am unsure if my theory
(the code has not been written yet) is correct.

I have an app server that has a singleton database class (aggregation). The
database class creates 10 connections and places them in a list.
I have a function called getFreeConnection, this will pop and return the
connection, decreasing the list by 1. The appserver is using pythons
standard ThreadingTCPServer class, I have my own handler class which will
lookup based on the xml request the appropriate management class to forward
it too.

For example if the xml request is for Client then it will dispatch it to x =
ClientManager(xmlData, dbCon). I need to make sure that each thread is using
its own database connection otherwise the mysql server is going to have a
stroke !

I am planning on passing it down to each subclass (not inherited i.e has-a)
to ensure that the same connection is been used throughout the thread, e.g
in ClientManager might have instance of Client created on startup eg x =
Client.Client(dbCon)

It is probably not easy for you to see what im trying to do without code, so
apologies, but has anyone else done this before and if my methodology is
flawed please say criticise the hell out of it :-)

Which leads me too ask: If one uses inheritence then one can simply access a
dbConnection attribute via super but if aggregation (has a) is used how does
the sub class access its initialiser class.

eg:

import client

class ClientManager:

    def __init__(self, dbCon=None):

        self.con = dbCon
        self.client = Client.Client()

Now inside client lets say I have a function

Class Client:

    def getClient(id):
        ??how do I access ClientManager's dbCon attrbute ?
         do I simply pass it in the constructor ?
            e.g. self.client = Client.Client(self.con)

Thanks a mill

Regards

Graeme







More information about the Python-list mailing list