Fix leaking connections in fetch_optional (#2647) (#3194)

When using the 'Any' driver with MySQL backend, fetch_optional
does not return the connection to the pool if no results
are returned from the query. This is due to not all of the packets
being read from the underlying stream.

This fix continues to read result packets from the stream until they
have all been exhausted (just like the normal MySql drivers
implementation of fetch_optional). In general, a better refactoring would
be to call the MySQL fetch_optional code in the Any driver, rather than
re-implementing and duplicating code.
This commit is contained in:
Dan Griffin 2024-04-25 04:18:55 +02:00 committed by GitHub
parent a5d7fffc1b
commit c82bf43e98
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -103,8 +103,10 @@ impl AnyConnectionBackend for MySqlConnection {
let stream = self.run(query, args, persistent).await?;
futures_util::pin_mut!(stream);
if let Some(Either::Right(row)) = stream.try_next().await? {
return Ok(Some(AnyRow::try_from(&row)?));
while let Some(result) = stream.try_next().await? {
if let Either::Right(row) = result {
return Ok(Some(AnyRow::try_from(&row)?));
}
}
Ok(None)