Posted on 2006-08-30 09:43:37-07 by stick
DBD::Mulitplex + Apache::DBI problem
Hi, i am using DBD::Multiplex for mysql master/slaves connection. In startup.pl i am initiating master and slave connection like below
$Apache::DBI::DEBUG=1; Apache::DBI->connect_on_init("DBI:mysql:database=$MASTER_DATABASE;host=$MASTER_DATABASE_HOST",$MAST +ER_DATABASE_USER,$MASTER_DATABASE_PASSWORD, { PrintError => 1, # warn( ) on errors RaiseError => 0, # don't die on error AutoCommit =>1 , # commit executes immediately } ); ### keep slave connection here Apache::DBI->connect_on_init("DBI:mysql:database=$REPLICA1_DATABASE;host=$REPLICA1_DATABASE_HOST",$ +REPLICA1_DATABASE_USER,$REPLICA1_DATABASE_PASSWORD, { PrintError => 1, # warn( ) on errors RaiseError => 0, # don't die on error AutoCommit =>1 , # commit executes immediately } );
In each page script starting i am calling dbconnect mehtod which have
sub dbConnect { my (@dsns) = ( "dbi:mysql:dbname=$REPLICA1_DATABASE;host=$REPLICA1_DATABASE_HOST;mx_id=dbslave1", "dbi:mysql:dbname=$MASTER_DATABASE;host=$MASTER_DATABASE_HOST;mx_id=dbmaster" ); my ($dsn) = 'dbi:Multiplex:'.join('|', @dsns); my %attr = ( 'mx_master_id' => 'dbmaster', 'mx_exit_mode' => 'first_success_random', 'mx_connect_mode' => 'ignore_errors', 'mx_error_proc' => \&ErrorProcedure, ); $DBH ||= DBI->connect($dsn,$MASTER_DATABASE_USER, $MASTER_DATABASE_PASSWORD, \%attr) or die $DBI::errstr; if(!$DBH){ print "Error Connecting to the database: $DBI::errstr\n"; } }
1. Whenever dbConnect called instead of using existing Apache::DBI connection, each time it is creating new connection. I have used Apache::DBI::DEBUG=1 in startup.pl when i check my error log it is creating new connection each time. Is any specific thing has to make to take the existing apache DBI connection. 2. when i check my last dbh handle using $dbh->lasth, it is displaying master db host and when i check the code it is always going to master(transaction code loop) instead of slave. Can you help to solve he above two problems.
Direct Responses: Write a response
Perl Weekly newsletter
A free weekly newsletter for people who are busy to read all the blogs. click here to check it out.