Monday 15 April 2013

multithreading - DBI and fork don't work on Windows -


I used this article: to write code, with the fork with DBI. It works on Linux but does not work on Windows XP. I am using Active Pearl v5.10.0 MSWin32-x86-multi-thread, DBD :: mysql v4.011

.021 on Linux Perl v5.16.1 i486-linux-thread-multi DBD :: mysql v4.

Code Dbi_fork.pl:

  #! Use / usr / bin / perl strict; Use warnings; Use DBI; "Mysql.pl" is required; My $ dbh = connect_mysql (); If (fork ()) {$ dbh-> ("UPDATE article SET title = 'parent' WHERE id = 1") ;; } And (my $ dbh_child = $ dbh-> clone (); $ Dbh- & gt; {InactiveDestroy} = 1; Undef $ dbh; $ Dbh_child- & gt; Tax ("Update Article SET title = 'child 'WHERE id = 2');}   

mysql.pl:

  sub-connection_mysql {my $ user_db = 'user'; My $ password_db = ' My '$ base_name =' test '; my $ mysql_host_url =' localhost '; my $ dsn = "dbi: mysql: $ base_name: $ mysql_host_url"; my $ dbh = dbi- & gt; connect ($ dsn, $ User_db, $ password_db) or $ DBI :: errstr; return $ dbh;} 1;   

article table:

  drop table if exists' article '; Table' Articles' (`id`inte (10) unsigned null AUTACCRment, `title 'varchar (50) DEFAULT NULL, primary key (` ID`)) Engine = InnoDB AUTO_INCREMENT = 3 DEFAULT CHARSET = utf8; ----------------- ----------- - Articles of records - ------------- --------------- 'included in the values ​​of articles INSERT ('2', 'title2');   

This gives an error on the windows: '<1', 'title1'); > $ perl ./dbi_fork.pl DBD :: mysql :: db Clone fail: handle 2 is owned by thread 2344 B4 current thread 1a45014 (. The handle can not be shared between threads and your driver may require clone method) ./dbi_fork.pl on line 14.

How to fix? There is no such thing as fork on Windows

The attribute is

Instead of trying to reconstruct the existing connection, just make connections in the work.

In other words, use

  if (fork ()) {my $ dbh = connect_mysql (); $ Dbh- & gt; do (...); } And {my $ dbh = connect_mysql (); $ Dbh- & gt; do (...); }    

No comments:

Post a Comment