DB API and thread safety

Discussion in 'Python' started by Robin Haswell, Jan 20, 2006.

  1. 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 :)

    Cheers

    -Rob
     
    Robin Haswell, Jan 20, 2006
    #1
    1. Advertising

  2. Robin Haswell

    Matt Goodall Guest

    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 :)


    Assuming the cursor created in the thread is never accessed by another
    thread then you need a dbapi module that supports threadsafety level 2 -
    "threads may share the module and connections".

    - Matt

    --
    __
    / \__ Matt Goodall, Pollenation Internet Ltd
    \__/ \ w: http://www.pollenation.net
    __/ \__/ e:
    / \__/ \ t: +44 (0)113 2252500
    \__/ \__/
    / \ Any views expressed are my own and do not necessarily
    \__/ reflect the views of my employer.
     
    Matt Goodall, Jan 20, 2006
    #2
    1. Advertising

  3. 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
     
    Daniel Dittmar, Jan 20, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGlmZmlkZW50?=

    Thread-safety and Singleton methods

    =?Utf-8?B?RGlmZmlkZW50?=, Jan 13, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    526
    Karl Seguin
    Jan 13, 2005
  2. =?Utf-8?B?U2ltbw==?=

    ASP.net and thread safety

    =?Utf-8?B?U2ltbw==?=, Jan 13, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    645
    =?Utf-8?B?U0VC?=
    Jan 13, 2006
  3. Casper
    Replies:
    1
    Views:
    366
    Nick Coghlan
    Nov 20, 2004
  4. Donnie Leen
    Replies:
    2
    Views:
    401
    Greg Ewing
    Nov 25, 2004
  5. pek
    Replies:
    2
    Views:
    1,120
Loading...

Share This Page