Context
We have a (Java, JPA:EclipseLink) application connected to MySQL.
There are cases where we want to fetch thousands of results, and perform an action per-result. We do not want to hold the whole result set in-memory.
JPA:EclipseLink's queries with a "scrollable cursor" allow us to iterate lazily through a result set, keeping only one result in-memory at a time.
It is my understanding that these queries with a "scrollable cursor" are implemented in MySQL as a cursor.
Problem
It is the consumer's responsibility to "close" any MySQL cursor.
We have trouble guaranteeing that the cursor gets closed. Naturally we close the cursor when we reach the end of the result set, but there are problems that would prevent us iterating as far as that:
- Exception thrown
- Program exit
- Intentional early-exit of iteration
- e.g. where the question is simply "is a non-zero amount of results returned?"
Anecdotes
We have observed before that: leaving many cursors open, uses up available connections in our database connection pool. Once the limit is reached: subsequent queries fail because no further database connections are available.
We have a suspicion that completing a transaction will close any open database cursors participating in that transaction. If so: this would provide a very useful safeguard. However we have yet to measure whether this does indeed happen, and haven't yet worked out what level of transaction provides this guard (MySQL transaction? JPA transaction? Spring transaction?). An answer to the question below, would enable us to measure this…
Question
Is it possible in MySQL to show a list of all currently-open database cursors? If so: how?