2012-05-16 2 views
0

J'ai un script CLI qui utilise Zend Framework et forge un processus fils. Cela fonctionne bien quand je le lance depuis CLI mais quand je le lance à partir d'un script bash, la connexion db est fermée quand le script bash se termine.Création d'une nouvelle connexion db basée sur une connexion existante dans Zend Framework

Je pense que je dois donner à l'enfant une nouvelle connexion db à la même base de données. Malheureusement, la façon dont la connexion est créée à l'origine est trop mystérieuse pour moi, donc je voudrais créer la nouvelle connexion basée sur l'existant. Comment puis je faire ça?

Voici comment son créé dans le Bootstrap et comment je y accéder plus tard:

$resource = $this->getPluginResource ('db'); 
$db = $resource->getDbAdapter(); 
Zend_Registry::getInstance()->dbAdapter = $db;  

$this->db = Zend_Registry::get ('dbAdapter'); 

Et c'est là que je voudrais utiliser la nouvelle connexion:

public function start_daemon($worker) { 
    if (file_exists ($this->get_pidfile ($worker))) 
     die ('process is already running - process pidfile already exists -> ' . $this->get_pidfile ($worker) . "\n"); 
    $cmd = 'php -f ' . __FILE__ . ' process'; 
    if ($this->is_win) { 
     $WshShell = new COM ("WScript.Shell"); 
     $oExec = $WshShell->Run ("$cmd /C dir /S %windir%", 0, false); 
     exec ('TASKLIST /NH /FO "CSV" /FI "imagename eq php.exe" /FI "cputime eq 00:00:00"', $output); 
     $output = explode ('","', $output [0]); 
     $pid = $output [1]; 
     file_put_contents ($this->get_pidfile ($worker), $pid); 
     echo ('JobQue daemon started with pidfile:' . $this->get_pidfile ($worker) . "\n"); 
    } else { 
     $PID = pcntl_fork(); 
     if ($PID) { 
      file_put_contents ($this->get_pidfile ($worker), $PID); 
      echo ('JobQue daemon started with pidfile:' . $this->get_pidfile ($worker) . "\n");        
      exit(); // kill parent 
     } 
     //!!Need to create a new db connection here 
     //to make sure the child will have one 
     //when the parent exits 
     posix_setsid(); // become session leader 
     chdir ("/"); 
     umask (0); // clear umask 
     $this->proc_nice (19); 
     $this->process_jobs(); 
    } 
} 

Répondre

0

Vous pouvez accéder comme ce

public function start_daemon($worker) { 

    $db = Zend_Registry::get ('dbAdapter'); 
    //do database operations with db object 

Pour nouvelle connexion

//if config is stored in registry, 
$config= Zend_Registry::get ('config'); 
$db = Zend_Db::factory($config['resources']['db']['adapter'], $config['resources']['db']['params']); 

OR 

$db = Zend_Db::factory('Pdo_Mysql', array(
      'host'  => 'hostname', 
      'username' => 'xxxxxxx', 
      'password' => 'xxxxxxxx', 
      'dbname' => 'xxxxxxxxx' 
     )); 



//set as default adapter 
    Zend_Db_Table_Abstract::setDefaultAdapter($db); 
+0

Bien sûr, c'est la façon dont j'accède à la DB maintenant. Mais je pense que cela utilisera simplement la connexion existante et n'en créera pas une nouvelle. – SunWuKung

+0

J'ai mis à jour anwser – Venu

Questions connexes