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
{{ message }}
Repository navigation
Support manual rollback in interactive transactions #15211
To rollback an interactive transaction, you have to throw an error. It would be awesome to have a manual rollback function (that potentially takes a data argument to pass back to the parent caller). Throwing errors makes code less predictable and weakly typed. We try to reserve throwing errors for unexpected errors, not for expected ones.
Suggested solution
When in a transaction block, be able to call rollback on the prisma client. One potential implementation:
await prisma.$transaction(
async (prisma) => {
// Code running in a transaction...
prisma.rollback();
// or with arbitrary data/error
prisma.rollback(err);
}
)
Alternatives
We don't really have an alternative here as we can't rollback without an error.
Reacted by jarupong pajakgo, Ethan Naluz, Shivam Sadachar, Hamza MADAD, Raúl Juárez, Michał Lytek, Mitch Goshorn, Daniel Daukševič, Eemeli Palotie, Barry Hagan and 16 more
interactive transaction inner function have to end with rollback or commit command.
so I suggest new solution.
Suggested solution
rollback when inner function return Prisma.Rollback instance.
constprisma=newPrismaClien();constresponse: Prisma.users|"NOT_FOUND_USER"|"UNVERIFIED_USER"=awaitprisma.$transaction(async(tx)=>{constuser: Prisma.users|null=awaittx.users.findFirst({where: {id: 1}});Prisma.Rollback();// it is just instance, so this line does nothing.if(user===null)returnPrisma.Rollback("NOT_FOUND_USER");if(!user.verified)returnPrisma.Rollback("UNVERIFIED_USER");// ... some transaction code...returnuser;});
While there is no built-in functionality for this, it can be implemented via client extension. Here is an example of the one that allows to roll back the transaction by returning rollback symbol:
But it also higlights a typesafety problem with this feature request: what should return value of $noThrowTransaction should be if it is using manual rollback? It can not be return value of your callback since transaction is stopped before we got it. In by example it is undefined which creates it's own bundle of problems. It can also be some magic value, for example, the same rollback symbol. But that will poison the return types and you'll have to add if (result !=== rollback) condition every time before you can do anything with the return value. Overall, it does not feel to me that for cases where we care about return value any of the proposed solutions substantially improves ergonomics over throwing errors. You are free to experiment with the extension I provided though - if it works for you then great, if it does not - feel free to adjust it in any way.
@pheetah because nobody built it yet. You are free to experiment with the extension I posted in my previous comment. In order for us to consider it as a built-in feature we need good answers to the questions I posted in the end of this comment and so far nobody attempted that.
// First, we start a transaction from your connection and save it into a variableconstt=awaitsequelize.transaction();try{// Then, we do some calls passing this transaction as an option:constuser=awaitUser.create({firstName: 'Bart',lastName: 'Simpson',},{transaction: t},);awaituser.addSibling({firstName: 'Lisa',lastName: 'Simpson',},{transaction: t},);// If the execution reaches this line, no errors were thrown.// We commit the transaction.awaitt.commit();}catch(error){// If the execution reaches this line, an error was thrown.// We rollback the transaction.awaitt.rollback();}
Activity
saboorajat commentedon Nov 5, 2023
Any update on this? i can't find this feature on official documentation.
josethoz commentedon Nov 6, 2023
any updates?
rojiwon123 commentedon Nov 20, 2023
interactive transaction inner function have to end with rollback or commit command.
so I suggest new solution.
Suggested solution
Prisma.Rollbackinstance.Alternatives
endHandler.SevInf commentedon Nov 20, 2023
While there is no built-in functionality for this, it can be implemented via client extension. Here is an example of the one that allows to roll back the transaction by returning
rollbacksymbol:But it also higlights a typesafety problem with this feature request: what should return value of
$noThrowTransactionshould be if it is using manual rollback? It can not be return value of your callback since transaction is stopped before we got it. In by example it isundefinedwhich creates it's own bundle of problems. It can also be some magic value, for example, the samerollbacksymbol. But that will poison the return types and you'll have to addif (result !=== rollback)condition every time before you can do anything with the return value. Overall, it does not feel to me that for cases where we care about return value any of the proposed solutions substantially improves ergonomics over throwing errors. You are free to experiment with the extension I provided though - if it works for you then great, if it does not - feel free to adjust it in any way.pheetah commentedon Mar 14, 2024
Why wasn't this a feature?
SevInf commentedon Mar 14, 2024
@pheetah because nobody built it yet. You are free to experiment with the extension I posted in my previous comment. In order for us to consider it as a built-in feature we need good answers to the questions I posted in the end of this comment and so far nobody attempted that.
binishjoshi commentedon May 12, 2024
Something like how sequelize does it: https://sequelize.org/docs/v6/other-topics/transactions/#unmanaged-transactions
Neosoulink commentedon Aug 23, 2024
Any update about this?
saboorajat commentedon Oct 15, 2024
#22309 This might help. Although we as team have moved to typeORM for better features.
ferhatte10 commentedon Apr 24, 2025
You can just use => await prisma.$executeRaw
ROLLBACK; to enforce manual sql rollback => working for me