Closed sigasigasiga closed 3 months ago
Depending on what you're trying to achieve, you have different options. In any case, the key point is that MySQL will roll back any open transaction automatically when the session that started them is either closed or reset. That means that the following actions will cause a transaction rollback:
(async_)execute("ROLLBACK")
as you're suggesting.(async_)reset_connection()
(although please read this function's docs before using it, as it has some non-obvious behavior server-side).(async_)close()
.any_connection
object goes out of scope.Now, I'd say there are two possible scenarios for you:
connection_pool
, and follow a similar scheme. Just call COMMIT
if everything worked fine, and let pooled_connection
destructor handle it.Under the hood, returning a connection to the pool will mark it as "pending reset". The implementation will attempt a background async_reset_connection
+ async_set_character_set
, which rolls back open transactions.
Note that execute
is not marked as noexcept
because it involves communication with the server, and thus may block and fail. For instance, if the exception happened because you lost communication with the server, executing the rollback will fail (it won't be required in any case). There isn't a built-in async equivalent for the RAII pattern. I know Boost.Cobalt implemented the equivalent of Python's async with, but again, it can fail on error.
If this answer doesn't fulfill your needs, please expand on your use case and I'll try to expand on my advice.
Regards, Ruben.
Thank you so much for such a detailed answer!
I'm writing some sort of library based on Boost.MySQL and I was wondering if it is possible to offer a strong exception guarantee, but I guess that only a basic one is possible
Yep. I don't think offering a strong guarantee is possible if the cleanup action requires a network transfer.
I'll close this issue now. Please feel free to ask any other questions you may have.
Regards, Ruben.
Examples in the documentation (link) don't answer that question, although I think it is pretty important.
I was thinking about implementing some sort of
scoped_transaction
class that'd be defined like this:but since
{,async_}execute
methods are not marked asnoexcept
I thought that this wouldn't be a great idea. Also, implementing it using coroutines is also not an easy task.Does the library provide any utilities to overcome this problem?