0

我有一个使用 Zend 框架并派生出一个子进程的 CLI 脚本。当我从 CLI 启动它时它运行良好,但是当我从 bash 脚本启动它时,当 bash 脚本结束时,db 连接关闭。

我想我需要给孩子一个新的数据库连接到同一个数据库。不幸的是,最初创建连接的方式对我来说太神秘了,所以我想在现有的基础上创建新的连接。我怎样才能做到这一点?

这是它在 Bootstrap 中的创建方式以及我以后如何访问它:

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

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

这就是我想使用新连接的地方:

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 ();
    }
}
4

1 回答 1

0

你可以这样访问

public function start_daemon($worker) {

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

对于新连接

//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);
于 2012-05-16T12:54:07.007 回答