I just looked at the DBI::FAQ, and there it says:
The DBI, as of version 1.02, does not yet support multi-threading so it would be unsafe to let more than one thread enter the DBI at the same time.
It is expected that some future version of the DBI will at least be thread-safe (but not thread-hot) by automatically blocking threads intering the DBI while it's already in use.
So the answer is no. Your program can have at most one DBI instance alive ("the brain").
Update: perrin points out that my information is old and not completely correct anymore - DBI will try its best and clone every database handle that was created before a thread was spawned, and it will even try its best to prevent you from sharing a database handle across thread boundaries. But there still is the caveat:
But BEWARE, some underlying database APIs (the code the DBD driver uses to talk to the database, often supplied by the database vendor) are not thread safe. If it's not thread safe, then allowing more than one thread to enter the code at the same time may cause subtle/serious problems. In some cases allowing more than one thread to enter the code, even if not at the same time, can cause problems. You have been warned.
Using DBI with perl threads is not yet recommended for production environments. For more information see Things you need to know before programming Perl ithreads
So, the "best" solution is to either have one thread to handle the database connection, or one or more external processes that connect to the database(s).
| [reply] |
| [reply] |
This brings up an interesting and kinda-related question - how is mod_perl2 / Apache::DBI going to handle this if the threaded model is used?
Being right, does not endow the right to be rude; politeness costs nothing. Being unknowing, is not the same as being stupid. Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence. Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.
| [reply] |
So basically, I'm not allowed to use any DBI calls in case DBI is already utilized in another thread ?
| [reply] |