当我使用带有$ factory的DB单例时,为什么会出现Apache seg错误

时间:2017-05-26 20:24:58

标签: php linux apache pdo unixodbc

为什么在使用$ factory时会出现Apache seg错误,我该如何解决?

我正在使用PHP7运行Linux Web服务器(Ubuntu和CentOS)中的代码,其中unixODBC,FreeTDS和DBLib作为驱动程序。我一直在使用下面的DB类,它一直工作正常......

<?php
class DB {
    private static $factory;
    private $database;

    public static function getFactory() {
        // if (!self::$factory) {
        if (!static::$factory) {
            static::$factory = new DB();
        }
        return static::$factory;
    }

    public function getConnection() {
        if (!$this->database) {
            try {
                $options = array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
                $this->database = new PDO('dblib:host=localhost;dbname=DB_NAME;port=3306;charset=utf8', 'DB_USER', 'DB_PASS', $options);
            } catch (PDOException $e) {
                echo 'Error code: ' . $e->getCode();
                exit;
            }
        }
        return $this->database;
    }
}// end of class
?>

以下查询无问题地运行...

<?php
$db = DB::getFactory()->getConnection();
$sql = "INSERT INTO ".Config::get('TBL_PREFIX')."resourceTypes (name) VALUES (:name)";
$query = $db->prepare($sql);
$query->execute(array(':name' => $name));
die('query complete.');
?>

然而,当我决定使用PDO的beginTransaction()时,下面的查询失败并且Apache抛出一个seg错误([core:notice] [pid 1219] AH00051:child pid 31779退出信号分段错误(11),可能的coredump在/ etc / apache2)。下面是beginTransaction()...

的代码
<?php
$db = DB::getFactory()->getConnection();
$db->beginTransaction();// Start our transaction.
try {
    $sql = "INSERT INTO ".Config::get('TBL_PREFIX')."sched_resourceTypes (name) VALUES (:name)";
    $query = $db->prepare($sql);
    $query->execute(array(':name' => $name));
    $db->commit();// No exception, so commit the changes.
    die('committed...');
} catch(Exception $e){// Handle any exceptions that are thrown.
    echo $e->getMessage();// Print out the error message.
    $db->rollBack();// Rollback the transaction.
    die('rolling back...');
}
?>

如果我修改我的DB类看起来像下面的代码,上面的查询将没有a seg错误。

<?php
class DB {
    private static $database;

    public static function getConnection() {
        if (!Self::$database) {
            try {
                $options = array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
                Self::$database = new PDO('dblib:host=localhost;dbname=DB_NAME;port=3306;charset=utf8', 'DB_USER', 'DB_PASS', $options);
            } catch (PDOException $e) {
                echo 'Error code: ' . $e->getCode();
                exit;
            }
        }
        return Self::$database;
    }
}// End of class
?>

问题是什么?

为什么工厂在Apache中创建了一个seg错误?

最重要的是,我怎样才能以正确的方式纠正这个问题?

0 个答案:

没有答案