sqlite_orm icon indicating copy to clipboard operation
sqlite_orm copied to clipboard

Transaction commit and rollback

Open ElephantInTheRoomFirst opened this issue 9 years ago • 16 comments

Thanks for providing commit and rollback of transactions. I found some deficiencies in the features. They may keep database in perpetually locked state.

  1. Possibilty of no excecution of either rollback or commit after begin_transaction(e.g. due to exception thrown after begin_transaction and before rollback or commit). Is it possible for you to create small Transaction class will rollback transaction in its destructor if it is not commited?
  2. commit can fail (https://sqlite.org/lang_transaction.html). There is no way to know if commit was successful. Can you return success or failure from commit method?

ElephantInTheRoomFirst avatar Jun 12 '17 08:06 ElephantInTheRoomFirst

Here is sample class for you.

class CTransaction { Storage& mStorage; bool mIsCommitted: CTransaction(Storage) : mStorage(Storage), mIsCommitted(false) { mStorage.begin transaction() } ~CTransaction() { //rollback transaction if uncommiited and begun if(!mIsCommitted) mStorage.rollback(); }

int Commit()
{
    int CommitRetVal = mStorage.commit();
    if(SQLITE_OK == CommitRetVal) mIsCommitted = true;
    return CommitRetVal;
}

}

ElephantInTheRoomFirst avatar Jun 12 '17 08:06 ElephantInTheRoomFirst

check transaction function with lambda. It works this way

fnc12 avatar Jun 12 '17 11:06 fnc12

if commit fails it throws an exception:

        void commit(sqlite3 *db) {
            std::stringstream ss;
            ss << "COMMIT";
            auto query = ss.str();
            sqlite3_stmt *stmt;
            if (sqlite3_prepare_v2(db, query.c_str(), -1, &stmt, nullptr) == SQLITE_OK) {
                statement_finalizer finalizer{stmt};
                if (sqlite3_step(stmt) == SQLITE_DONE) {
                    //  done..
                }else{
                    auto msg = sqlite3_errmsg(db);
                    throw std::runtime_error(msg);
                }
            }else {
                auto msg = sqlite3_errmsg(db);
                throw std::runtime_error(msg);
            }
        }

fnc12 avatar Jun 12 '17 12:06 fnc12

I checked the transaction function. It is little less flexible than I want. The lambda function signature is not useful for me.

ElephantInTheRoomFirst avatar Jun 12 '17 14:06 ElephantInTheRoomFirst

@Ninja-007 show me your case please

fnc12 avatar Jun 12 '17 19:06 fnc12

Thanks

try
{
    auto Storage = GetStorage(); //make_storage equivalent
    Storage.sync_schema(true);
    Storage.begin_transaction();
    
    //following replace call are only simplified version of code.
    Obj = {...}; 
    Storage.replace(Obj);

    Obj = {...}; 
    Storage.replace(Obj);
    
    Storage.commit();
}
catch(...)
{
    //ideally transaction should be rolled back if exception is thrown before commit
    
}

ElephantInTheRoomFirst avatar Jun 13 '17 09:06 ElephantInTheRoomFirst

Why don't you try this? Ok I'll add transaction_lock

    auto Storage = GetStorage(); //make_storage equivalent
    Storage.sync_schema(true);
    Storage.transaction([&]{
        try{
            //following replace call are only simplified version of code.
            Obj = {...}; 
            Storage.replace(Obj);

            Obj = {...}; 
            Storage.replace(Obj);
        }catch(...){
            return false;
        }
        return true;
    });

fnc12 avatar Jun 13 '17 10:06 fnc12

Thanks, let me try it.

  1. There are some class method calls in between replace calls.
  2. I need to set some return values and log some details among their calls.

Let me make better example if possible.

ElephantInTheRoomFirst avatar Jun 13 '17 10:06 ElephantInTheRoomFirst

We can add transaction_lock function which work like this:

auto lock = storage.transaction_lock();  // calls `BEGIN TRANSACTION` and returns lock object that rollbacks on destroy or commits if you tune it to commit.
if(...){
    lock.commit_on_destroy = true;  // commit_on_destroy is `false` default
}

// transaction is over at the end of the scope or on exception throw. If `commit_on_destroy` then lock calls `COMMIT` else `ROLLBACK`

fnc12 avatar Jun 13 '17 10:06 fnc12

Thanks, it will be good. If the lock rollbacks on destroy then it is much more flexible option.

ElephantInTheRoomFirst avatar Jun 13 '17 10:06 ElephantInTheRoomFirst

added in b27ef78c570a5153dc5b41910d1704a93213b8c1. Please check it out. Also added tests for it

fnc12 avatar Jun 19 '17 16:06 fnc12

Thank you very much. It looks like you did lot many chnages. One simple Q, what will happen when you call sqlite3_close(nullptr);. I hope it will not crash.

ElephantInTheRoomFirst avatar Jun 19 '17 18:06 ElephantInTheRoomFirst

I don't understand why do you need to call sqlite3_close by yourself? sqlite_orm performs it for you

fnc12 avatar Jun 19 '17 18:06 fnc12

I know it performs and I am not going to do it. I was just reading your code. Then then saw the code in destructor. What does happen if sqlite3_close(this->db = nullptr); gets executed?

ElephantInTheRoomFirst avatar Jun 19 '17 18:06 ElephantInTheRoomFirst

It cannot happen

fnc12 avatar Jun 19 '17 18:06 fnc12

OK. Then I close the issue. Thanks for addition of the code.

ElephantInTheRoomFirst avatar Jun 19 '17 18:06 ElephantInTheRoomFirst