You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Reacted by qsona, SegaraRai, megumin, Jiri Spac, Rowin Hernández, Jacob Coker-Dukowitz, Tronikel, Joe Daley and TJ LaBaugh
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.
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.
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)
--
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.
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.
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.
If you have an interactive transaction that contains a nested write, will that cause an issue since nested writes are done with transactions?
Reacted by jarupong pajakgo, Michael Mentele, Sergio Ríos, Binish Joshi and Tamas Barasz
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.
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?
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 hackygeneratorclient {provider="prisma-client-js"previewFeatures=["interactiveTransactions"]}datasourcedb {provider="postgresql"url="postgres://postgres:[email protected]:5432/placeholder"}modelPlaceholder {idInt@id}
// ...some other fileimport{Prisma,PrismaClient}from'@prisma/client';// Prisma only available after type generation
...
construnInTransaction=async<TReturn>(connection: PrismaClient,fn: (tx: Prisma.TransactionClient)=>TReturn,): Promise<TReturn>=>{returnconnection.$transaction(asynctx=>{returnfn(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
@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.
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.
@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.
"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
Activity
pkellner commentedon Aug 10, 2021
I love this. Very important IMHO. I'm wondering why it is called "Interactive Transaction" and not just "Transaction". What is interactive about it?
matthewmueller commentedon Aug 10, 2021
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.
pkellner commentedon Aug 10, 2021
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.
maxerbox commentedon Aug 11, 2021
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 ?
andreimc commentedon Aug 11, 2021
we get the following, has anyone experienced this?
2color commentedon Aug 11, 2021
Hey @andreimc,
Could you please share your schema and the transaction that triggers this error?
millsp commentedon Aug 11, 2021
@maxerbox It will rollback on its own, either cleaned up by the QE (if still alive) or the db.
hugbubby commentedon Aug 14, 2021
But what about the performance issues!!!
millsp commentedon Aug 16, 2021
@hugbubby would you mind sharing your experience with more detail? Thanks!
hugbubby commentedon Aug 24, 2021
I'm joking. This is a critical feature. Thank you for finally releasing it and for the product in general <3
maxerbox commentedon Aug 24, 2021
Would be interesting anyway to have a benchmark :P
25 remaining items
aniravi24 commentedon Jul 5, 2022
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.
$prisma.transaction, which may contain another call toprisma.$transaction. In languages like Elixir for example, Ecto is capable of seamlessly nesting transactions and handling them gracefully.casey-chow commentedon Jul 13, 2022
Is there a way to disable the timeout in development across the board? It's making debugging a pain.
millsp commentedon Jul 25, 2022
Hey @casey-chow @aniravi24 feel free to open a feature request so that we can prioritize.
casey-chow commentedon Jul 26, 2022
Opened! #14487
iiAku commentedon Aug 23, 2022
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:
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.
Akxe commentedon Aug 23, 2022
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?jacobleecd commentedon Aug 25, 2022
Really looking forward to this being GA!
aniravi24 commentedon Sep 6, 2022
@millsp sorry for the delay, opened some issues (#15211, #15212) and a discussion!
ragrag commentedon Oct 27, 2022
Hello,
One thing i find a little bit annoying is that
Prisma.TransactionClientcan 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:
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
aqrln commentedon Nov 14, 2022
@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.TransactionClientstub in placeholder types will be just an alias toany— 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 wholePrismanamespace was missing there.ricardopieper commentedon Nov 17, 2022
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:
This is OK, but we see these methods growing a lot for many reasons.
However, I found a way to make this possible:
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.
janpio commentedon Nov 30, 2022
Thanks for all your feedback! Interactive transactions went generally available in 4.7.0. See the docs at https://www.prisma.io/docs/concepts/components/prisma-client/transactions#interactive-transactions
nimeshvaghasiya commentedon Jan 20, 2023
@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