Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

“Transaction API error: Transaction not found” #16050

Closed
heymartinadams opened this issue Oct 28, 2022 · 12 comments
Closed

“Transaction API error: Transaction not found” #16050

heymartinadams opened this issue Oct 28, 2022 · 12 comments
Assignees
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/client Issue for team Client. topic: database-provider/planetscale topic: interactiveTransactions topic: previewFeatures Issue touches on an preview feature flag topic: prisma-client topic: referentialIntegrity/relationMode topic: transaction
Milestone

Comments

@heymartinadams
Copy link

heymartinadams commented Oct 28, 2022

Bug description

Context: I’m trying to implement Replicache using Prisma interactive transactions for an offline-first, instant UI. 🚀

Bug: While using transactions, I get a Transaction API error: Transaction not found error.

How to reproduce

  1. Clone https://github.com/ConsciousApps/replicache-demo
  2. Obtain PlanetScale database test password via instructions in README.md file and replace placeholder in .env file
  3. Install: yarn (or npm install)
  4. Reset database (in case I, or someone else, has already populated it with data): yarn db:reset
  5. Run yarn dev
  6. Sign up or sign in
  7. Once signed in, create 5 new to-dos in quick succession (creating new to-dos slowly does not produce this bug)

Expected behavior

All transactions should be found.

Prisma information

Schema: https://github.com/ConsciousApps/replicache-demo/blob/main/prisma/schema.prisma

Prisma Transactions:
https://github.com/ConsciousApps/replicache-demo/blob/main/pages/api/replicache/push.js#L24-L59
https://github.com/ConsciousApps/replicache-demo/blob/main/pages/api/replicache/pull.js#L20-L61

(bug shows up in either files)

Environment & setup

  • OS: macOS
  • Database: PlanetScale MySQL
  • Node.js version: 16.16.0

Prisma Version

4.6.0-dev.32 (latest)
@janpio
Copy link
Member

janpio commented Nov 1, 2022

One nice aspect to exclude here would be PlanetScale databases and referentialIntegrity = "prisma". Can you maybe run your reproduction against a normal MySQL and confirm that this is also happening there? Thanks.

@heymartinadams
Copy link
Author

Found what caused it. Transaction not found error went away once I removed this:

} catch (err) {
  console.error(err)
  res.status(500).send(err.toString())
} finally {
  await prisma.$disconnect()
}

onetask-me/prisma-replicache-demo@734f9ed#diff-b5c776daef6284d72036715b72e4d5ff18cc0558dea8a0742f6c859e6f0f12b8L62-L68

@janpio
Copy link
Member

janpio commented Nov 2, 2022

We should try to figure out what is going on here.

@janpio janpio reopened this Nov 2, 2022
@heymartinadams
Copy link
Author

heymartinadams commented Nov 3, 2022

@janpio the repo is a complete demo/reproduction so hopefully should be easy to set up and figure out.

@aqrln aqrln self-assigned this Nov 14, 2022
@heymartinadams
Copy link
Author

Hey @aqrln, I noticed you signed on yesterday... revisiting this repo today as well. Seems like there are a few bugs related to Replicache, I’ll see if I can work them out first and will let you know once I have.

@aqrln
Copy link
Member

aqrln commented Nov 17, 2022

Hey @heymartinadams no worries, I already finished the investigation and was going to share an update with you tomorrow. The gist is the issue was triggered by calling $disconnect inside the finally clause of the try/catch block — i.e., every time after every single transaction regardless of whether succeeded or errored, which broke other concurrently running transactions as the disconnect actually happened. I was going to call this a bug and say that $disconnect should wait for the transactions to complete, but then I realised it has some implications in edge cases, so we will have to think about which behaviour should be considered correct together with the team.

I assume you actually meant to put the $disconnect into the catch clause and only put it into the finally clause by accident, but either way I’m curious what is the purpose of that? I would expect it to be redundant or even lead to performance issues due to re-establishing the database connection. Have you run into any issues where Prisma doesn’t properly recover from errors?

@heymartinadams
Copy link
Author

heymartinadams commented Nov 18, 2022

Hi @aqrln, I didn’t know what I was doing regarding the $disconnect, so it’s more of a case of me having put this there without understanding it properly 🤦🏻‍♂ Is the Prisma tx, the way it’s written without $disconnect, correct?

So I finally figured out how to make it work, yay! 🎉 Looking forward to implementing it, and, once implemented, would love to connect with someone at Prisma to write up a blog post about it! Offline / undo-redo / and instant UI are definitely features in high demand and I think a blog post could add tremendous value to many devs. Please let me know!

CleanShot.2022-11-18.at.09.52.44.mp4

aqrln added a commit to prisma/prisma-engines that referenced this issue Nov 23, 2022
Make the error messages for closed transactions more detailed, add extra context about the transaction timeout.

  Before:

  - `Transaction API error: Transaction already closed: A query/commit/rollback cannot be executed on a closed transaction..`
  (also note the double "." at the end)

  Now:

  - `Transaction API error: Transaction already closed: A query/commit/rollback cannot be executed on a committed transaction.`
  - `Transaction API error: Transaction already closed:  A query/commit/rollback cannot be executed on a transaction that was rolled back.`
  - `Transaction API error: Transaction already closed:  A query/commit/rollback cannot be executed on an expired transaction. The timeout for this transaction was X ms, however Y ms passed since the start of the transaction. Consider increasing the interactive transaction timeout or doing less work in the transaction.`

Additionally, the "Transaction not found error" is now also more verbose, ref: https://www.notion.so/disconnect-with-iTX-f3cfee3ff4924e40aa90aadb2454e9fa?d=3bd7c7103b02461bbfe414a978a994c1#547ab127682b41898c87bdd5c841c0bf

Also contains minor cleanup things related to iTX:

* Remove the unused `CachedTx::Aborted` variant
* Remove references to an obsolete env var that doesn't exist since #3028 from comments and `.envrc`

Client PR: prisma/prisma#16382

Closes: prisma/prisma#13713
Ref: prisma/prisma#16050
Ref: #3028
@aqrln
Copy link
Member

aqrln commented Nov 24, 2022

Hi @heymartinadams and sorry for the late reply!

Looking forward to implementing it, and, once implemented, would love to connect with someone at Prisma to write up a blog post about it! Offline / undo-redo / and instant UI are definitely features in high demand and I think a blog post could add tremendous value to many devs. Please let me know!

Thank you, I passed your message to the team!

Is the Prisma tx, the way it’s written without $disconnect, correct?

Yep, I didn't look into what happens inside the transaction itself in details, so I can't give much additional advice, but in the context of this issue just removing the $disconnect should be good! You can leave the try/catch block if you need it for any other reason, no need to remove it like in onetask-me/prisma-replicache-demo@734f9ed, just remove the await prisma.$disconnect() line.

In prisma/prisma-engines#3391 I changed the error message from just Transaction not found to Transaction not found. Transaction ID is invalid, refers to an old closed transaction Prisma doesn't have information about anymore, or was obtained before disconnecting. Would that have been helpful in figuring out what happened and fixing the issue if the error message looked like that when you encountered it?

@aqrln
Copy link
Member

aqrln commented Nov 24, 2022

I'm going to go ahead and close this issue since it has been resolved. Feel free to leave a comment or open a new issue if you think there is still something missing on our side.

We also have ideas for improvements of $disconnect behaviour to try to prevent mistakes like this — new GitHub issue(s) will be opened for that.

@aqrln aqrln closed this as completed Nov 24, 2022
@aqrln aqrln added this to the 4.7.0 milestone Nov 25, 2022
@heymartinadams
Copy link
Author

Thank you @aqrln ✨ A more detailed error message might have been helpful at the time. Glad it’s made its way into 4.7.0.

I’ll write up a blog post about Replicache shortly. https://github.com/simplerlist/replicache-demo

@aruns05
Copy link

aruns05 commented Jul 25, 2023

Hello @aqrln Is any fix found for this ? I am facing same issue

@janpio
Copy link
Member

janpio commented Jul 26, 2023

This issue is closed, so we do not think there is a problem any more. If something is not working for you @aruns05, please open a new issue. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/client Issue for team Client. topic: database-provider/planetscale topic: interactiveTransactions topic: previewFeatures Issue touches on an preview feature flag topic: prisma-client topic: referentialIntegrity/relationMode topic: transaction
Projects
None yet
Development

No branches or pull requests

6 participants