【问题标题】:Php datastore script fails on development server while runs as expected on production serverPHP 数据存储脚本在开发服务器上失败,而在生产服务器上按预期运行
【发布时间】:2019-05-30 04:44:35
【问题描述】:

查看以下数据存储脚本:

<?php
use google\appengine\datastore\v4\Mutation\Operation;
use google\appengine\datastore\v4\BeginTransactionRequest;
use google\appengine\datastore\v4\BeginTransactionResponse;
use google\appengine\datastore\v4\CommitRequest;
use google\appengine\datastore\v4\CommitRequest\Mode;
use google\appengine\datastore\v4\CommitResponse;
use google\appengine\datastore\v4\RollbackRequest;
use google\appengine\datastore\v4\RollbackResponse;
use google\appengine\runtime\ApiProxy;
use google\appengine\runtime\ApplicationError;


function beginTransaction(){
    global $request, $response;
    $request = new BeginTransactionRequest();
    $request->setCrossGroup(true);
    $response = new BeginTransactionResponse();
    try{
        ApiProxy::makeSyncCall('datastore_v4', 'BeginTransaction', $request, $response, 60);
    }
    catch(ApplicationError $e){
        echo $e->getMessage();
        exit();
    }

    $request = new CommitRequest();
    $request->setTransaction($response->transaction);
    $request->setMode(Mode::TRANSACTIONAL);
}

function prepareEntity($entity, $name, $author, $channel){
    global $projectId;
    $entity->mutableKey()->mutablePartitionId()->setDatasetId($projectId);
    $entity->mutableKey()->addPathElement()->setKind('Books');
    $entity->addProperty()->setName('Name')->mutableValue()->setStringValue($name);
    $entity->addProperty()->setName('Author')->mutableValue()->setStringValue($author);
    $entity->addProperty()->setName('Channel')->mutableValue()->setStringValue($channel);
}

function commit(){ 
    global $request, $response;
    $response = new CommitResponse();     
    try{
        ApiProxy::makeSyncCall('datastore_v4', 'Commit', $request, $response, 60);
    }
    catch(ApplicationError $e){
        echo $e->getMessage();
        exit();
    }
}


$projectId = $_SERVER['APPLICATION_ID'];
beginTransaction();

prepareEntity(
    $request->addMutation()->setOp(Operation::INSERT)->mutableEntity(),
    'Contemporary Abstract Algebra',
    'J Gallian',
    'Mutation'
    );

prepareEntity(
    $request->addMutation()->setOp(Operation::INSERT)->mutableEntity(),
    'Principals of Mathematical Analysis',
    'Rudin',
    'Mutation'
    );
commit('Mutation Commit');
echo "<h2>Mutation Commit</h2>";
foreach($response->getMutationResultList() as $mutationResult){
    echo 'Book@'. end($mutationResult->getKey()->getPathElementList())->getId(). '<br/>';
}
echo '<hr/>';

beginTransaction();
prepareEntity(
    $request->mutableDeprecatedMutation()->addInsertAutOId(),
    'Contemporary Abstract Algebra',
    'J Gallian',
    'Deprecated Mutation'
    );

prepareEntity(
    $request->mutableDeprecatedMutation()->addInsertAutOId(),
    'Principals of Mathematical Analysis',
    'Rudin',
    'Deprecated Mutation'
    );

commit();
echo "<h2>Deprecated Mutation Commit</h2>";
foreach($response->getDeprecatedMutationResult()->getInsertAutoIdKeyList() as $key){
    echo 'Book@'. end($key->getPathElementList())->getId(). '<br/>';
}
echo '<hr/>';

在此脚本中,使用突变的提交在开发服务器上不起作用。他们既不保存数据也不抛出任何错误。尽管它们在生产服务器上按预期工作。不推荐使用的突变在开发服务器和生产服务器上都按预期工作。我调试发现make_call函数调用在使用google\appengine\runtime\RealApiProxy 无法产生响应。有人可以解释这种行为。这是开发运行时中的错误吗?

【问题讨论】:

    标签: php google-app-engine google-cloud-datastore protocol-buffers


    【解决方案1】:

    我预计您的问题出在 use google\appengine\datastore\v4\... 行上,您可能应该使用 google-cloud-datastore 库。入门指南位于https://cloud.google.com/php/getting-started/using-cloud-datastore

    【讨论】:

    • 代码在 appengine 上运行良好,所以我认为不存在问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 2014-12-09
    • 2011-09-17
    相关资源
    最近更新 更多