blackbeam / rust-mysql-simple

Mysql client library implemented in rust.
Apache License 2.0
661 stars 144 forks source link

Statement Cache Does Not Work Correctly With Switching Databases #359

Open winstonewert opened 1 year ago

winstonewert commented 1 year ago

I have a case where I need to run the exact same query against a number of different databases within one MySQL instance. I did this by running "use database_name" follow by my actual query in a loop. Sadly, I just get the results from the first query back over and over. It looks like this is because the statement is cached after the first query and then reused. Using the select_db function does not solve the problem. I was able to solve it by manually preparing and closing my statement.

blackbeam commented 1 year ago

Hi. I believe the statement is bound to the database at the server side. I believe the only reasonable thing we can do is to add a note in the docs.

I was able to solve it by manually preparing and closing my statement.

Another option is to invoke Conn::reset after use another_db is executed.

winstonewert commented 1 year ago

Perhaps select_db could reset the statement cache? You could also possibly identify "USE" statements and clear the cache if they are detected? I'm not sure either of those is worthwhile.