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

I'm new to Perl. When my code tried to create new threads, the logger in the sub-thread body threw out exception like this:

thread failed to start: DBD::Oracle::db prepare failed: handle 2 is owned by thread 83e0008 not current thread 8973c18 (handles can't be shared between threads and your driver may need a CLONE method added) at /usr/cisco/packages/perl/perl-5.8.8/lib/site_perl/5.8.8/Log/Log4perl/Appender/DBI.pm line 88.

Plz let me know if it is possible to used DBIAppender in multithread env, and how to make it happen, if anyone on the earth knows. Thanks a lot!

Replies are listed 'Best First'.
Re: Can Log4perl's DBI Appender be used in multithread?
by afoken (Chancellor) on Apr 13, 2011 at 10:33 UTC

    DBI: Threads and Threads Safety

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Can Log4perl's DBI Appender be used in multithread?
by chrestomanci (Priest) on Apr 13, 2011 at 13:32 UTC

    As you discovered, most DBI interfaces don't like it when you create multiple threads and they each try to use the same database handle. This is nothing to to with log4perl.

    Assuming your database will allow multiple clients to connect at once. (Almost all do), then the simple solution is for each thread to have it's own database connection.

    Off the top of my head I don't know how to make the DBI appender create a new connection, but there should be a way do to it, as this will be a common problem.

      Guys thanks a lot for your input. I'll try to find a way to make log4perl get new connection during new threads. But meanwhile can anyone share the experience if he/she happened to meet the same issue or learned about the solution before?
        share the experience if he/she happened to meet the same issue

        Well, duh, mixing DBI and threads in the wrong way causes crashes, exactly those that you experienced. You are not alone.

        Do you feel better now?

        learned about the solution

        Create threads before calling DBI->connect(), as explained in the fine manual.

        I'll try to find a way to make log4perl get new connection during new threads.

        CHANGING DBH CONNECTIONS (POOLING) sounds promising.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)