DB API and thread safety

Daniel Dittmar daniel.dittmar at sap.corp
Fri Jan 20 08:20:30 EST 2006


Robin Haswell wrote:
> Hey guys
> 
> I've been reading http://www.python.org/peps/pep-0249.html and I don't
> quite get what level of thread safety I need for my DB connections.
> 
> If I call db = FOOdb::connect() at the start of my app, and then every
> thread does it's own c = db.cursor() at the top, what level of thread
> safety do I need to avoid threads stepping on each other? Hopefully the
> answer to this question will get me oriented enough to understand the
> other options :-)

Even if you can share the connection between threads, it will be the 
rare database which can actually work concurrently on one connection. 
Most drivers promising "threads may share the module and connections" 
will use a lock so that only one operation is active at a time.

For maximum throughput (at the cost of using more ressources on the 
database), you'd use one connection per thread. If you want to use 
transactions, then it is the only way.

Daniel



More information about the Python-list mailing list