Thread

Posted on Wed Jul 20 16:54:01 2005 by jspath
mysql_auto_reconnect problems

I am using mysql_auto_reconnect to ensure that my long running process can reliably interact with mysql. Here is the code I use to execute statements and account for auto reconnects.

my $rc = $sth->execute(@$bind); if (!$rc) { if ($dbh->errstr && $dbh->errstr =~ /(?:lost connection|mysql server has gone away)/i) { $rc = $sth->execute(@$bind); if (!$rc) { return; } else { return 1; } } else { return; } } else { return 1; }

I've tested this functionality by establishing the connection, then killing the database process, then attempting to execute a statement. The first execute fails as expected, with a "lost connection" error, and DBD::mysql automatically reestablishes the connection so that the 2nd execute will work.

The problem is, when I leave it for long periods, the first execute fails, AND the 2nd execute fails, both with the "MySQL server has gone away." error message. mysql_auto_reconnect does not seem to have an effect in this case.

I don't understand the point of mysql_auto_reconnect if it doesn't reconnect in the "server has gone away" case.

Direct Responses: 766 | Write a response
Posted on Wed Jul 20 23:31:30 2005 by itub in response to 764
Re: mysql_auto_reconnect problems
Upgrade your version of DBD::mysql. This is a bug that was fixed in version 2.9006; see the first item in the changelog: http://search.cpan.org/src/RUDY/DBD-mysql-2.9006/ChangeLog
Direct Responses: 769 | 774 | Write a response
Posted on Thu Jul 21 14:11:21 2005 by islandyorker in response to 766
Re: mysql_auto_reconnect problems
ATTN.Dear Friend DR.Hassan Aliou. RE: TRANSFER OF ($26,000.000.00 USD} TWENTY SIX MILLION DOLLARS. I want to transfer ($26,000.000.00 USD) Twenty six million United States Dollars from a Diamond trust Bank here in Dakar Senegal to oversea account. First, I must solicit your strictest confidence in this transaction.This is by virtue of it's nature as being utterly confidential. I am sure and have confidence of your ability and reliability to prosecute a transaction of this great magnitude. I solicit your assistance to enable us transfer the said amount into your safe account for unward investment.You can either provide us with an existing account or to set up a new Bank account immediately to receive this money, even an empty a/c can serve to receive this money, as long as you will remain honest to me till the end for this important business trusting in you and believing that you will never let me down either now or in future. I am the personal account officer to a great late INDUSTRIALIST who has an account in one of the top banks here in Senegal. The account was opened in 1996 and he died in 1998 without a written or oral WILL and since 1998 nobody has operated on this account again hence the money is floating and if I do not remit this money out urgently it will be forfeited for nothing since the bank has been sold on Wed, 19 Mar 2003 to another bank and the management of the bank is now ready to release the fund to any foreigner who has the correct information of the account before fully handing over the management to the new bank. The owner of this account is a foreigner and no other person knows about this account or anything concerning it, the account has no other beneficiary and until his death he was the manager of the company . My investigation through the National i mmigration department proved to me as well that he was single as at the point of his entry into the Republic of senegal. The amount in this account is USD$26 Million ( Twenty six Million United States Dollars ) only. As a matter of fact, I have decided to transfer this fund abroad for investment. Your assistance as a foreigner is necessary because the management of the bank will welcome any foreigner who has correct information to this account which I will give you immediately, if you are interested to do this business with me. There is no risk to this business. With my position and my personal contact with the manager of the bank, the money can be transferred to any account you can provide with assurance that this money will be intact pending our physical arrival in your country for sharing. We will start the first transfer with Six million [$6,000.000]. Upon successful transfer without any disappointment from your side, we shall re-apply for the payment of the remaining balance of $20 million to your account. I am only contacting you as a foreigner because this money can not be approved to a local person here, but can only be approved to any foreigner who has the correct information of the account which I will provide to you. So you should provide me with your correct account detail where you will like the fund to be transferred or you can set up a new account for the transfer even an empty account is ok. At the conclussion of the transfer you will take 35% for you, 5% for any expenses incurred in this business and the remaining 60% for me. As soon as I hear from you and upon your strong assurance that you will not let me down once the fund goes into your account I will then start to process the transfer of the fund to your account without further delay. Contact me urgently for further detail and my alternative email:hassan_aliou02@yahoo.co.uk. Thanks. Yours Faithfully, DR.Hassan Aliou.phn no.+221-470-52-44..
Write a response
Posted on Thu Jul 21 20:22:00 2005 by jspath in response to 766
Re: mysql_auto_reconnect problems
D'oh. I was afraid of that. Hopefully my hosting service will install the newer version for me.

I'm curious, how hard is it to install a module like DBD::mysql into a home directory? I have installed modules like this as root, but never as a normal user. And if my hosting service won't upgrade the module, I'll be forced to do it myself.

Anyways, thanks for your help! It's nice to know that I wasn't the cause of the problem.
Direct Responses: 776 | Write a response
Posted on Thu Jul 21 22:15:25 2005 by itub in response to 774
Re: mysql_auto_reconnect problems

In general, there is no problem with installing modules to a home directory. However, in the case of DBD::mysql, it can be tricky, because you need to compile it, and you need to have the mysql header files and libraries available. Some hosting providers don't provide compilers, which complicates things. If you have a system that is very similar to the one used by the hosting provider, you might be able to compile it at home and then just copy the files with some luck.

Other than that, perhaps you can work around it in your perl code by reconnecting explicitly. You can use $dbh->ping to check if the conection is live, and then reconnect if it's not. Since you need to check all the time, it would be a good idea to encapsulate $dbh in a method or subroutine. For example,

# untested code... sub dbh { my $self = shift; unless ($self->{dbh} and $self->{dbh}->ping) { $self->{dbh} = DBI->connect('dbi:mysql:test') or die "couldn't connect! "; } return $self->{dbh}; }
Direct Responses: 778 | Write a response
Posted on Fri Jul 22 04:48:37 2005 by jspath in response to 776
Re: mysql_auto_reconnect problems

Thanks for your help itub! I ended up being able to build and install the latest version of DBD::mysql in my home directory and now the auto reconnect is functioning as expected. I guess I should RTFChangelog next time! :D

About your idea of reconnecting explicitly. I found one downside to this method, which I was using before I found out about mysql_auto_reconnect. Any statement handles that I had prepared before the disconnect needed to reprepared after reconnecting. When using mysql_auto_reconnect, it seems that I do not need to reprepare any of the statements. I could be wrong about this, but this is what I understood to be the case from my testing.

And one last note... it would be nice if mysql_auto_reconnect reconnected AND reexecuted statements. It seems a little silly to me to have to EXECUTE, test for a lost connection, then EXECUTE again if the connection dropped. I understand that this could be "dangerous", as losing the connection does not necessarily mean that the statement wasn't executed. At the same time, for the application I am developing, it's fine if the statement happens to execute twice, especially given that this would be a VERY VERY rare case. Just a suggestion to anyone who is listening ;)

Direct Responses: 779 | Write a response
Posted on Fri Jul 22 16:38:43 2005 by itub in response to 778
Re: mysql_auto_reconnect problems

Thanks for the tips about the explicit reconnects; I didn't know because I had never tested that in practice. ;-)

it would be nice if mysql_auto_reconnect reconnected AND reexecuted statements.

This is what the documentation claims, and it seems to work for me...

DBD::mysql has a "reconnect" feature that handles the so-called MySQL "morning bug": If the server has disconnected, most probably due to a timeout, then by default the driver will reconnect *and attempt to execute the same SQL statement again*. However, this behaviour is disabled when AutoCommit is off: Otherwise the transaction state would be completely unpredictable after a reconnect.

Direct Responses: 782 | Write a response
Posted on Fri Jul 22 21:26:24 2005 by jspath in response to 779
Re: mysql_auto_reconnect problems

by default the driver will reconnect *and attempt to execute the same SQL statement again*

This has not been my experience.

After testing this repeatedly, I have determined that the 1st execute that fails due to a lost connection does nothing. The connection is not even re-established!!!

If I attempt the same execute again, it is this 2nd execute that reconnects and successfully executes the statment.

Is your experience with mysql_auto_reconnect different?

What would be ideal is for connection issues to be entirely contained in DBD::myql, so that I can issue a single execute call, and it will always work, regardless of the current database connection status.

Write a response