MySQL TAdoConnnection connection "connected" property incorrectly set True - mysql

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:
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.
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.

Related

Delphi +Firedac and network connection error

My app is working with MySQL database, to connection I'm using FireDAC components. Last time I have got a network problem, I test it and it is looks like (time to time) it losing 4 ping request. My app return me error: "[FireDAC][Phys][MySQL] Lost connection to MySQL server during query". Now the question: setting fdconnection.TFDUpdateOptions.LockWait to true (default is false) will resolve my problem or make new problems?
TFDUpdateOptions.LockWait has no effect on your connection to the database. It determines what happens when a record lock can't be obtained immediately. The documentation says it pretty clearly:
Use the LockWait property to control whether FireDAC should wait while the pessimistic lock is acquired (True), or return the error immediately (False) if the record is already locked. The default value is False.
The LockWait property is used only if LockMode = lmPessimistic.
FireDAC can't wait to get a lock if it loses the connection, as clearly there is no way to either request the lock or determine if it was obtained. Therefore, changing LockWait will not change the lost connection issue, and it may slow many other operations against the data.
The only solution to your lost ping requests is to fix your network connection so it stops dropping packets. Simply randomly changing options on TFDConnection isn't going to fix networking issues.

Hibernate (Grails 2.1.1) produces socket timeout exception on insert

this is my first question on the site so be patient if something is not correctly in it.
I have a problem with Hibernate in the Grails application. The application works with MySQL database. When it tries to perform save operation for one specific domain object (this class is very simple, just several fields and many to one relation with another domain class) it hangs. I am getting socket timeout exception no matter how big max connection and max socket timeout was set during connection.
The strangest thing is that this happens only with production server. On local environment this piece of code works fine. And other database operations are also executed fine.
I tried to perform generated by Hibernate insert statement manually and it works good. But Hibernate still fails to finish the operation no matter how long I wait.
Do you have any ideas what can cause such behavior?

Data source rejected establishment of connection, message from server: "Too many connections"?

I am using MySQL database and hibernate and JSP.using hibernate select database store value and prepared view and display using Ajax.i am polling database every 1 seconds using Java script timer that called a ajax function and return the new responds,it result me an error
JDBCExceptionReporter:78 - Data source rejected establishment of connection, message from server: "Too many connections"".
Help me to sort-out the above define problem.
Make sure you close the session (and connection) after using it
Make sure the maximum connection configured for mysql is sufficient
Use of some caching layer. It is insane to hit the database every 1 second from each user.
If you are making some chat application, consider comet and server-side pub-sub solutions (jmx for example).

"Lost connection to MySQL server during query" kills Windows Service

I have developed a windows service using Delphi 2007. It connects to a remote MySql database via the internet using TAdoConnection and TAdoQuery. I have retained the default value of 30 seconds for CommandTimeout property. I also create the connection/query objects on each new query and free them when done (i.e. I don't create the database connection at startup and keep it open).
Every once in a while the service stops and the event viewer shows "Lost connection to MySQL server during query". I have everything wrapped in exceptions. My suspicion is that there is a drop in the network while the query is executing.
Anyone have any resolution/ideas?
What triggers windows to shutdown the service?
Also, I have the service "Recovery" set to restart the service but this never happens.
My next step will be to start logging when each query starts and compare this to the date/time of the shutdown. Because as of now I don't know how log this is.
This is may be not a direct answer, but I had same problem few days ago, and I have the mysql on local server, and I connect using Mydac components.
After many tries, I found the problem came from one table that has BLOB fields, I tried to query on the table like
select * from table
And I got this problem after the query fetch around 1600 rows, after few inspection I found the problem came from few records and seems they corrupted. so when I do query like
Select * from my table where id not
between 1599 and 1650
I had this problem, if I removed Not from they query which fetch only 51 records I got the error, which means there's some records are corrupted, also I did mysqlcheck but it didn't fix the problem, and I tried some other check tools and I got same result, I didn't tried to delete these records, because I need why this problem happen, but I got busy with other things so I left the server for a while.
BTW, I used MySql Query browser for trying to do the queries, because other tools will give me the errors without showing how many records fetched before mysql instance terminated unexpectedly.

NHibernate mysql connection not closed?

I am using NHibernate with mySQL 5 and I am a bit unsure whether NHibernate really closes the connection to mySQL.
This is the code I am using in order to save a Player entity to the database:
using (ISession session = SessionFactory.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
session.Save(player);
transaction.Commit();
}
}
After running this piece of code, I select the current connection count from the mySQL server using
show status like 'Conn%' ;
and the value is increased every time by 1!
So, inserting the player 10 times (=starting the program 10 times) increases the connection count by 10. But the session.IsClosed property of the NHibernate Session is false.
Do I have to do anything else in order to release the NHibernate resources and close the connection? Are the connections pooled / timed out?
It would be great if anyone could give me a hint.
I'm pretty sure the Connection is being closed. The dispose method on the ISession does just that. So it's unlikely it remains open when exiting the using block.
Also , from what I know the connection is usually opened only when writing/reading to the database . The session may be open, but that doesn't mean the connection is also open.
That, plus the fact that in the mysql documentation, it says that the keyword Connections means :
The number of connection attempts (successful or not) to the MySQL server.
and doesn't say anything about the current state of the connection (open or not), makes me believe that the connection is closed.
PS: you should be using Threads_connected to see open connections.
There is a bug in the MySQL.Data dll in certain versions that was causing this same problem for me. Make sure you update your Connector/NET to the most recent. This solved the problem for me.