mod_perl2 newbie DBI question
am 12.06.2008 20:09:00 von Brian GaberThis is a multi-part message in MIME format.
------_=_NextPart_001_01C8CCB7.63C07F87
Content-Type: text/plain;
charset="US-ASCII"
Content-Transfer-Encoding: quoted-printable
I have a MySQL database application that is used and managed by cgi-bin
scripts (CGI.pm). In development the performance was fine, but a
productin trial showed the performance to be unacceptable. I am
attempting to fix the performance by using mod_perl2 which I have never
used. I have successfully compiled and install mod_perl2 and have added
these entries into httpd.conf:
PerlModule Apache::DBI
PerlModule ModPerl::Registry
Alias /perl/ /usr/local/apache2/perl/
SetHandler perl-script
PerlResponseHandler ModPerl::Registry
PerlOptions +ParseHeaders
Options +ExecCGI
Order allow,deny
Allow from all
PerlModule ModPerl::PerlRun
Alias /perl-run/ /usr/local/apache2/perl-run/
SetHandler perl-script
PerlResponseHandler ModPerl::PerlRun
PerlOptions +ParseHeaders +GlobalRequest
Options +ExecCGI
Order allow,deny
Allow from all
I have modified scripts to work in mod_perl2, but they don't
work reliably. Sometimes they work and then they stop working and then
I have to stop and start Apache to get it working again. I am hoping if
I supply one of my scripts someone can advise me what needs to be done
to make in work fast and reliably and then I can use this as an example
to fix my other scripts. Here is the one script:
#!/usr/bin/perl -w
#use CGI qw/:standard :html3 :netscape/;
use CGI '-autoload';
use DBI();
use warnings;
use strict;
my $region =3D param('region');
my $JSCRIPT=3D<
{
parent.document.title=3Ddocument.title;
}
function validate(theForm)
{
theForm.submit();
}
JSEND
my $LOCAL_STYLE=3D<
body {
font-family:Verdana;
font-size:12px;
}
.btn {
font-family:Verdana;
font-size:9px;
color:black;
border:1px solid #000000;
margin-top:5px;
background-color:white
}
table {
font-family:Verdana;
border:1px solid #000000;
background-color:white
}
th {
font-family:Verdana;
font-size:12px;
color:black;
}
td.right {
font-family:Verdana;
font-size:12px;
color:black;
text-align:right;
}
td {
font-family:Verdana;
font-size:12px;
color:black;
text-align:center;
}
CSSEND
print header( -type =3D> "text/html" );
print start_html( -title =3D> "Title", =
-style=3D>{-code=3D>$LOCAL_STYLE},
-onLoad=3D>"changeTitle()", -script=3D>$JSCRIPT ),
br({ -clear =3D> 'all' }),
"\n";
# Connect to the database.
my $dbh =3D DBI->connect("DBI:mysql:database=3Desnap;host=3Dlocalhost",
"athena", "godess",
{'RaiseError' =3D> 1});
# Determine MySQL locks table name
my $sth =3D $dbh->prepare("SELECT * FROM region_props WHERE region =3D
'$region'");
$sth->execute();
my $ref =3D $sth->fetchrow_hashref();
$sth->finish();
my $locks_table =3D $ref->{locks_table};
my @form_vars =3D param();
if ( @form_vars > 1 ) { # if required parameters were passed
rm_lock();
}
print_form(); # Display the MySQL table
# Disconnect from the database.
$dbh->disconnect();
print end_html();
sub print_form {
my $i =3D 0;
my @clmnNames =3D ();
my @rows =3D ();
my $sth =3D $dbh->prepare("SELECT * FROM $locks_table");
$sth->execute();
while (my $ref =3D $sth->fetchrow_hashref()) {
push(@rows, td({-class=3D>'centre'},checkbox(-name=3D>"ckbx_$i",
-value=3D>"$ref->{id}", -label=3D>'')).
td({-class=3D>'centre'},$ref->{id}).
td({-class=3D>'centre'},$ref->{rcd_opener}).
td({-class=3D>'centre'},$ref->{lock_date})
);
$i++;
}
$sth->finish();
# Specified values for table column heading names
$clmnNames[0] =3D "Select";
$clmnNames[1] =3D "Id";
$clmnNames[2] =3D "User Id";
$clmnNames[3] =3D "Date";
print start_form(),
font({-face=3D>"Trebuchet MS, Arial", -size=3D>2},br(),
center(strong("Delete Record Locks"),br(),br(),
table({-class=3D>'bdr', -width=3D>'100%', -BgColor=3D>"white",
-border=3D>'0'},
Tr([th(\@clmnNames)]),"\n",
Tr([@rows])),"\n",
button(-class=3D>"btn", -value=3D>"Delete selected record locks",
-onClick=3D>"validate(this.form)"),
));
print hidden(-name=3D>'region', -value=3D>param('region')),"\n";
print end_form();
}
sub rm_lock() {
foreach ( param() ) {
if ($_ =3D~ /^ckbx_\d+$/) {
my $id2del =3D param($_);
# Delete row from $locks_table
$dbh->do("DELETE FROM $locks_table WHERE id=3D'$id2del'");
my $errno =3D $dbh->{mysql_errno};
my $errTxt =3D $dbh->{mysql_error};
if ( $errno > 0 ) {
print center(font({-face=3D>"Trebuchet MS, Arial", =
-size=3D>2,
-color=3D>"red"}),b("Error deleting row from $locks_table,
",font({-color=3D>"black"},"MySQL Error Code: $errno -
$errTxt"))),"\n",p();
}
}
}
}
------_=_NextPart_001_01C8CCB7.63C07F87
Content-Type: text/html;
charset="US-ASCII"
Content-Transfer-Encoding: quoted-printable
charset=3Dus-ascii">
6.5.7652.24">
I have a MySQL database application =
that is used and managed by cgi-bin scripts (CGI.pm). In =
development the performance was fine, but a productin trial showed the =
performance to be unacceptable. I am attempting to fix the =
performance by using mod_perl2 which I have never used. I have =
successfully compiled and install mod_perl2 and have added these entries =
into httpd.conf:
PerlModule Apache::DBI
PerlModule ModPerl::Registry
Alias /perl/ =
/usr/local/apache2/perl/
<Location /perl/>
SetHandler =
perl-script
PerlResponseHandler =
ModPerl::Registry
PerlOptions =
+ParseHeaders
Options =
+ExecCGI
Order =
allow,deny
Allow from =
all
</Location>
PerlModule ModPerl::PerlRun
Alias /perl-run/ =
/usr/local/apache2/perl-run/
<Location /perl-run/>
SetHandler =
perl-script
PerlResponseHandler =
ModPerl::PerlRun
PerlOptions =
+ParseHeaders +GlobalRequest
Options =
+ExecCGI
Order =
allow,deny
Allow from =
all
</Location>
FACE=3D"Arial">I have modified scripts to work in mod_perl2, but they =
don't work reliably. Sometimes they work and then they stop =
working and then I have to stop and start Apache to get it working =
again. I am hoping if I supply one of my scripts someone can =
advise me what needs to be done to make in work fast and reliably and =
then I can use this as an example to fix my other scripts. Here is =
the one script:
#!/usr/bin/perl -w
#use CGI qw/:standard :html3 =
:netscape/;
use CGI '-autoload';
use DBI();
use warnings;
use strict;
my $region =3D param('region');
my $JSCRIPT=3D<<JSEND;
function =
changeTitle()
{
=
parent.document.title=3Ddocument.title;
}
function =
validate(theForm)
{
=
theForm.submit();
}
JSEND
my =
$LOCAL_STYLE=3D<<CSSEND;
body {
=
font-family:Verdana;
=
font-size:12px;
}
.btn {
=
font-family:Verdana;
=
font-size:9px;
=
color:black;
=
border:1px solid #000000;
=
margin-top:5px;
=
background-color:white
}
table {
=
font-family:Verdana;
=
border:1px solid #000000;
=
background-color:white
}
th {
=
font-family:Verdana;
=
font-size:12px;
=
color:black;
}
td.right {
=
font-family:Verdana;
=
font-size:12px;
=
color:black;
=
text-align:right;
}
td {
=
font-family:Verdana;
=
font-size:12px;
=
color:black;
=
text-align:center;
}
CSSEND
print header( -type =3D> =
"text/html" );
print start_html( -title =3D> =
"Title", -style=3D>{-code=3D>$LOCAL_STYLE}, =
-onLoad=3D>"changeTitle()", -script=3D>$JSCRIPT =
),
br({ =
-clear =3D> 'all' }),
=
"\n";
# Connect to the database.
my $dbh =3D =
DBI->connect("DBI:mysql:database=3Desnap;host=3Dlocalhost",<=
/FONT>
FACE=3D"Arial"> &nbs=
p; =
"athena", "godess",
FACE=3D"Arial"> &nbs=
p; =
{'RaiseError' =3D> 1});
# Determine MySQL locks table =
name
my $sth =3D =
$dbh->prepare("SELECT * FROM region_props WHERE region =3D =
'$region'");
$sth->execute();
my $ref =3D =
$sth->fetchrow_hashref();
$sth->finish();
my $locks_table =3D =
$ref->{locks_table};
my @form_vars =3D param();
if ( @form_vars > 1 ) { # if =
required parameters were passed
rm_lock();
}
FACE=3D"Arial">print_form(); &nb=
sp; # Display the MySQL table
# Disconnect from the database.
$dbh->disconnect();
print end_html();
sub print_form {
my $i =3D 0;
my @clmnNames =3D =
();
my @rows =3D ();
my $sth =3D =
$dbh->prepare("SELECT * FROM $locks_table");
=
$sth->execute();
while (my $ref =3D =
$sth->fetchrow_hashref()) {
=
push(@rows, =
td({-class=3D>'centre'},checkbox(-name=3D>"ckbx_$i", =
-value=3D>"$ref->{id}", -label=3D>'')).
FACE=3D"Arial"> &nbs=
p; =
td({-class=3D>'centre'},$ref->{id}).
FACE=3D"Arial"> &nbs=
p; =
td({-class=3D>'centre'},$ref->{rcd_opener}).
FACE=3D"Arial"> &nbs=
p; =
td({-class=3D>'centre'},$ref->{lock_date})
=
);
=
$i++;
}
$sth->finish();
# Specified values for =
table column heading names
$clmnNames[0] =3D =
"Select";
$clmnNames[1] =3D =
"Id";
$clmnNames[2] =3D =
"User Id";
$clmnNames[3] =3D =
"Date";
print start_form(),
=
font({-face=3D>"Trebuchet MS, Arial", =
-size=3D>2},br(),
=
center(strong("Delete Record Locks"),br(),br(),
=
table({-class=3D>'bdr', -width=3D>'100%', =
-BgColor=3D>"white", -border=3D>'0'},
=
Tr([th(\@clmnNames)]),"\n",
=
Tr([@rows])),"\n",
=
button(-class=3D>"btn", -value=3D>"Delete selected =
record locks", =
-onClick=3D>"validate(this.form)"),
=
));
print =
hidden(-name=3D>'region', =
-value=3D>param('region')),"\n";
print end_form();
}
sub rm_lock() {
foreach ( param() ) =
{
if ($_ =
=3D~ /^ckbx_\d+$/) {
FACE=3D"Arial"> my =
$id2del =3D param($_);
FACE=3D"Arial"> # Delete =
row from $locks_table
FACE=3D"Arial"> =
$dbh->do("DELETE FROM $locks_table WHERE =
id=3D'$id2del'");
FACE=3D"Arial"> my =
$errno =3D $dbh->{mysql_errno};
FACE=3D"Arial"> my =
$errTxt =3D $dbh->{mysql_error};
FACE=3D"Arial"> if ( =
$errno > 0 ) {
FACE=3D"Arial"> &nbs=
p; print center(font({-face=3D>"Trebuchet MS, Arial", =
-size=3D>2, -color=3D>"red"}),b("Error deleting row =
from $locks_table, =
",font({-color=3D>"black"},"MySQL Error Code: =
$errno - $errTxt"))),"\n",p();
FACE=3D"Arial"> }
=
}
}
}
------_=_NextPart_001_01C8CCB7.63C07F87--