简体   繁体   English

如何处理neo4jphp中的事务?

[英]How to handle transactions in neo4jphp?

For relational databases like mysql, transaction handling in PHP is just like. 对于像mysql这样的关系数据库,PHP中的事务处理就像。

Begin transaction
...
Insert queries
...
Update queries
...
if error in any query then
Rollback transaction
...
at end, if no error in any query then
Commit transaction

How to handle transactions in ? 如何处理事务?

I have tried same like but there was failure. 我已经尝试过一样,但是失败了。 Even after rollback changes were saved. 即使在回滚更改之后也被保存。

I was doing like this. 我就是这样

//$client = Neo4jClient
$transaction = $client->beginTransaction();
...
//Insert queries
...
//Update queries
...
//if error in any query then
$transaction->rollback();
...
// at end, if no error in any query then
$transaction->commit();

Check following code. 检查以下代码。

//$client = Neo4jClient
$transaction = $client->beginTransaction();

$dataCypherQuery = new Query($client, $dataQuery, $params);

Instead of getting resultset from query, we need to add statement into transaction. 而不是从查询中获取结果集,我们需要在事务中添加语句。

// $dataResult = $dataCypherQuery->getResultSet(); // don't do this for transaction

Important : Pass query object to transaction's add statements method. 重要提示:将查询对象传递给事务的add语句方法。

$dataResult = $transaction->addStatements($dataCypherQuery);

We can pass true as parameter indicating transaction commit. 我们可以传递true作为指示事务提交的参数。

//$dataResult = $transaction->addStatements($dataCypherQuery, true);

If there is an error, changes are automatically rolled back. 如果有错误,更改将自动回滚。 You can check $dataResult variable for validity, result should be returning something. 您可以检查$ dataResult变量的有效性,结果应该返回一些值。

if (0 == $dataResult->count()) {
    $transaction->rollback();
}

At end, if no error in any query then 最后,如果任何查询没有错误,则

$transaction->commit();

For more info see Cypher-Transactions 有关更多信息,请参见Cypher-Transactions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM