I have created a small kivy app. Here, I have made use of mysql database. When the kivy app gets killed or destroyed ,I want to close the database connection. How will I know if my kivy app is running in background or destroyed.
to close database connection, you can set wait timeout parameter with minimum value which have 8 hours by default. So if database will have any idle connection, same will be killed by MySQL server itself.
Let's say, if we set wait_timeout 60 seconds. then idle connection will be active for 60 seconds only. after 60 seconds, connection will be released. To change required variable, Please use below steps:
mysql -uUSER -pPASSWORD
then
set global variables wait_timeout=60;
or you can also set this variable for your kivy app only with session variables
Related
I have a MySQL 5.7.16 running on a Centos 6. I read about these two configuration variables as,
interactive_timeout - interactive time out for mysql shell sessions in
seconds like mysqldump or mysql command line tools.
wait_timeout - the amount of seconds during inactivity that MySQL will
wait before it will close a connection on a non-interactive connection
in seconds.
I set both these variables to 120 seconds in my server which means that after this time, both interactive (mysql shell) and non-interactive (like front end applications) should have their connections get disconnected automatically, if they are in "sleep" mode.
I observed some sessions from the application and other TCP/IP connections from different IDEs like MySQL Workbench running even after 120 seconds. Sometimes they go more than 200 seconds.
Are there any other settings I need to do in my configuration file?
Did you set the GLOBAL variable to 120?
Use
SET GLOBAL wait_timeout = 120
insted
SET wait_timeout= 120
remember that value is refreshed only for new connections.
Run:
SELECT ##global.wait_timeout, ##session.wait_timeout;
to check the real value.
When manually running an sql query from an app server, if a user just clicks the X to close the window instead of using F12 or the menu to close the connection to the DB, what kind of negative impact can there be?
.
Is there an automatic timeout for connections?
The timeout of a MySQL connection is defined in the /etc/my.cnf file in the wait_timeout server system variable. It's defaulted to 28800 (seconds) - we usually set it for our clients to a very low value - ranging from 10 seconds to 60 seconds. So, if you leave a connection unclosed, then it'll close, by default, after 28800 seconds (that's very high if you have a large number of clients using the app).
In your case, interactive_timeout doesn't apply, since you are not really using the console.
I'm using Net-SNMP on an Ubuntu 14.04 server to capture SNMP traps in snmptrapd, which I've set up (using "perl do '/path/to/traphandler.pl'" in the /etc/snmp/snmptrapd.conf) to call a Perl::DBI script to insert data into a mySQL database. Everything has been running fine since 16th March, then about 9am yesterday (6th April - Bank Holiday - typical) the database updates stopped, though the syslog shows that the traps were still coming in.
I can't see anything obvious in /var/log, so I'm wondering whether the database connection simply expired and closed. Does that happen? I stopped and restarted snmptrapd and it all started working again, which makes me think that was the cause. How would I check?
Yes it is 28800 seconds
connect_timeout=28800
wait_timeout=28800
interactive_timeout=28800
You can set the values in /etc/my.cnf
If you want to change it then you have to run this query:
SET GLOBAL interactive_timeout = 100; //Change it if you want
SET GLOBAL wait_timeout = 100;
If a service needs a database connection, the best is to check in the main loop or before each query if the connection is still available.
If the connection is no longer available or if it doesn't work anymore, you can close it properly and reconnect.
This way, the deamon continues to work even if the database server is temporarily unavailable. For instance, the service will continue to work even if the database server restarts.
Not exactly an answer to your question but you can handle lost db connections without altering a timeout or checking the connection before each query.
If you access MySQL databases with DBI you can set the flag mysql_auto_reconnect ( see DBD::mysql )
This attribute determines whether DBD::mysql will automatically reconnect to mysql if the connection be lost. This feature defaults to off; [..]
Setting mysql_auto_reconnect to on is not advised if 'lock tables' is used because if DBD::mysql reconnect to mysql all table locks will be lost.
Example:
my $dbh = DBI->connect(
$host,
$user,
$password,
{
mysql_auto_reconnect => 1
}
) or die("DB connect failed: : $DBI::errstr");
for some reason when I open a connection the the Percona MySQL database on my HostGator website, after fetching the query, it will disconnect/ close the connection about 10 seconds later.
I typically wouldn't care, but HeidiSQL freezes up, preventing exporting or sorting the returned rows with it's UI unless I connect again.
Any thoughts on making the connection last longer? is it something I can do myself, or will it require a dedicated server or some upgrade? (I'm currently on a shared one). Thanks!
Sounds like it may be the "wait" timeout on the MySQL connection.
SHOW VARIABLES LIKE 'wait_timeout'
That's the amount of time (in seconds) that MySQL will leave the session (the database connection) open while it's idle, waiting for another statement to be issued. After this amount of time expires, MySQL can close the connection.
You should be able to change this for a session, to change the timeout to 5 minutes
SET wait_timeout = 300
Verify the setting with the SHOW VARIABLES statement again.
NOTE: This is per connection. It only affects the current session. Every new connection will inherit their own wait_timeout value from the global setting.
(This is only a guess. There's insufficient information in the question to make a precise diagnosis. It could be something other than MySQL server that's closing the database connection, e.g. it could be your connection pool settings (if you are using a connection pool).
I've done the following:
SET GLOBAL wait_timeout = 5;
SET SESSION wait_timeout = 5;
SET GLOBAL interactive_timeout = 5;
SET SESSION interactive_timeout = 5;
But when I run a time consuming query it's still losing the connection at 600 seconds - not 5 seconds. I'm doing the queries in the same MySQL Workbench tab, one after the other, so it should all be in the same session..
I also tried updating C:\Program Files (x86)\MySQL\MySQL Server 5.1\my.ini and adding wait_timeout=5 and... nothing.
Any ideas?
Also, why, when one time consuming query is running on 127.0.0.1 in one tab, can't I do SHOW FULL PROCESSLIST in another tab?
wait_timeout only controls how long a connection can be idle before the server closes it. If you're executing a query that takes a long time, then that variable has nothing to do with your issue.
There is nothing built in to MySQL which will kill active running queries after a set time. So there is something else causing that to happen. And you need to give more information.
When you say "it's closing the connection" What exactly happens? Do you receive an error in your program, if so what is it? How are you running the program?