title: How do I use connect to DB2 with DBI and mod_perl?
body: <p>I'm having issues with getting DBI's IBM DB2 driver to work with mod_perl. My test script is:</p>
  
  <pre><code>#!/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-&gt;new;
      print $q-&gt;header;
      print $q-&gt;start_html();
  
      $dsn = "DBI:DB2:SAMPLE";
      $username = "username";
      $password = "password";
  
      print "&lt;pre&gt;".$q-&gt;escapeHTML(Dumper(\%ENV))."&lt;/pre&gt;";
  
      $dbc = DBI-&gt;connect($dsn, $username, $password);
  
      $sth = $dbc-&gt;prepare("SELECT * FROM SOME_TABLE WHERE FIELD='SOMETHING'");
      $sth-&gt;execute();
      $row = $sth-&gt;fetchrow_hashref();
      print "&lt;pre&gt;".$q-&gt;escapeHTML(Dumper($row))."&lt;/pre&gt;";
  
      print $q-&gt;end_html;
  }
  </code></pre>
  
  <p>This script works as CGI but not under mod_perl. I get this error in apache's error log:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>First of all, why is it using ODBC? The native DB2 driver is installed (hence it works as CGI).</p>
  
  <p>Running Apache 2.2.3, mod_perl 2.0.4 under RHEL5.</p>
  
  <p>This guy had the same problem as me:
  <a href="http://www.mail-archive.com/dbi-users@perl.org/msg22909.html" rel="nofollow noreferrer">http://www.mail-archive.com/dbi-users@perl.org/msg22909.html</a>
  But I have no idea how he fixed it. What does mod_php4 have to do with mod_perl?</p>
  
  <p>Any help would be greatly appreciated, I'm having no luck with google.</p>
  
  <p><strong>Update</strong>:</p>
  
  <p>As james2vegas pointed out, the problem has something to do with PHP: I disable PHP all together I get the a different error:</p>
  
  <pre><code> Total Environment allocation failure! Did you set up your DB2 client environment?
  </code></pre>
  
  <p>I believe this error is to do with environment variables not being set up correctly, namely <code>DB2INSTANCE</code>. However, I'm not able to turn off PHP to resolve this problem (I need it for some legacy applications). So I now have 2 questions:</p>
  
  <ol>
  <li>How can I fix the original issue without disabling PHP all together?</li>
  <li>How can I fix the environment issue?</li>
  </ol>
  
  <p>I've set DB2INSTANCE, DB2_PATH and SQLLIB variables correctly using <code>SetEnv</code> and <code>PerlSetEnv</code> in <code>httpd.conf</code>, but with no luck.</p>
  
  <p>Note: I've edited the code to determine if the problem was to do with Global Variable Persistence.</p>