Aaron - Thanks for your answers.
On Mon, Aug 3, 2015 at 4:22 PM, Aaron Laws <dartm...@gmail.com> wrote: > > On Mon, Aug 3, 2015 at 2:56 PM, K. Frank <kfrank2...@gmail.com> wrote: >> >> Hello List! >> >> I am looking at the Wt::Dbo tutorial: >> >> http://www.webtoolkit.eu/wt/doc/tutorial/dbo.html >> ... >> I'm a little confused about what happens if an exception is thrown. >> Presumably an SQL "rollback transaction" will be executed. But >> how? Destructors will be called for local variable within the try block. >> So, tweaking the example code a little: >> >> try { >> dbo::Transaction transaction(session); >> >> User *user = new User(); >> user->name = "Joe"; >> user->password = "Secret"; >> user->role = User::Visitor; >> user->karma = 13; >> >> dbo::ptr<User> userPtr = session.add(user); >> } >> catch (...) { >> // ... >> } >> >> If an exception is thrown in the try block, the destructor for >> "transaction" >> will be called. How will this destructor know to call "rollback >> transaction", >> rather than "commit transaction"? One possibility is that if the call to >> "session.add" fails and throws an exception, it could first set some >> "dirty transaction" flag that gets inspection by Wt::Dbo::Transaction's >> destructor. But what if it's not a Wt::Dbo exception? Suppose I add >> the line "if (42) throw 17;" to the try block. How would the destructor >> then know to roll back the SQL transaction? >> >> (Or is this some kind of c++ thing where the destructor "knows" that >> it's being called in an exception-triggered stack unwind?) > > It's some kind of C++ thing: > http://en.cppreference.com/w/cpp/error/uncaught_exception Very good. That clears up my question. (I wasn't aware of std::uncaught_exception().) (I deduce from your comment that the destructor for transaction calls std::uncaught_exception(), and if the stack IS being unwound, it rolls back the transaction, while if the stack is NOT being unwound, it commits the transaction. All good.) > Happy coding! > > In Christ, > Aaron Laws Thanks again. K. Frank ------------------------------------------------------------------------------ _______________________________________________ witty-interest mailing list witty-interest@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/witty-interest