LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-23-2008, 04:24 AM   #1
Fredde87
Member
 
Registered: Aug 2005
Posts: 158

Rep: Reputation: 30
Perl dbd::mysql and child process


Hi,

I am still in the early stages of learning Perl. I am having a bit of a play using a mysql databse in Perl.

I have a infinite loop which receives commands on a socket and then I call various functions and child process to process the data received. When I open the connection to the mysql database, is it better to open it at the start of the script and keep it running all the time? Or is it a better practice to open it just before I execute my query, close it and then reopen it when I need it again?

I guess I would have to open a new connection for each thread though wouldnt I as the connection can not be shared?


Thanks!
 
Old 05-23-2008, 08:01 AM   #2
derzok
Member
 
Registered: Aug 2004
Location: Ohio
Distribution: Debian, Slackware
Posts: 58

Rep: Reputation: 15
Depends on how many processes you intend on having and how often they'll occur. I have an IRC bot that keeps statistics on everything said in a chatroom. When it's in one channel without much traffic, the single connection will time out when no queries have been sent for a while (though you can change this).

So if it's receiving info on a slow time table where it might time out, connect to the database in every process. If it's going to be in constant use (like when my bot is in 14 channels) you should consider a shared connection. Especially if you've got more than one child running at a time. mysql can handle that many connections, but I'm sure you'd have better speed on a single connection.
 
Old 05-24-2008, 07:26 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Actually, last time I checked, the DBI, DBD code was not officially thread-safe, so in my threaded prog I had a separate cxn in each thr. Worked a treat.
As mentioned, if there's a long enough break between SQL statements (in a given cxn) it'll timeout.
In MYSQL use

show variables;

the last line looks like this

| wait_timeout | 28800

that's in seconds. You amend that in the my.conf file, or catch the SQL (which you should be doing anyway) and check for the explicit error:

Code:
         # Check if real SQL error or DB gone away
        if( $cfg::dbh->errstr =~ /MySQL server has gone away/ )
        {
            # Try to re-connect for later
            $error_msg = "db_update_active(): ".localtime()." ".
                                                $cfg::dbh->errstr;
            db_connect();
        }

Last edited by chrism01; 05-24-2008 at 07:28 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bugzilla - MySQL - perl - DBD::mysql install problem amchargue Linux - Software 3 09-10-2008 12:01 AM
Bugzilla - MySQL - perl - DBD::mysql install problem Runningonair Linux - Software 8 10-12-2007 12:42 AM
Problem installing Perl DBD::mysql orfiyus Linux - Software 6 03-03-2007 01:52 PM
Error compiling perl-DBD-MySQL after upgrading to MySQL 4.1 pitaro920 Linux - Software 1 01-16-2006 04:28 PM
Error with Perl DBD::mysql ginda Programming 3 03-04-2005 06:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration