are session variables saved after query execution when using connection pool? - mysql

I am struggling to understand how session variables are managed when using a connection pool on every documentation I am reading the definition of session variable is
Session variables
Session variables are set in the scope of your session with the MySQL
server.
A session starts with a connection to the server and ends when the
connection is closed.
Variables go out of scope once the connection is terminated.
for example :
http://www.java2s.com/Tutorial/MySQL/0201__Procedure-Function/LOCALSESSIONANDGLOBALVARIABLESINMYSQL.htm
if we're using a connection pool the connection is not really getting close or terminated
does it mean we need to reset the session variable at the end of the transaction?

Related

Regarding MySQL Aborted connection

I'm looking into aborted connection -
2022-11-21T20:10:43.215738Z 640870 [Note] Aborted connection 640870 to db: '' user: '' host: '10.0.0.**' (Got timeout reading communication packets)
My understanding is that I need to figure out whether it is an interactive or not connection, and increase wait_timeout (or interactive_timeout) accordingly. If it has no effect, then I'll need to adjust net_read_timeout or net_write_timeout and see.
I'd like to ask:
Is there a meta table that I can query for the connection type
(interactive or not)?
There are how-to's on the internet on adjusting wait_timeout (or
interactive_timeout) and all of them have rebooting the database as
the last step. Is that really required? Given that immediate effect
is not required, the sessions are supposed to come and go, and new
sessions will pick up the new value (after the system value is set),
I suppose if there is a way to track how many connections are left
with the old values, then it will be ok?
Finally, can someone suggest any blog (strategy) on handling aborted
connection or adjusting the timeout values?
Thank you!
RDS MySQL version 5.7
There is only one client that sets the interactive flag by default: the mysql command-line client. All other client tools and connectors do not set this flag by default. You can choose to set the interactive flag, because it's a flag in the MySQL client API mysql_real_connect(). So you would know if you did it. In some connectors, you aren't calling the MySQL client API directly, and it isn't even an option to set this flag.
So for practical purposes, you can ignore the difference between wait_timeout and interactive_timeout, unless you're trying to tune the timeout of the mysql client in a shell window.
You should never need to restart the MySQL Server. The timeout means the client closed the session after there has been no activity for wait_timeout seconds. The default value is 28800, which is 8 hours.
The proper way of handling this in application code is to catch exceptions, reconnect if necessary, and then retry whatever query was interrupted.
Some connectors have an auto-reconnect option. Auto-reconnect does not automatically retry the query.
In many applications, you are borrowing a connection from a connection pool, and the connection pool manager is supposed to test the connection before returning it to the caller. For example running SELECT 1; is a common test. The action of testing the connection causes a reconnect if the connection was not used for 8 hours.
If you don't use a connection pool (for example if your client program is PHP, which doesn't support connection pools as far as I know), then your client opens a new connection on request, so naturally it can't be idle for 8 hours if it's a new connection. Then the connection is closed as the request finishes, and presumably this request lasts less than 8 hours.
So this comes up only if your client opens a long-lived MySQL connection that is inactive for periods of 8 hours or more. In such cases, it's your responsibility to test the connection and reopen it if necessary before running a query.

What is the proper way to close a sql db connection?

I am using sql with Go, using the mysql driver and database/sql.
While reading about it I found that this driver handles the things related to connection pooling itself and db, err := db.Open("conn string") returns the connection pool instead of a single connection and when we use db.Query("some query"), it actually picks a free connection from connection pool (if no connection in pool is free it opens a new one) and uses it.
Please correct me, if I am wrong in my above understanding.
Now my question is, what happens when I call db.Close():
Will it close all connections in the connection pool?
Will it close then even if they they are passed to different goroutines and are being used there?

Can connection being established before procedures hooked on startup event being executed in MySQL?

I need to do some cleanups and custom initialization before the MySQL server can accept connections, Can connection being established before
init_file = '.../startup.sql';
is executed?
If not, is there any alternative methods to make it possible, like temporary global variables that reset every time the server starts?
You can set a global variable in the mysqld config file and have your tasks change that value after they complete. It would be reset every time the server starts. That would work as the flag you are looking for. You could also use an EVENT to check for that.
However, you would be better scripting the start up process of your MySQL service to carry out your cleanup tasks once it returned that it was up and establishing a connection locally to execute them. You could, if it's critical, have this same process enable remote connections only after it had executed its tasks.

How to fetch MYSQL connection time

How to find out MYSQL connection time not connection_timeout?
I am able to find out the connection timeout value using
SHOW VARIABLES LIKE 'connect_timeout'
But I am not able to get the variable for connection time.

Mysql connection timeout in doctrine

I have developed a application which send large number of emails to different users in batches. The most common problem I faced in this application is the problem of mysql connection timeout. In between the batches when there is no queries executed in the previously opened connection and connection remained idle for long time, mysql itself close the connection. After sending the current batch when I again try to execute any sql query it gives me mysql connection error.
Right now I am using mysql_ping($conn) function to check whether the connection id timedout or not. If the connect is timed out I connect again with mysql_connect() function. Now I am moving to doctrine rather than native PHP function. Is there a recconnect() function in Doctorine as well ?
Never faced this problem with my batch actions but I think you could probably do something like this in those places in your code where you think there's a risk the connection will time out:
// Fetch current connection
$conn = Doctrine_Manager::connection();
if(!$conn) {
// Open a new connection
$conn = Doctrine_Manager::connection('mysql://username:password#localhost/whatever', 'connection 1');
}
http://docs.doctrine-project.org/projects/doctrine1/en/latest/en/manual/connections.html
There's a custom bundle that allow connection to reopen:
https://github.com/facile-it/doctrine-mysql-come-back
You can set a maximum amount of retry, or you can manually call method retry on Connection!