Thanks, Perrin, I'm sure you're right just don't see where this happens.
It's hard to extract the part of the code because my configs look wild
:)
One sh script starts/stops/restarts 3 servers with 1 config for proxy
and 1 config full of <Perl> sections - for two back-end servers.
Yes, I do call mysql from <Perl> section of conf-file.
One "thread" of this execution looks like
require "$counter_root/startup-image.pl";
inside this file:
@Counter::INI::DBopendata = (
base => 'ranks',
host => "localhost",
port => 3306,
"username" => "....",
"password" => "....",
);
...
# this is because I use the same startup-image.pl for standalone scripts
# with $no_GATEWAY_INTERFACE = 1;
require DBI;
unless ($no_GATEWAY_INTERFACE) {
# Apache::DBI should be before DBI but after Apache::Status
require Apache;
require Apache::Status;
require Apache::DBI;
# here's a workaround to override DBI methods with Apache::DBI ones
{
package DBI;
$connect_via = "Apache::DBI::connect";
}
.... lots more
}
then back to conf:
my $db = MySQL::Tools->dbopen(@Counter::INI::DBopendata,
connect_method => "connect") or die $DBI::errstr;
print "image startup thread:".$db->dbh->{mysql_thread_id}."
".scalar($db->dbh)."\n";
# this is just an object-oriented interface to SQL language
my $res = $db->select(
Tables => Counter::Folders->table_name,
Columns => "ICON",
DISTINCT => 1,
SQL_CACHE => 1,
) or die "reading counter images: ".MySQL::Tools->errstr;
ref($res) && @$res or die "reading counter images: no images found\n";
...stuff...
$db->dbclose;
undef $db;
I'm calling this with connect_method => "connect" and really, I see this
result of the upper print:
image startup thread:717 DBI::db=HASH(0x8746500)
- means I really use DBI and dbclose is just a simple $dbh->disconnect
in this case. And I really don't see thread 717 any more
Actually there should've been a 2nd line (like I saw while printed to
file, I won't change thread id's here)
image startup thread:718 DBI::db=HASH(0x8746500)
because of the 2nd pass. Dunno, may be there is some problem with this
2nd pass... I don't actually know why it happens. But it's still DBI and
disconnects as DBI::db handler should disconnect.
OK, and after that the whole stuff starts, few different PerlHandler
modules that use the same dbh like this
sub handler {
...
$Counter::db and $Counter::db->reconnect;
unless ($Counter::db) {
$Counter::db ||= MySQL::Tools->dbopen( @Counter::INI::DBopendata )
or croak MySQL::Tools->errstr;
}
MySQL::Tools->dbopen returns $db and $db->dbh is DBI::db object (or
Apache::DBI::db in case it overrides DBI)
dbopen() finishes with
$self->reconnect or return;
$self;
}
and reconnect() actually opens the connection:
sub reconnect {
my $self = shift;
my $old_dbh = $self->{dbh} ? "$self->{dbh}" : "";
my $old_thr = $self->{dbh} ? ($self->{dbh}->{mysql_thread_id} || 0) :
0;
$self->{"dbh"} and $self->{"dbh"}->disconnect;
$self->{"dbh"} = DBI->connect(
sprintf("dbi:%s:database=%s;host=%s;port=%s",
$self->{"driver"}, $self->{"name"}, $self->{"host"}, $self->{"port"}),
$self->{"username"}, $self->{"password"}, {
AutoCommit => 1, # 0 makes error in mysql
PrintError => 0,
RaiseError => 0,
($self->{"connect_method"} ? (
dbi_connect_method => $self->{"connect_method"}
) : ()),
}) or return;
my $new_thr = $self->{dbh} ? ($self->{dbh}->{mysql_thread_id} || 0) :
0;
!$old_dbh || $old_dbh eq "$self->{dbh}" or
warn "reconnect: reconnected successfully\n";
if (Apache->request && $old_thr != $new_thr) {
warn "reinit ($$) thread new: $new_thr old: $old_thr
$self->{dbh}\n";
}
... reading tables with column properties ...
$self;
}
I've just made more clear test print and the last 'warn' prints much
like I sent before but now it shows how wrong mysql thread (719 - same
for 5 server childs) is being replaced with the new threads:
old: 0 means first connection for this child, no previous thread
[Thu May 27 16:00:23 2004] [error] reinit (13339) thread new: 719 old:
0 Apache::DBI::db=HASH(0x8389fa4)
[Thu May 27 16:00:23 2004] [error] reinit (13341) thread new: 719 old:
0 Apache::DBI::db=HASH(0x8389fa4)
[Thu May 27 16:00:23 2004] [error] reinit (13340) thread new: 719 old:
0 Apache::DBI::db=HASH(0x8389fa4)
[Thu May 27 16:00:23 2004] [error] reinit (13342) thread new: 719 old:
0 Apache::DBI::db=HASH(0x8389fa4)
[Thu May 27 16:00:23 2004] [error] reinit (13343) thread new: 719 old:
0 Apache::DBI::db=HASH(0x8389fa4)
[Thu May 27 16:00:23 2004] [error] SQL execute error: LISTFIELDS
counter_sitegroups
Lost connection to MySQL server during query
at /host/counter/lib/Counter/Common.pm line 502
[Thu May 27 16:00:23 2004] [error] TablesProperties: Lost connection to
MySQL server during query
at /host/counter/lib/Counter/Common.pm line 502
[Thu May 27 16:00:23 2004] [error] reinit (13344) thread new: 719 old:
0 Apache::DBI::db=HASH(0x8389fa4)
[Thu May 27 16:00:24 2004] [error] reinit (13346) thread new: 723 old:
0 Apache::DBI::db=HASH(0x8389fa4)
[Thu May 27 16:00:24 2004] [error] reinit (13348) thread new: 725 old:
0 Apache::DBI::db=HASH(0x8389fa4)
[Thu May 27 16:00:24 2004] [error] reinit (13351) thread new: 728 old:
0 Apache::DBI::db=HASH(0x8389fa4)
[Thu May 27 16:00:24 2004] [error] ListTables: Lost connection to MySQL
server during query
at /host/counter/lib/Counter/ShowCounter.pm line 204
[Thu May 27 16:00:24 2004] [error] reinit (13353) thread new: 730 old:
0 Apache::DBI::db=HASH(0x8389fa4)
[Thu May 27 16:00:24 2004] [error] reinit (13345) thread new: 722 old:
0 Apache::DBI::db=HASH(0x8389fa4)
[Thu May 27 16:00:24 2004] [error] reinit (13347) thread new: 724 old:
0 Apache::DBI::db=HASH(0x8389fa4)
[Thu May 27 16:00:24 2004] [error] reinit (13349) thread new: 726 old:
0 Apache::DBI::db=HASH(0x8389fa4)
[Thu May 27 16:00:24 2004] [error] reinit (13350) thread new: 727 old:
0 Apache::DBI::db=HASH(0x8389fa4)
[Thu May 27 16:00:24 2004] [error] reinit (13341) thread new: 731 old:
719 Apache::DBI::db=HASH(0x8389fa4)
[Thu May 27 16:00:24 2004] [error] reinit (13352) thread new: 729 old:
0 Apache::DBI::db=HASH(0x8389fa4)
[Thu May 27 16:00:24 2004] [error] reinit (13339) thread new: 732 old:
719 Apache::DBI::db=HASH(0x8389fa4)
[Thu May 27 16:00:24 2004] [error] ListTables: Lost connection to MySQL
server during query
at /host/counter/lib/Counter/ShowCounter.pm line 204
[Thu May 27 16:00:24 2004] [error] reinit (13342) thread new: 733 old:
0 Apache::DBI::db=HASH(0x8389fa4)
[Thu May 27 16:00:24 2004] [error] reinit (13340) thread new: 734 old:
0 Apache::DBI::db=HASH(0x8389fa4)
[Thu May 27 16:00:24 2004] [error] reinit (13388) thread new: 735 old:
0 Apache::DBI::db=HASH(0x8389fa4)
Sorry, I tried to simplify this but as usual with OO - things are hidden
inside the other things ad infinitum.
$self->{"dbh"} and $self->{"dbh"}->disconnect;
in 'reconnect' sub makes nothing in case of Apache::DBI and is supposed
to do reconnection if called in non-mod_perl environment. It's actually
added for some persistency, to mean 'reconnect' even not under mod_perl
DBI->connect(...) each time the PerlHandler runs is supposed to ping
(Apache::DBI::connect) and keep the previous handler if it's alive
otherwise - create a new connection
dbclose looks like
sub dbclose {
my ($self) = @_;
my $rc = $self->{"dbh"}->disconnect;
undef $_[0];
$rc;
}
and destroys MySQL::Tools object. It calls the appropriate 'disconnect'
method for dbh.
I call it only in configs (no Apache::DBI). In PerlHandler modules I
have only
END { eval{ $Counter::db->dbclose } if $Counter::db }
Guys, I'm sorry - lots of stuff here and not an easy reading.
To remind the problem - few first Apache childs manage to connect the
same mysql thread at startup. Then they all fail, reconnect and live
happily ever after :)
Thanks for any input here!
Mike Blazer
Perrin Harkins wrote:
>
> On Thu, 2004-05-27 at 15:20, Mike Blazer wrote:
> > See, in the very beginning - 3 child processes started and connected to
> > the same mysql thread_id
> >
> > None of my processes ever disconnects, they all use Apache:DBI and
> > everything is checked to be correct.
> > Seems like it's mysql error in dispatching threads?
>
> Usually it's something opening a database connection before apache
> forks. How about showing us the parts of your startup code that deal
> with database connections?
>
> - Perrin
--
==================================
Mike Blazer
[EMAIL PROTECTED]
==================================
--
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html