in reply to databases and multithreading

If you instantiate and use separate database handles in each thread and use thread-safe DBD drivers with recent client libraries (like recent DBD::mysql and libmysqlclient) you can make something like this work. But since your code just seems to share handles over different threads, chances of it working at all with any driver are slim.