drogon icon indicating copy to clipboard operation
drogon copied to clipboard

Add autorollback functionality for database

Open BurievSardor opened this issue 3 years ago • 3 comments

if adding autorollback functionality is impossible, please uncomment commit() function in transaction class.

class Transaction : public DbClient { public: virtual void rollback() = 0; // virtual void commit() = 0; virtual void setCommitCallback( const std::function<void(bool)> &commit Callback) = 0; void closeAll() override { } };

BurievSardor avatar Sep 18 '22 10:09 BurievSardor

A transaction will rollback automatically on any query failure.

hwc0919 avatar Sep 20 '22 02:09 hwc0919

Thank you for your answer. In drogon when the Transaction object is destructed, the commit statement is automatically executed to end the transaction. But i need the opposite. I meant this.

BurievSardor avatar Sep 20 '22 15:09 BurievSardor

We could add a setAutoCommit(bool) method to make the Transaction commit or rollback in destructor. In the meantime we should uncomment the commit() method to let user commit manually.

There is also an easy solution without any changes on the library.

class TransactionAutoRollback
{
  public:
    TransactionAutoRollback(std::shared_ptr<Transaction> &&trans)
        : trans_(std::move(trans))
    {
    }
    ~TransactionAutoRollback()
    {
        if (trans_)
            trans_->rollback();
    }
    const std::shared_ptr<Transaction> &get()
    {
        return trans_;
    }
    void commit(const std::function<void(bool)> &commitCallback)
    {
        if (trans_)
        {
            if (commitCallback)
            {
                trans_->setCommitCallback(commitCallback);
            }
            trans_.reset();
        }
    }

  private:
    std::shared_ptr<Transaction> trans_;
};

hwc0919 avatar Sep 21 '22 03:09 hwc0919

@an-tao Can we add setAutoCommit(bool) funtion for Transaction class? I really need it.

BurievSardor avatar Dec 08 '22 18:12 BurievSardor