PHP-Postgresql如果返回错误,如何回滚查询? [英] PHP - Postgresql How I can rollback a query if is returning error?

查看:121
本文介绍了PHP-Postgresql如果返回错误,如何回滚查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用php和postgres制作CRUD,我想知道是否有办法在发生错误时回滚事务。实际上,如果
交易出现错误,则id(主键)会增加,那么我会丢失一个id供将来使用。

I'm making a CRUD with php and postgres, and I want to know if there a way to rollback the transaction if there is a error. actually if I get an error in the transaction the id (primary key) get increased, then I lose one id for the future use.

我该如何预防吗?我的意思是,如果插入查询失败,请不要在表中进行自动递增。

How can I prevent it? I mean, if the Insert's Query fails, don't make the auto increment in the table.

我使用一个类来执行查询:

Im using a class for execute the querys:

public function insertRecord ($data){
        $campos =$this->getTableFields();
        $data =implode ("', '", $data);
        $sql ="INSERT INTO {$this->table} ($campos) VALUES (";
        $sysData =$this->getDefaultValues();
        if($sysData){
            $sysData .= ",";
            $sql .="$sysData ";
        }
        $sql .="'$data') RETURNING {$this->campoId};";
        echo $sql;
        pg_query($this->linkid,$sql);
        return $this->validateOperation();
    }


推荐答案

事务已回滚。但是它仍然会用完序列中的一个数字,该序列是自动递增值的基础。这是设计使然,可以防止持久的锁,否则将导致并行执行性能下降。

The transaction is rolled back. But it will still use up a number from a sequence, on which automatically incrementing values are based. This is by design and prevents long lasting locks, which would kill parallel execution performance.

有一些解决方法:

  • postgresql generate sequence with no gap

这篇关于PHP-Postgresql如果返回错误,如何回滚查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆