openworm / owmeta-core

Core library for owmeta
MIT License
3 stars 2 forks source link

Unbind transaction boundaries from connection open/close #44

Open mwatts15 opened 2 years ago

mwatts15 commented 2 years ago

Problem

The connection boundaries in owmeta-core are tied to the transaction boundaries in owmeta-core through the use of traction.begin/abort/commit calls in the owmeta_core.data.Data methods. This presents problems when, for instance, we want to abort a transaction but closing the connection via Data.closeDatabase commits the ongoing transaction:

with transaction.manager, connect():
    result = do_something()
    if condition(result):
        raise Exception("nope")

contrariwise, we can just have unnecessary transactions which aren't clearly tied to our transaction manager, which is just confusing. In this piece of code, we have a transaction that's initiated but then implicitly aborted when the transaction manager does its thing:

with connect(), transaction.manager:
    result = do_something()
    if condition(result):
        raise Exception("nope")

Which could prove problematic when we're integrating with other schemes that may have to do some extra work in setting up a transaction.

Proposal

Alternatives

Considered letting this alone, but it actually becomes a problem when trying to allow for retrying user actions within the scope of a transaction since the implicit commit commits partial work, even when we raise an exception when our connection boundary is within our transaction boundary and the retry starts a new transaction.

Just removing the transaction.begin/commit/abort calls doesn't completely solve the issue since it still leads to subtle bugs that are only solved by carefully rearranging when connections are started and ended, but that's really fragile, confusing, and harder to use in the long run than explicit transaction boundaries with standardized context managers.