Frage

I want to implement multithreading in my code to reduce time. I implemented Thread::Queue, But I'm getting error Because code contains SQL queries. I'm unable access database through thread at same time.

  sub worker{ 
    while($q->queue){
if (exists $href->{$issue}->{'parent'}) {
        foreach my $parent (sort keys %{$href->{$issue}->{'ddts_parent'}}) {
        my $sql_dest1 ="select pkey from jiraissue where ID=(Select Query')";
            my $sth_dest1 = $dbh->prepare($sql_dest1);
            $sth_dest1->execute();

            foreach (my $dest_array = $sth_dest1->fetchrow_array) {
                if ($dest_array ne '') {
------------some code here------------------------------
                        #to retain link between projects and delete Active url.
                        my $remote_key=$dbh->selectrow_array("select Query)");
                        my $temp_ddtsid=$dbh->selectrow_array("select Query')");
                            $jira->update_issue($remote_key,$update);
                        }
                }   
                else {  
                        $jira->update_issue($src_issue,$update);
                    }
                }           
            }
        }       
    } 
    if (exists $href->{$src_issue}->{'ddts_child'}) { 
        foreach my $ddts_child (sort keys %{$href->{$src_issue}->{'ddts_child'}}) { 
            my $sql_dest ="select Query')";         
            my $sth_dest = $dbh->prepare($sql_dest);
            $sth_dest->execute();


            foreach (my $dest_array = $sth_dest->fetchrow_array) {
                if ($dest_array ne '') {

                            $jira->update_issue($src_issue,$update);
                        }
                        #to retain link between projects and delete Active url.
                        my $remote_key=$dbh->selectrow_array("select Query");
                    my $temp_ddtsid=$dbh->selectrow_array("select Query");

        }
    } 

please give me a solution how to implement multithreading and semaphore.

War es hilfreich?

Lösung

From Threads and Thread Safety in the DBI documentation, emphasis mine:

[When a new thread is created, the whole interpreter with all it's data is cloned.]

However, the internal pointer data within the handles will refer to the DBI and drivers in the original interpreter. Using those handles in the new interpreter thread is not safe, so the DBI detects this and croaks on any method call using handles that don't belong to the current thread (except for DESTROY).

Because of this (possibly temporary) restriction, newly created threads must make their own connections to the database. Handles can't be shared across threads.

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 http://www.perlmonks.org/index.pl?node_id=288022

To summarize:

  • In principle, DBI can work in a multi-threaded environment, as long as the underlying driver is thread safe as well.
  • You must create a new connection for each thread. Connections, statement handles, and result handles cannot be shared between threads.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top