perlangel22 has asked for the wisdom of the Perl Monks concerning the following question:

I am using a perl script using DBI can i implement multi threading in it ,can anoy one help me to avoid this error
"Thread 5 terminated abnormally: DBD::mysql::db prepare failed: handle 2 is owned by thread 238d010 not current thread 5e24250 (handles can't be shared between threads and your driver may need a CLONE method added) at /var/www/html/app/data/config/pldb_conn/db_conn.pm line 132."

Replies are listed 'Best First'.
Re: Multithreading DBI error
by Corion (Patriarch) on Feb 16, 2015 at 10:22 UTC

    Just like the error message says, it's simple to avoid it. Don't mix threads and DBI, or at least, do not share statement handles between threads.

    One approach to have both, threads and DBI is to create the statement handle in the thread that will be using it instead of creating it outside.

Re: Multithreading DBI error
by Anonymous Monk on Feb 16, 2015 at 19:41 UTC
    Each thread will need to independently acquire its own handle for its own use, then dispose of it, itself. But it is highly unlikely that this will do you any good because the server with whom you are connecting will accept only so many requests and might execute only one per PID. (All of your threads share one PID.) Check the docs of your database server before proceeding too far with this.