Transaction commit and rollback
Thanks for providing commit and rollback of transactions. I found some deficiencies in the features. They may keep database in perpetually locked state.
- 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?
- 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?
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;
}
}
check transaction function with lambda. It works this way
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);
}
}
I checked the transaction function. It is little less flexible than I want. The lambda function signature is not useful for me.
@Ninja-007 show me your case please
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
}
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;
});
Thanks, let me try it.
- There are some class method calls in between replace calls.
- I need to set some return values and log some details among their calls.
Let me make better example if possible.
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`
Thanks, it will be good. If the lock rollbacks on destroy then it is much more flexible option.
added in b27ef78c570a5153dc5b41910d1704a93213b8c1. Please check it out. Also added tests for it
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.
I don't understand why do you need to call sqlite3_close by yourself? sqlite_orm performs it for you
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?
It cannot happen
OK. Then I close the issue. Thanks for addition of the code.