Database connection error when displaying web pages over a period of time to visit again

  I encountered this problem before, and after a long time to think to solve it, because it did not have much impact, is nothing more than a re-visit the issue, then there is a time to observe the operation of the website and found this quite serious problem , if you have been using, it does not cause problems if the rest a middle, and then it will prompt data query fails, try again later, before I always thought it was occasionally a database problem, the problem is definitely not found in the database , well, to start addressing.

  Probably find a bit laws, I found that as long as a break using a web query again will error, this error is the failure of the back-end database queries reported, then look at the back-end code, repeatedly read it several times and found no problem, then why would being given it, search on the net, some people say the problem is timeout settings database, I went to see a bit timeout data from the database, shown below:

After this data is normal ah, how it will lead to a few minutes, connecting on the failure of it? Here I am back end is written in python, connect to the database using SQLALchemy, SQLALchemy reconnection mechanism is in default two hours, this is not a problem, inconvenience debugging code on the server, so I was in my own virtual machine debugging a bit and found the problem connection failure will not appear on the virtual machine, on both sides of the code is the same, the only difference is the database, virtual machine using a local database server using another server database, then I realized the problem, timeout before the query is the current server, rather than using the server to query the database server timeout discovery data is normal:

Later inquiries global variables found a problem here, SHOW GLOBAL VARIABLES LIKE '% timeout';

Here timeout and interactive_timeout are 100s, there will be no wonder every once in a while a database connection is interrupted it, and later ask the reason while this parameter is set, as if the data collection terminal using labview written, in order to prevent too much occupied connection, so the setting value is very short, since the database can not be changed is provided, it is our code change, SQLAlchemy has a configuration is provided to reconnect mechanism: SQLALCHEMY_POOL_RECYCLE, the value is set to a value smaller than the timeout and, therefore, SQLALCHEMY_POOL_RECYCLE = 90, restart the application server, the problem is solved.

Seen from this problem, in fact, sometimes the error is caused by a number of reasons, but because of the location query error, leading to spending a lot of extra time, like this one, should have information check the database server, the result of the investigation is to apply information server (in fact, the contents of both database server is the same, I do master-slave synchronization, and deletions separate inquiries have been trying to change the database, not separately here, but mistakenly believe that their own consciousness separated).

 

Guess you like

Origin www.cnblogs.com/maodoudou/p/11425019.html