api接口必备 try catch实例(操作mongo)

/**
 * 更新mongo
 * @param string $table_name
 * @param array $where
 * @param array $data
 * @param array $options
 * @return boolean
 */
public function updateMongo($table_name, $where, $data, $options=['w'=>0])
{
    if (!$table_name || !$where || !$data) {
        return false;
    }
    $collection = getMongoCollection($table_name);

    $retry = 0;
    $result = false;
    while (($retry < 3) && !$result) {
        try {
            $result = $collection->update($where, $data, $options);
        } catch (Exception $e) {
            $retry++;
            $this->debug('sid: ' . $sid . ', msg: 更新'. $table_name .'' . $retry . 'failed, ' . $e->getMessage());
            sleep(1);
            continue;
        }
    }

    return $result;
}
展开阅读全文

没有更多推荐了,返回首页