开发者

MySQL TAdoConnnection connection "connected" property incorrectly set True

I have an application which connects to a MySql database using Delphi's TAdoConnection object. This 开发者_开发百科is a very query intensive application. So I create the connection and keep it open to avoid the high resource expense of open/closing database connections. But obviously problems can arise (database restart, network connection failure, etc). So I have built in code to free my database object, recreate it, and reconnect when queries fail.

I have a common function to connect to the database. The relevant code is this:

try
  AdoConnection.open;
  result := Adoconnection.Connected
except
  result := False;
  ......
end;

I ran some test by turning on and off the MySql database. Everything works fine if the database is off on application startup (i.e. it properly throws an exception). However, if I turn off the database after the application has already successfully connected, subsequent re-connections do not throw exceptions, and additionally falsley report true for AdoConnection.Connected. I am sure the connection object had been freed/recreated first.

It seems there is some sort of caching mechanism going on here (most likely at the hardware/driver level - not application level). Anyone have any ideas?


I observed this also.

Ideas for dealing with it:

  1. If you get an error on running a query then check if it's a connection issue and if so try to reconnect and then run the query again.
  2. If your program uses a timer and waits a while between running batches of queries then perform a simple query (maybe "select now()") before each batch and if this fails then try to reconnect.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜