Menu

Threads/Connections/Cursors

Help
Anonymous
2001-04-09
2012-09-19
  • Anonymous

    Anonymous - 2001-04-09

    MySQL: 3.23.35
    MySQLdb: 0.3.5
    Python: 1.5.2
    O/S: Linux RH7.0

    I want several threads to insert records into the same table (which has an auto-increment column). Each thread then needs to update the records it created based on insert_id().

    I think the threads shouldn't share a connection because insert_id() may only work per connection but I'm not sure.

    I tried having each thread create its own connection. But I soon ran out of mysql connections as it appears the connection isn't being released when the thread ends.

    So, will insert_id() work for each thread if the connection is shared? If not, is there something the thread should be doing to close its connection when it ends?

    Thanks.

    2. Create a connection in each thread.

     
    • Andy Dustman

      Andy Dustman - 2001-04-09

      Yes, each thread should have it's own connection: Two threads cannot use the same connection at the same time. Make sure you close() the connection when the thread ends and you shouldn't have a problem.

       
      • Anonymous

        Anonymous - 2001-04-09

        Ok. Does close() apply to the connection or just the cursor?

         
        • Andy Dustman

          Andy Dustman - 2001-04-09

          close() on the cursor only closes the cursor, which for MySQL effectively does nothing. You need to call close() on the connection. i.e. if db is your MySQL connection object, and c=db.cursor(), do db.close().

           

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.