Project

General

Profile

Support #7373

SEH Exception on Wt:DBO rollback

Added by Michael Tornack over 2 years ago. Updated over 2 years ago.

Status:
New
Priority:
Normal
Assignee:
-
Target version:
-
Start date:
12/19/2019
Due date:
% Done:

0%

Estimated time:

Description

Hello,

I'm trying to implement a SQL storage class with your Wt::Dbo framework. In general it is working fine but with the rollback scenario I have problems.

I have function which accepts a lambda (fun). All database actions are performed within this function. If an error occurs in this lambda function I want to gracefully roll back and throw a SqlException.

Storage class

void Storage::execute(std::function<void (dbo::Session *session)> fun)

{

dbo::Transaction transaction{m_session};

bool exceptionCaught = false;

std::string errorMessage = "";

try {

fun(&m_session);

transaction.commit();

} catch (const std::exception& e) {

errorMessage = e.what();

exceptionCaught = true;

}

transaction.rollback();

if(exceptionCaught){

throw SqlException(errorMessage);

}

}

In my tests, the exception (std::exception& e) is caught. But when I try to commit new transactions in the database I get an exception (error: SEH exception with code 0xc0000005). Furthermore I noticed that the warning "1 Dirty Object" is shown. Can you give me a hint what is wrong with my setup?

The unit test looks like that btw:

Unit Test

TEST_F(TestStorage, RollBack){

auto storage = Storage::getInstance();

auto funInsert = [](dbo::Session *session){

auto user = std::make_uniquePersistence::User();

user->userName = "Joe";

user->id = 1;

session->add(std::move(user));

};

auto funFailed = [](dbo::Session *session){

dbo::ptrPersistence::User joe = session->findPersistence::User().where("user_name = ?").bind("Joe");

ASSERT_EQ(joe.get()->userName, "Joe");

ASSERT_EQ(joe.get()->id, 1);

joe.modify()->userName = "Jane";

// Check if username was changed

dbo::ptrPersistence::User jane = session->findPersistence::User().where("user_name = ?").bind("Jane");

ASSERT_EQ(jane.get()->userName, "Jane");

// Insert User with same id, this should trigger a unique constraint failure

auto user = std::make_uniquePersistence::User();

user->userName = "SameId";

user->id = 1;

session->add(std::move(user));

};

auto funFind = [](dbo::Session *session){

dbo::ptrPersistence::User joe = session->findPersistence::User().where("user_name = ?").bind("Joe");

ASSERT_EQ(joe.get()->userName, "Joe");

ASSERT_EQ(joe.get()->id, 1);

};

ASSERT_NO_THROW(storage->execute(funInsert));

ASSERT_THROW(storage->execute(funFailed), SqlException);

ASSERT_NO_THROW(storage->execute(funFind));

}

Also available in: Atom PDF