[DB-SIG] Managing My Database Connections

Gerhard Häring gh at ghaering.de
Thu Mar 13 17:00:17 CET 2008


Robert Rawlins - Tamed Technology wrote:
> Essentially I only have a single database attached to my application but 
> I’m looking for the best way to handle its connection so thought I would 
> come and get your thoughts and opinions. 
> 
> Within my applications I’ll have many classes which access the database, 
> I’m wondering to what level I should extract the database connection. [...]

Be sure to wrap the actual "getting the connection object".

> Should I create a new database connection and close it for every method 
> which calls the database? Should I create the connection  to the DB when 
> I construct the class and place the cursor in the self scope? Or should 
> I create an application wide connection to the database and inject the 
> connection into all the classes which require it? [...]

That's what I'd do. But I'd not inject it and rather just get it (like 
with get_con() in my example code).

I've attached example code that shows how DB-API database access can be 
simplified.

-- Gerhard
-------------- next part --------------
A non-text attachment was scrubbed...
Name: db_example.py
Type: text/x-python
Size: 2266 bytes
Desc: not available
Url : http://mail.python.org/pipermail/db-sig/attachments/20080313/62bc6415/attachment.py 


More information about the DB-SIG mailing list