Skip to content

Preview feature feedback: interactiveTransactions #8664

Closed

Description

@m-abdelwahab

Please share your feedback about the preview of Interactive Transactions support that was released in v2.29.0 in this issue.

If you encounter a bug, please open a bug report in this repo.
If the feature is working well for you, please share this in a comment below or leave a 👍 on this issue.
If you have any questions, don't hesitate to ask them in the #prisma2 channel in the Prisma Slack.

Activity

  1. pkellner commented on Aug 10, 2021

    @pkellner

    I love this. Very important IMHO. I'm wondering why it is called "Interactive Transaction" and not just "Transaction". What is interactive about it?

  2. matthewmueller commented on Aug 10, 2021

    @matthewmueller
    Contributor

    Hey @pkellner, thanks for raising this.

    We need a separate concept for $transaction([..., ...]) and $transaction(async tx => { ... }) because we may need to treat them differently in the future.

    We decided on "interactive transaction" based on prior art that @fhut researched in this comment. The interactive part is that you can insert custom logic inside the transaction.

    We decided against "client-side transaction" because we thought it could be interpreted as a browser transaction of some sort. Wrong part of the stack.

  3. pkellner commented on Aug 10, 2021

    @pkellner

    I agree "client-side" would have been very confusing (I'd make up what that meant and I'd be wrong). The good news I suppose is that I don't even have a guess (without reading the doc) for what an interactive transaction might be so at least I wouldn't be thrown off. Naming things is hard and thankless.

  4. maxerbox commented on Aug 11, 2021

    @maxerbox

    What happens if my nodejs app process is killed instantly when I have a running transaction ? Does it rollback automatically, or cause a dead lock ?

  5. andreimc commented on Aug 11, 2021

    @andreimc

    we get the following, has anyone experienced this?

    
    
    TypeError: Cannot read property 'id' of undefined     at PrismaService._transactionWithCallback (/workspace/libs/legacy-prisma/dist/runtime/index.js:36883:61)     at runMicrotasks (<anonymous>)     at processTicksAndRejections (internal/process/task_queues.js:95:5)
    --
    
    
    
  6. 2color commented on Aug 11, 2021

    @2color
    Contributor

    Hey @andreimc,

    Could you please share your schema and the transaction that triggers this error?

  7. millsp commented on Aug 11, 2021

    @millsp
    Contributor

    @maxerbox It will rollback on its own, either cleaned up by the QE (if still alive) or the db.

  8. hugbubby commented on Aug 14, 2021

    @hugbubby

    But what about the performance issues!!!

  9. millsp commented on Aug 16, 2021

    @millsp
    Contributor

    @hugbubby would you mind sharing your experience with more detail? Thanks!

  10. hugbubby commented on Aug 24, 2021

    @hugbubby

    @hugbubby would you mind sharing your experience with more detail? Thanks!

    I'm joking. This is a critical feature. Thank you for finally releasing it and for the product in general <3

  11. maxerbox commented on Aug 24, 2021

    @maxerbox

    @hugbubby would you mind sharing your experience with more detail? Thanks!

    I'm joking. This is a critical feature. Thank you for finally releasing it and for the product in general <3

    Would be interesting anyway to have a benchmark :P

  12. 25 remaining items

  13. aniravi24 commented on Jul 5, 2022

    @aniravi24

    Excited to see more development of this feature! I've looked around the discussions and issues to see if these were addressed, but wanted to ask here more directly (also let me know if I should open issues for any of these):

    Just for more context, we use PostgreSQL.

    1. We tend to write more code in a functional style and avoid throwing errors if unnecessary to do so - it would be awesome to have a manual rollback function so we don't have to throw errors to have transactions rollback.
    2. Is it possible to support nested transactions even if the underlying database doesn't support it? Meaning, you would call $prisma.transaction, which may contain another call to prisma.$transaction. In languages like Elixir for example, Ecto is capable of seamlessly nesting transactions and handling them gracefully.
    3. If you have an interactive transaction that contains a nested write, will that cause an issue since nested writes are done with transactions?
  14. casey-chow commented on Jul 13, 2022

    @casey-chow

    Is there a way to disable the timeout in development across the board? It's making debugging a pain.

  15. millsp commented on Jul 25, 2022

    @millsp
    Contributor

    Hey @casey-chow @aniravi24 feel free to open a feature request so that we can prioritize.

  16. casey-chow commented on Jul 26, 2022

    @casey-chow

    Opened! #14487

  17. iiAku commented on Aug 23, 2022

    @iiAku

    I really like the interactiveTransactions it fits a lot of needs into what I'm currently working on. Thanks for providing such a feature.

    I am aware of this:

    Note: If you use interactive transactions, then you cannot use the Data Proxy at the same time.

    However, I was just wondering if that was basically something being worked on to be supported. My go-to solution, for now, would be to use PgBouncer as stated here but I guess a bunch of people would be willing to use the alternative solution Data Proxy instead.

  18. Akxe commented on Aug 23, 2022

    @Akxe
    Contributor

    Could we get a promise wrapper that we can use to prevent timeout when a non-Prisma task is being processed? Just a special method on the passed Prisma object, something like awaitForieign?

  19. jacobleecd commented on Aug 25, 2022

    @jacobleecd

    Really looking forward to this being GA!

  20. aniravi24 commented on Sep 6, 2022

    @aniravi24

    @millsp sorry for the delay, opened some issues (#15211, #15212) and a discussion!

  21. ragrag commented on Oct 27, 2022

    @ragrag

    Hello,
    One thing i find a little bit annoying is that Prisma.TransactionClient can only be used after type generation w/prisma generate.
    this limitation forces low level libraries that need to only deal with prisma internal types to actually have a dummy placeholder schema otherwise type generation is not possible, e.g:

    // only used for type generation, which is kinda hacky
    generator client {
        provider        = "prisma-client-js"
        previewFeatures = ["interactiveTransactions"]
    }
    
    datasource db {
        provider = "postgresql"
        url      = "postgres://postgres:[email protected]:5432/placeholder"
    }
    
    model Placeholder {
        id Int @id
    }
    
    // ...some other file
    import { Prisma, PrismaClient } from '@prisma/client'; // Prisma only available after type generation
    
    ...
    
    const runInTransaction = async <TReturn>(
        connection: PrismaClient,
        fn: (tx: Prisma.TransactionClient) => TReturn,
    ): Promise<TReturn> => {
        return connection.$transaction(async tx => {
            return fn(tx);
        });
    };
    
    ...

    tldr: it would be really nice to have an export for TransactionClient directly from @prisma/client without the need to generate prisma db specific types

  22. aqrln commented on Nov 14, 2022

    @aqrln
    Member

    @ragrag thank you for this feature request, this will be added when interactive transactions go to general availability soon (see #16257). Note that just like regular PrismaClient, Prisma.TransactionClient stub in placeholder types will be just an alias to any — a more specific type is not possible here without the schema.

    Please don't hesitate to tell us if you could benefit from anything else defined in the default index.d.ts — until now the whole Prisma namespace was missing there.

  23. ricardopieper commented on Nov 17, 2022

    @ricardopieper

    I found a way to expose the transaction using some ""clever"" promise handling, which should be good for transactions that cross repositories and services. I am not sure it's 100% safe or reliable, but here is what we have right now:

    Today Prisma offers us the following:

    await prisma.$transaction(prismaTransaction => {
        **everything inside this lambda**
    })
    

    This is OK, but we see these methods growing a lot for many reasons.
    However, I found a way to make this possible:

    const tx = await transactionProvider.createInteractiveTransaction();
    
    //pass the transaction along to other repo/service/etc methods
    const user = await userRepo.create(..., tx);
    const purchase = await purchaseRepo.create(..., tx);
    
    //tx object has all the prisma methods and properties in a Prisma.TransactionClient
    const users = await tx.users.findMany(...);
    
    //with extra methods
    await tx.commit();
    await tx.rollback();
    

    The downside is you must remember to always commit or rollback a transaction, while $transaction does it for you, but this is not unheard of in other DB abstractions.

    Another downside is passing a Prisma object along the repositories, which is not very desirable... maybe some kind of integration with the techniques displayed here https://dev.to/kenfdev/cross-module-transaction-with-prisma-5d08 would make it better.

  24. janpio commented on Nov 30, 2022

    @janpio
    Contributor
  25. added this to the 4.7.0 milestone on Nov 30, 2022
  26. nimeshvaghasiya commented on Jan 20, 2023

    @nimeshvaghasiya

    Another downside is passing a Prisma object along the repositories, which is not very desirable... maybe some kind of integration with the techniques displayed here https://dev.to/kenfdev/cross-module-transaction-with-prisma-5d08 would make it better.

    @ricardopieper @janpio
    Agreed! Sometime your code goes very lengthy, and you have to divide it into small peace of functions to make it more understandable. Most of devs are following pattern/layered etc... code and would not like to pass transactional prisma(tx) as parameters to every sub functions.

    connection-transaction-management-in-asp-net-boilerplate

    "If a unit of work method calls another unit of work method, both use the same connection & transaction. The first entered method manages the connection & transaction and then the others reuse it."

    Not sure this is doable in prisma, if it is then will add more value to interactive-transactions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: interactiveTransactionstopic: previewFeatures

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions