How do I use a DB2 connection with DBI and mod_perl?

I'm having trouble getting the IBM DB2 DBI driver to work with mod_perl. My test script:

#!/usr/bin/perl

use strict;
use CGI;
use Data::Dumper;
use DBI;

{
    my $q;
    my $dsn;
    my $username;
    my $password;
    my $sth;
    my $dbc;
    my $row;

    $q = CGI->new;
    print $q->header;
    print $q->start_html();

    $dsn = "DBI:DB2:SAMPLE";
    $username = "username";
    $password = "password";

    print "<pre>".$q->escapeHTML(Dumper(\%ENV))."</pre>";

    $dbc = DBI->connect($dsn, $username, $password);

    $sth = $dbc->prepare("SELECT * FROM SOME_TABLE WHERE FIELD='SOMETHING'");
    $sth->execute();
    $row = $sth->fetchrow_hashref();
    print "<pre>".$q->escapeHTML(Dumper($row))."</pre>";

    print $q->end_html;
}

      

This script works like CGI but not under mod_perl. I am getting this error in apache error log:

DBD::DB2::dr connect warning: [unixODBC][Driver Manager]Data source name not found, and no default driver specified at /usr/lib/perl5/site_perl/5.8.8/Apache/DBI.pm line 190.
DBI connect('SAMPLE','username',...) failed: [unixODBC][Driver Manager]Data source name not found, and no default driver specified at /data/www/perl/test.pl line 15

      

First of all, why is it using ODBC? The native DB2 driver is installed (hence it works like a CGI).

Running Apache 2.2.3, mod_perl 2.0.4 under RHEL5.

This guy had the same problem as mine: http://www.mail-archive.com/ dbi-users@perl.org /msg22909.html But I have no idea how he fixed it. What is mod_php4 related to mod_perl?

Any help would be greatly appreciated, I had no luck with google.

Update

As james2vegas pointed out, the problem has something to do with PHP: I disabled PHP together, I get another error:

 Total Environment allocation failure! Did you set up your DB2 client environment?

      

I believe this error is due to incorrect setting of environment variables, viz DB2INSTANCE

. However, I am unable to disable PHP to solve this problem (I need this for some legacy applications). So, I now have 2 questions:

  • How can I fix the original problem without disabling PHP together?
  • How can I fix the environment problem?

I have set the variables DB2INSTANCE, DB2_PATH and SQLLIB correctly using SetEnv

and PerlSetEnv

in httpd.conf

, but no luck.

Note. I edited the code to determine if the issue was globally persistent.

+2


a source to share


2 answers


This looks resolved, for reference: disable PHP ODBC support in /etc/php.d/pdo_odbc.ini, set environment variables and preload DBD :: DB2 in run mode mod_perl script, although one could have used PERL_DL_NONLAZY value 1 to force load the correct library.



+1


a source


Plain old DBI won't work in mod_perl; it goes all wibbly when your process forks and you try to use your handle again. You need to use Apache :: DBI (this is a replacement for DBI) or even better use a postmodern DBI wrapper like DBIx :: Connector .



You can read more details here in Apache's Guide to Using mod_perl with Relational Databases .

+1


a source







All Articles