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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2001-04-09
Ok. Does close() apply to the connection or just the cursor?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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().
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
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.
Ok. Does close() apply to the connection or just the cursor?
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().