Skip to content

transaction API is problematic, can we just have a getConnection style API? #99

@mberndt123

Description

@mberndt123

Hi,

the current transaction API is difficult to integrate with libraries like cats-effect or ZIO. The problem is that you're expected to pass a block to it that will perform side effects, and when the block finishes, the transaction is committed. IOW, the lifecycle of the transaction object is tied to the control flow of the application. But side effects and imperative control flow is not how these libraries normally work. While it's possible to perform side effects using things like ZIO's Runtime API, this is problematic because it interferes with fiber interruption.
There are also other scenarios where this API is problematic, for instance when you want to interact with any kind of asynchronous API (e. g. Future-based) within your transaction or when you're already using some other API for resource management, e. g. a scala.util.Using.Manager . While I can see the advantage of not being able to forget calling close, the conflation of lifecycle management and control flow makes these situations difficult to deal with.

The getAutoCommitClientConnection method is better in this regard, but just because you require more control over the transaction lifecycle doesn't mean you want auto-commit. A simple solution to deal with all this is to just add some method like startTransaction that gives you a DbApi.Txn object and that you need to call close() on yourself. This should be easy to add as DbApi is already a subtype of AutoCloseable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions