[RADIATOR] Persistent database connection

Johnson, Neil M neil-johnson at uiowa.edu
Wed Aug 19 12:02:24 CDT 2009


Question from a PERL/Radiator newbie.

I would like to make a database query  inside of a ReplyHook. I can successfully open the database connection in the StartupHook, but how to I pass the connection to the ReplyHook to peform the query ? Or do I have to open the database connection every time I call the ReplyHook.

Code snippets below:

Thanks.


Code in startuphook.pl

# -*- mode: Perl -*-
use DBI;

sub
{
#    my $rp = ${$_[1]};
#    my $p = ${$_[2]};

	# Constants
	my $DHCP_DB_DSN 	= "DBI:ODBC:dhcp";
	my $DHCP_DB_USER 	= "poolupdater";
	my $DHCP_DB_PASSWORD = "secretsauce";
	
	# Variables
	my $dhcp_dbh;
	my $dhcp_query_sth;

    &main::log($main::LOG_DEBUG, "StartupHook called.");
	
	$dhcp_dbh = DBI->connect($DHCP_DB_DSN, $DHCP_DB_USER, $DHCP_DB_PASSWORD)
		|| die "$DBI::errstr";
		
	&main::log($main::LOG_DEBUG, "StartupHook - DatbaseConnected");
	
	$dhcp_query_sth = $dhcp_dbh->prepare(
	  qq{ SELECT * from pools; }
	);
	
return;
}

Code in ReplyHook.pl
sub
{
    my $rp = ${$_[1]};
    my $p = ${$_[2]};

    my $ASCIIvlan;
    my $binaryvlan = 830;
	
    &main::log($main::LOG_DEBUG, "ReplyHook VLAN rewriter called.");
	$ASCIIvlan = $rp->get_attr('Tunnel-Private-Group-ID');
	# check for attribute tag
	if ($ASCIIvlan =~ /^(d+):(.*)/)
	{
			# tagged attribute
			&main::log($main::LOG_DEBUG, "Found tagged ASCII VLAN attribute of $ASCIIvlan");
	}
	else
	{
			# untagged attribute
			&main::log($main::LOG_DEBUG, "Found untagged ASCII VLAN attribute of $ASCIIvlan");
	}

	# Replace attribute
	
               # Query DHCP database to determine pool with most available leases

	$dhcp_query_sth = $execute();
	
	&main::log($main::LOG_DEBUG, "Replacing ASCII vlan tag with $binaryvlan");
	$rp->change_attr('Tunnel-Private-Group-ID', $binaryvlan);
return;
}

Thanks.

-- 
Neil Johnson
Network Engineer
Information Technology Services
The University of Iowa
Work: 319 384-0938
Mobile: 319 540-2081
Fax: 319 355-2618
E-mail: neil-johnson at uiowa.edu




More information about the radiator mailing list