could you explain why it is owned by other thread, when i am calling it from with only thread i have created,

You have only created one thread, but every process starts with one thread, so now you have two.

My guess is--if you posted the code, we wouldn't have to guess--that you have something like this:

use threads; use DBI; my $dbh = DBI->connect('dbi:DBM:'); ... my $thread = threads->create( \&thread ); ... sub thread { ... $sql = $dbh->prepare($query) || die($dbh->errstr); #55 ... }
  1. When your process starts, it starts with one thread.
  2. Within that thread you create a DB handle;
  3. You then create a(nother) thread that attempts to use that handle.

When the new thread is created, Perl clones (creates a copy of) the perl scalar that contains the DB handle for use by that new thread, but as the DBD driver you're using doesn't have a CLONE method, only the handle is cloned, not the object that lives behind it.

So, when you attempt to use the cloned handle within the new thread, DBI is detecting that it was created for a different thread and is telling you--very clearly--that that isn't going to work.

One workaround would be to move the creation of the handle--ie. the DBI->connect(...) into the thread also. But be aware that it will mean not using DBI from your main thread. And if your thread is short lived and created repeatedly, then it will cost you the process of reconnected each time, which may or may not be a problem.

Please note the "My guess is" up front and take this speculation in knowledge of it. If you posted the code, you'd be more likely to get a definitive answer.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP PCW

In reply to Re: Threads and handle sharing by BrowserUk
in thread Threads and handle sharing by gemoroy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.