Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: Transactions across multiple threads

Preliminary:

I have an application which maintains a thread pool of about 100 threads. Each thread can last about 1-30 seconds before a new task replaces it. When a thread ends, that thread will almost always will result in inserting 1-3 records into a table, this table is used by all of the threads. Right now, no transactional support exists, but I am trying to add that now. Also, the table in question is InnoDB. So...

Goal

I want to implement a transaction for this. The rules for whether or not this transaction commits or rollback reside in the main thread. Basically there is a simple function that will return a boolean.

  1. Can I implement a transaction across multiple connections?
  2. If not, can multiple threads share the same connection? (Note: there are a LOT of inserts going on here, and that is a requirement).
like image 876
Zombies Avatar asked Mar 22 '10 20:03

Zombies


1 Answers

1) No, a transaction is limited to a single DB connection.

2) Yes, a connection (and transaction) can be shared across multiple threads.

like image 192
Kevin Crowell Avatar answered Oct 14 '22 20:10

Kevin Crowell