ysth ysth - 9 months ago 20
Perl Question

Apache::DBI->connect_on_init database connection unused

I have mod perl 2 code that is doing

Apache::DBI->connect_on_init($dsn, $user, $passwd);
in a script loaded by PerlRequire.

But when processing a request, doing
DBI->connect_cached($dsn, $user, $passwd)
is creating an additional connection, not using the one created at child init. (DBI is used instead of Apache::DBI because that code is also used in non-apache contexts.)

How do I make it use the already cached connection?

Answer Source

Setting $Apache::DBI::DEBUG=2 shows that the connection created by DBI has an extra attribute set; adding that attribute to the connect_on_init call makes the cached connection be reused:

Apache::DBI->connect_on_init($dsn, $user, $passwd, {
    dbi_connect_method => 'Apache::DBI::connect'
});