package MyWebApp; use base 'CGI::Application'; use strict; use CGI; use CGI::Ajax; use CGI::Carp qw(fatalsToBrowser); use CGI::Application::Plugin::DBH qw(dbh_config dbh); use DBI qw(:sql_types); use DBIx::Chart; sub setup{ my $self = shift; $self->start_mode('mode1'); $self->mode_param('rm'); $self->run_modes( 'mode1' => 'runMode1', 'mode2' => 'runMode2' ); $self->dbh_config('dbh1', ['DBI:DB2:DS1', 'usr', 'pwd', {RaiseError => 0, PrintError => 0}]); } sub runMode1 { my $q = new CGI; my $pjx = new CGI::Ajax('GET_CHART' => \&BUILD_CHART, 'skip_header' => 1); open(SRC, "pageSrc.htm") or die $!; my $src = ; close(SRC); my $output = $q->start_html; $output .= $src; $output .= $q->end_html; return $pjx->build_html($q, $output); } sub BUILD_CHART { my ($chartSelection, $screenWidth) = @_; my $chartWidth = $screenWidth / 2; #this is where I want to take advantage of CGI::Application::Plugin::DBH -- so that I don't have to build this connection every time an ajax call is made to this subroutine my $dbh = DBIx::Chart->connect('DBI:DB2:DS1', 'usr', 'pwd', {RaiseError => 0, PrintError => 0}) or die $DBI::errstr; my $qryStatement = "SELECT SOMETHING FROM SOMEWHERE"; my $chartStatement = "RETURNING BARCHART(*), IMAGEMAP WHERE ..."; my $sth = $dbh->prepare("$qryStatement $chartStatement"); unless(eval{$sth->execute();}){ die "Failed to execute!"; } my $tempChartRef = $sth->fetchrow_arrayref or die "Failed to fetch!"; $sth->finish; my $imageData = $$tempChartRef[0]; my $imageMap = $$tempChartRef[1]; my $imageName = &WRITE_IMAGE_TO_DISK($imageData, 'PNG', 'barchart'); my $html = ""; $html .= $imageMap; return $html; }