Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autorollback functionality for database #1386

Open
BurievSardor opened this issue Sep 18, 2022 · 4 comments
Open

Add autorollback functionality for database #1386

BurievSardor opened this issue Sep 18, 2022 · 4 comments
Labels
enhancement New feature or request

Comments

@BurievSardor
Copy link

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 BurievSardor changed the title Add autorollback functionality Add autorollback functionality for database Sep 18, 2022
@hwc0919
Copy link
Member

hwc0919 commented Sep 20, 2022

A transaction will rollback automatically on any query failure.

@BurievSardor
Copy link
Author

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 BurievSardor reopened this Sep 20, 2022
@an-tao an-tao added the enhancement New feature or request label Sep 21, 2022
@hwc0919
Copy link
Member

hwc0919 commented Sep 21, 2022

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_;
};

@BurievSardor
Copy link
Author

BurievSardor commented Dec 8, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants