question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MySQL operation failed: Unknown prepared statement handler (${#}) given to mysqld_stmt_execute

See original GitHub issue

I have a service that has been running in production for a month with no issues. Last evening, the service failed after all queries using .execute were throwing this error. Restarting the service resolved the issue.

Let me know if I can provide more information.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
rallfocommented, Dec 17, 2018

@dekyfin are you getting errors with your code? With manual .prepare() statement is not cached, it’s up to you to reuse it or not

Actually it does appear that manual prepare() statements are both cached and retrieved from the connection._statements LRU cache. Which is the source of the bug.

In the case of a manual .prepare() then PreparedStatementInfo.close() a CloseStatement is sent but the statement entry is not removed from the cache. Leaving an entry to the invalid prepared statement.

Replication: prepare() an insert query, execute() and close() it. Attempting to prepare() and execute() the same SQL a second time triggers this issue.

Solution:

  • either stop caching manual prepare() statements
  • or make PreparedStatementInfo.close() correctly delete the statement from the connection._statements cache
0reactions
xWTFcommented, Jan 14, 2022

TL;DR for anyone facing the same issue: rallfo found the exact cause of this, simply use Connection.unprepare() when you’ve done with your instead of PreparedStatementInfo.close() solves the problem. I’ve opened https://github.com/sidorares/node-mysql2/pull/1493 to address this, hopefully it clarifies the usage.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MySQL connection throws "Unknown prepared statement ...
If prepare failed, that is where you would check the error messages and exit the function. I would suggest only proceeding if the...
Read more >
Unknown prepared statement handler (stmt) given to EXECUTE
An error "Unknown prepared statement handler (stmt) given to EXECUTE" means that previous PREPARE statement fails.
Read more >
Unknown prepared statement handler (X) given to ...
I have a java application calling percona mysql through a generic data access library. Every so often calls will fail with this error:-...
Read more >
43096: Unknown prepared statement handler (1) given to ...
Description: java.sql.SQLException: Unknown prepared statement handler (1) given to mysql_stmt_execute at com.mysql.jdbc.SQLError.
Read more >
php - Unknown prepared statement handler given to ...
Unknown prepared statement handler (4) given to mysqld_stmt_execute ... die('bind_param() failed: ' . htmlspecialchars($stmt->error)); } ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found