Transactions inside transactions, is that possible? #12373
-
|
https://www.prisma.io/docs/concepts/components/prisma-client/transactions#the-transaction-api I get the following code FROM documentation: async function transfer(from: string, to: string, amount: number) {
return await prisma.$transaction(async (prisma) => {
// 1. Decrement amount from the sender.
const sender = await prisma.account.update({
data: {
balance: {
decrement: amount,
},
},
where: {
email: from,
},
})
// 2. Verify that the sender's balance didn't go below zero.
if (sender.balance < 0) {
throw new Error(`${from} doesn't have enough to send ${amount}`)
}
// 3. Increment the recipient's balance by amount
const recipient = prisma.account.update({
data: {
balance: {
increment: amount,
},
},
where: {
email: to,
},
})
return recipient
})
}const [posts, totalPosts] = await prisma.$transaction([
prisma.post.findMany({ where: { title: { contains: 'prisma' } } }),
prisma.post.count(),
])Is it possible to do something like it? await prisma.$transaction([
prisma.$transaction(async (prisma) => {
// 1. Decrement amount from the sender.
const sender = await prisma.account.update({
data: {
balance: {
decrement: amount,
},
},
where: {
email: from,
},
})
// 2. Verify that the sender's balance didn't go below zero.
if (sender.balance < 0) {
throw new Error(`${from} doesn't have enough to send ${amount}`)
}
// 3. Increment the recipient's balance by amount
const recipient = prisma.account.update({
data: {
balance: {
increment: amount,
},
},
where: {
email: to,
},
})
return recipient
})
])I get the following error: What do I need it? Why do I expect? I am running a bulk operation that each insertion needs to use an interactive transaction. ALL insertions MUST FAIL or SUCCESS. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 10 replies
-
|
Hey @felinto-dev 👋 Can't you just do all the bulk insertion operations in one single interactive transaction? |
Beta Was this translation helpful? Give feedback.
-
|
What would be the point of wrapping an interactive transaction into a non interactive transaction with just 1 item? Your code reformatted: await prisma.$transaction([
prisma.$transaction(async (prisma) => {
// 1. Decrement amount from the sender.
const sender = await prisma.account.update({
data: {
balance: {
decrement: amount,
},
},
where: {
email: from,
},
})
// 2. Verify that the sender's balance didn't go below zero.
if (sender.balance < 0) {
throw new Error(`${from} doesn't have enough to send ${amount}`)
}
// 3. Increment the recipient's balance by amount
const recipient = prisma.account.update({
data: {
balance: {
increment: amount,
},
},
where: {
email: to,
},
})
return recipient
})
]) |
Beta Was this translation helpful? Give feedback.
-
|
Hi guys, how u doing? I have the same cenarious here. I'm devolping a create user service and when this service calls my repository to create the user, i open a transaction. Inside this transaction i need to call my add-profile service to create the user with the respective profiles. The problem occurs because my repository where i create the user is using the transaction, but when i call my service to create profile it is using the prisma client. I can't create nasted transactions, but i can overwrite other classes "this" context to use the same transaction as PrismaClient. This is the only way that i found to do what i want. The otherway was duplicating code to use nasted queries how is showed in documentation. The $transaction property doesn't exists in Transactions. This is the only way that i found that works. When i tryed to use other things like {...tx, $transaction: async (func) => func(tx)} the transaction connection was closed. Waiting for nasted transactions in prisma |
Beta Was this translation helpful? Give feedback.
-
Could you please explain more ? I'm also needing something like it in my project, where does the Proxy comes from ? |
Beta Was this translation helpful? Give feedback.
-
|
#17215 (comment) |
Beta Was this translation helpful? Give feedback.
-
|
Hi there, To keep our discussions organized and focused on the most relevant topics, we’re reviewing and tidying up our backlog. As part of this process, we’re closing discussions that have already been marked as answered but remain open. If this discussion still requires further input or clarification, feel free to reopen it or start a new one with updated details. Your contributions are invaluable to the community, and we’re here to help! For more details about our priorities and vision for the future of Prisma ORM, check out our latest blog post: https://www.prisma.io/blog/prisma-orm-manifesto. Thank you for your understanding and ongoing support of the Prisma community! |
Beta Was this translation helpful? Give feedback.


Hi guys, how u doing?
I have the same cenarious here. I'm devolping a create user service and when this service calls my repository to create the user, i open a transaction. Inside this transaction i need to call my add-profile service to create the user with the respective profiles.
The problem occurs because my repository where i create the user is using the transaction, but when i call my service to create profile it is using the prisma client.
I can't create nasted transactions, but i can overwrite other classes "this" context to use the same transaction as PrismaClient. This is the only way that i found to do what i want. The otherway was duplicating code to use nasted queries how is…