You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Octavian Rasnita <or...@fcc.ro> on 2004/12/28 20:21:30 UTC

Using DBI

Hi all,

I am a new member on this list and after reading the posts from this list in
the last few days, I don't even know if this is the apropriate place for
asking questions about using mod_perl.
If it is not, please tell me if there is another list which could be more
useful for a mod_perl beginner.

I want to use the DBI module with mod_perl 2 with Perl 5.8.4, and for the
moment... under Windows 2000.

I have tried:

In httpd.conf:
PerlRequire [path]/preload.pl

in preload.pl:
use Apache::DBI();
use DBI ();
DBI->install_driver("mysql");
###Apache::DBI->connect_on_init("DBI:mysql:database=presa;host=localhost",
"ODBC", undef, {PrintError =>1, RaiseError=>0, AutoCommit => 1});

in the GetCategories.pm module:
##use Apache::DBI;
use DBI;
my $dbh = ...;

I have tried more combinations, but every time I get the following error in
the logs:

[Tue Dec 28 21:07:34 2004] [error] DBD::mysql::db prepare failed: handle 2
is owned by thread 265c564 not current thread 14ce78c (handles can't be
shared between threads and your driver may need a CLONE method added) at
f:/web/presa/modules/GetCategories.pm line 18.\n
-----

So I have commented out the following line in preload.pl:
###Apache::DBI->connect_on_init("DBI:mysql:database=presa;host=localhost",
"ODBC", undef, {PrintError =>1, RaiseError=>0, AutoCommit => 1});

And in GetCategories.pm I have used only:

use DBI;
my $dbh = ...;

Well, in this case the script works, but I don't know if this way of using
DBI is the best way.

Thank you for any suggestion.

Teddy


-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: Using DBI

Posted by Stas Bekman <st...@stason.org>.
Octavian Rasnita wrote:
> Hi all,
> 
> I am a new member on this list and after reading the posts from this list in
> the last few days, I don't even know if this is the apropriate place for
> asking questions about using mod_perl.
> If it is not, please tell me if there is another list which could be more
> useful for a mod_perl beginner.
> 
> I want to use the DBI module with mod_perl 2 with Perl 5.8.4, and for the
> moment... under Windows 2000.
> 
> I have tried:
> 
> In httpd.conf:
> PerlRequire [path]/preload.pl
> 
> in preload.pl:
> use Apache::DBI();
> use DBI ();
> DBI->install_driver("mysql");
> ###Apache::DBI->connect_on_init("DBI:mysql:database=presa;host=localhost",
> "ODBC", undef, {PrintError =>1, RaiseError=>0, AutoCommit => 1});
> 
> in the GetCategories.pm module:
> ##use Apache::DBI;
> use DBI;
> my $dbh = ...;
> 
> I have tried more combinations, but every time I get the following error in
> the logs:
> 
> [Tue Dec 28 21:07:34 2004] [error] DBD::mysql::db prepare failed: handle 2
> is owned by thread 265c564 not current thread 14ce78c (handles can't be
> shared between threads and your driver may need a CLONE method added) at
> f:/web/presa/modules/GetCategories.pm line 18.\n
> -----
> 
> So I have commented out the following line in preload.pl:
> ###Apache::DBI->connect_on_init("DBI:mysql:database=presa;host=localhost",
> "ODBC", undef, {PrintError =>1, RaiseError=>0, AutoCommit => 1});
> 
> And in GetCategories.pm I have used only:
> 
> use DBI;
> my $dbh = ...;
> 
> Well, in this case the script works, but I don't know if this way of using
> DBI is the best way.

I'm afraid your script, just appears to be working and if you put it under 
a  stress testing, that will really involve multithreading you will see 
the same problem. Take a look at:
http://search.cpan.org/~rudy/DBD-mysql-2.9004/lib/DBD/mysql.pm#MULTITHREADING

-----------------------------
The multithreading capabilities of DBD::mysql depend completely on the 
underlying C libraries: The modules are working with handle data only, no 
global variables are accessed or (to the best of my knowledge) thread 
unsafe functions are called. Thus DBD::mysql is believed to be completely 
thread safe, if the C libraries are thread safe and you don't share 
handles among threads.

The obvious question is: Are the C libraries thread safe? In the case of 
MySQL the answer is "mostly" and, in theory, you should be able to get a 
"yes", if the C library is compiled for being thread safe (By default it 
isn't.) by passing the option -with-thread-safe-client to configure. See 
the section on How to make a threadsafe client in the manual.
-----------------------------

-- 
__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com

-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: Using DBI

Posted by Stas Bekman <st...@stason.org>.
Malcolm J Harwood wrote:
[...]
> This works if you are using the pre-fork MPM (which I understand doesn't work 
> well under Win32 due to limitations of NT). 
> 
> It would seem that if you are using the worker MPM then it doesn't make a 
> connection for each thread, just for the owning process. In which case you 
> get the 'shared handle' error you saw.

Win32 doesn't use neither worker nor prefork, but winnt MPM (which is 
somewhat similar to worker mpm, i.e. using threads, but uses win32 
specific guts). I guess Randy can explain more. I've never used it myself.

-- 
__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com

-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: Using DBI

Posted by Malcolm J Harwood <mj...@liminalflux.net>.
On Tuesday 28 December 2004 04:56 pm, Octavian Rasnita wrote:

> Ok, but I have seen an example in a tutorial where the database is accessed
> with the username, and the password from a startup.pl file exactly like
> this:
>
> Apache::DBI->connect_on_init("DBI:mysql:database=presa;host=localhost",
> "ODBC", undef, {PrintError =>1, RaiseError=>0, AutoCommit => 1});
>
> In what situation can I access the database at the server startup?

The above does not actually access the database.  What it does is tell 
Apache::DBI to make a connection during the initialisation of each child 
process, so that when you later ask for a connection it has one.

This works if you are using the pre-fork MPM (which I understand doesn't work 
well under Win32 due to limitations of NT). 

It would seem that if you are using the worker MPM then it doesn't make a 
connection for each thread, just for the owning process. In which case you 
get the 'shared handle' error you saw.

> I would like to preload the DBI module at the  server startup and to be

Just put "use DBI;" and "use DBD::mysql;" in your startup, that will compile 
and share the source between processes. (Though if you are using single 
process/multi-thread apache on NT, I'm not sure if that gains you much).

> able to create persistent connections in order to make them faster.

That's what Apache::DBI does. It keeps a pool of persistant connections and 
simply hands you one whenever you ask for a new DB handle.

-- 
"How wonderful it is that nobody need wait a single moment to
 improve the world."
- Anne Frank, Diary of a Young Girl

-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: Using DBI

Posted by Randy Kobes <ra...@theoryx5.uwinnipeg.ca>.
On Tue, 28 Dec 2004, Randy Kobes wrote:

[ ... ]
> Can you try the
> following patch against Apache::DBI (version 0.94) - if
> you're using ppm on Win32, you can get this from the
> Apache-DBI.ppd package in the
>    http://theoryx5.uwinnipeg.ca/ppms/
> repository.

Sorry about this - I missed one thing. Can you try the
following instead:
=============================================================
--- DBI.pm~	Tue Feb 17 18:18:50 2004
+++ DBI.pm	Tue Dec 28 17:17:18 2004
@@ -13,7 +13,7 @@

 # 1: report about new connect
 # 2: full debug output
-$Apache::DBI::DEBUG = 0;
+$Apache::DBI::DEBUG = 2;
 #DBI->trace(2);

 my %Connected;    # cache for database handles
@@ -23,6 +23,8 @@
 my %LastPingTime; # keeps track of last ping per data_source
 my $Idx;          # key of %Connected and %Rollback.

+use Apache::ServerUtil ();
+my $s = Apache->server;

 # supposed to be called in a startup script.
 # stores the data_source of all connections, which are supposed to be created upon
@@ -32,9 +34,8 @@
     # provide a handler which creates all connections during server startup

     # TODO - Should check for mod_perl 2 and do the right thing there
-    carp "Apache.pm was not loaded\n" and return unless $INC{'Apache.pm'};
-    if(!@ChildConnect and Apache->can('push_handlers')) {
-        Apache->push_handlers(PerlChildInitHandler => \&childinit);
+    if(!@ChildConnect) {
+        $s->push_handlers(PerlChildInitHandler => \&childinit);
     }
     # store connections
     push @ChildConnect, [@_];
@@ -94,9 +95,9 @@
     # this PerlCleanupHandler is supposed to initiate a rollback after the script has finished if AutoCommit is off.
     my $needCleanup = ($Idx =~ /AutoCommit[^\d]+0/) ? 1 : 0;
     # TODO - Fix mod_perl 2.0 here
-    if(!$Rollback{$Idx} and $needCleanup and Apache->can('push_handlers')) {
+    if(!$Rollback{$Idx} and $needCleanup) {
         print STDERR "$prefix push PerlCleanupHandler \n" if $Apache::DBI::DEBUG > 1;
-        Apache->push_handlers("PerlCleanupHandler", \&cleanup);
+        $s->push_handlers("PerlCleanupHandler", \&cleanup);
         # make sure, that the rollback is called only once for every
         # request, even if the script calls connect more than once
         $Rollback{$Idx} = 1;

==============================================================================

-- 
best regards,
randy

-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: Using DBI

Posted by Randy Kobes <ra...@theoryx5.uwinnipeg.ca>.
On Wed, 29 Dec 2004, Octavian Rasnita wrote:

> I have tried to install Apache-DBI from TheoryX but ppm gave an error:
>
> Error: no suitable installation target found for package Apache-DBI.

That usually means that ppm found a package, but it was in
a repository that didn't match your platform. Does
  C:\> ppm install http://theoryx5.uwinnipeg.ca/ppms/Apache-DBI.ppd
work any better?

> How can I apply that patch manually? Do I need to edit the module
> Apache::DBI by hand?

As it's just something to try, I'll send the whole file to
you (privately).

-- 
best regards,
randy


-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: Using DBI

Posted by Octavian Rasnita <or...@fcc.ro>.
I have tried to install Apache-DBI from TheoryX but ppm gave an error:

Error: no suitable installation target found for package Apache-DBI.

How can I apply that patch manually? Do I need to edit the module
Apache::DBI by hand?
(Under Windows)

Thanks.

Teddy

From: "Randy Kobes" <ra...@theoryx5.uwinnipeg.ca>



It looks from some comments in the latest Apache::DBI
(version 0.94) that the connect_on_init method hasn't been
completely ported to mod_perl 2 yet. Can you try the
following patch against Apache::DBI (version 0.94) - if
you're using ppm on Win32, you can get this from the
Apache-DBI.ppd package in the
   http://theoryx5.uwinnipeg.ca/ppms/
repository.

==============================================================
--- DBI.pm~ Tue Feb 17 18:18:50 2004
+++ DBI.pm Tue Dec 28 16:43:46 2004
@@ -13,7 +13,7 @@

 # 1: report about new connect
 # 2: full debug output
-$Apache::DBI::DEBUG = 0;
+$Apache::DBI::DEBUG = 2;
 #DBI->trace(2);

 my %Connected;    # cache for database handles
@@ -23,6 +23,8 @@
 my %LastPingTime; # keeps track of last ping per data_source
 my $Idx;          # key of %Connected and %Rollback.

+use Apache::ServerUtil ();
+my $s = Apache->server;

 # supposed to be called in a startup script.
 # stores the data_source of all connections, which are supposed to be
created upon
@@ -32,9 +34,8 @@
     # provide a handler which creates all connections during server startup

     # TODO - Should check for mod_perl 2 and do the right thing there
-    carp "Apache.pm was not loaded\n" and return unless $INC{'Apache.pm'};
-    if(!@ChildConnect and Apache->can('push_handlers')) {
-        Apache->push_handlers(PerlChildInitHandler => \&childinit);
+    if(!@ChildConnect) {
+        $s->push_handlers(PerlChildInitHandler => \&childinit);
     }
     # store connections
     push @ChildConnect, [@_];
@@ -96,7 +97,7 @@
     # TODO - Fix mod_perl 2.0 here
     if(!$Rollback{$Idx} and $needCleanup and Apache->can('push_handlers'))
{
         print STDERR "$prefix push PerlCleanupHandler \n" if
$Apache::DBI::DEBUG > 1;
-        Apache->push_handlers("PerlCleanupHandler", \&cleanup);
+        $s->push_handlers("PerlCleanupHandler", \&cleanup);
         # make sure, that the rollback is called only once for every
         # request, even if the script calls connect more than once
         $Rollback{$Idx} = 1;

============================================================================
========

Does this help?

-- 
best regards,
randy kobes


-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: Using DBI

Posted by Randy Kobes <ra...@theoryx5.uwinnipeg.ca>.
On Tue, 28 Dec 2004, Octavian Rasnita wrote:

> Ok, but I have seen an example in a tutorial where the database is accessed
> with the username, and the password from a startup.pl file exactly like
> this:
>
> Apache::DBI->connect_on_init("DBI:mysql:database=presa;host=localhost",
> "ODBC", undef, {PrintError =>1, RaiseError=>0, AutoCommit => 1});
>
> In what situation can I access the database at the server startup?
>
> I would like to preload the DBI module at the  server startup and to be able
> to create persistent connections in order to make them faster.

It looks from some comments in the latest Apache::DBI
(version 0.94) that the connect_on_init method hasn't been
completely ported to mod_perl 2 yet. Can you try the
following patch against Apache::DBI (version 0.94) - if
you're using ppm on Win32, you can get this from the
Apache-DBI.ppd package in the
   http://theoryx5.uwinnipeg.ca/ppms/
repository.

==============================================================
--- DBI.pm~	Tue Feb 17 18:18:50 2004
+++ DBI.pm	Tue Dec 28 16:43:46 2004
@@ -13,7 +13,7 @@

 # 1: report about new connect
 # 2: full debug output
-$Apache::DBI::DEBUG = 0;
+$Apache::DBI::DEBUG = 2;
 #DBI->trace(2);

 my %Connected;    # cache for database handles
@@ -23,6 +23,8 @@
 my %LastPingTime; # keeps track of last ping per data_source
 my $Idx;          # key of %Connected and %Rollback.

+use Apache::ServerUtil ();
+my $s = Apache->server;

 # supposed to be called in a startup script.
 # stores the data_source of all connections, which are supposed to be created upon
@@ -32,9 +34,8 @@
     # provide a handler which creates all connections during server startup

     # TODO - Should check for mod_perl 2 and do the right thing there
-    carp "Apache.pm was not loaded\n" and return unless $INC{'Apache.pm'};
-    if(!@ChildConnect and Apache->can('push_handlers')) {
-        Apache->push_handlers(PerlChildInitHandler => \&childinit);
+    if(!@ChildConnect) {
+        $s->push_handlers(PerlChildInitHandler => \&childinit);
     }
     # store connections
     push @ChildConnect, [@_];
@@ -96,7 +97,7 @@
     # TODO - Fix mod_perl 2.0 here
     if(!$Rollback{$Idx} and $needCleanup and Apache->can('push_handlers')) {
         print STDERR "$prefix push PerlCleanupHandler \n" if $Apache::DBI::DEBUG > 1;
-        Apache->push_handlers("PerlCleanupHandler", \&cleanup);
+        $s->push_handlers("PerlCleanupHandler", \&cleanup);
         # make sure, that the rollback is called only once for every
         # request, even if the script calls connect more than once
         $Rollback{$Idx} = 1;

====================================================================================

Does this help?

-- 
best regards,
randy kobes

-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: Using DBI

Posted by Octavian Rasnita <or...@fcc.ro>.
From: "Malcolm J Harwood" <mj...@liminalflux.net>

> [Tue Dec 28 21:07:34 2004] [error] DBD::mysql::db prepare failed: handle 2
> is owned by thread 265c564 not current thread 14ce78c (handles can't be
> shared between threads and your driver may need a CLONE method added) at
> f:/web/presa/modules/GetCategories.pm line 18.\n

This means you've got a db handle before the thread was cloned (created by
the
connect_on_init - which connects when the process, not the thread, is
created). The handle can't be shared between the parent and child thread,
generating the above error.

> So I have commented out the following line in preload.pl:
> ###Apache::DBI->connect_on_init("DBI:mysql:database=presa;host=localhost",
> "ODBC", undef, {PrintError =>1, RaiseError=>0, AutoCommit => 1});
>
> And in GetCategories.pm I have used only:
>
> use DBI;
> my $dbh = ...;
>
> Well, in this case the script works, but I don't know if this way of using
> DBI is the best way.

If you are using multiple threads, then yes. (Though I'm not sure how
Apache::DBI's thread pool works with multiple threads, presumably it
provides
seperate pools for each thread, but I could be wrong there).

--

Ok, but I have seen an example in a tutorial where the database is accessed
with the username, and the password from a startup.pl file exactly like
this:

Apache::DBI->connect_on_init("DBI:mysql:database=presa;host=localhost",
"ODBC", undef, {PrintError =>1, RaiseError=>0, AutoCommit => 1});

In what situation can I access the database at the server startup?

I would like to preload the DBI module at the  server startup and to be able
to create persistent connections in order to make them faster.

Thank you.

Teddy


-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: Using DBI

Posted by Malcolm J Harwood <mj...@liminalflux.net>.
On Tuesday 28 December 2004 02:21 pm, Octavian Rasnita wrote:

> I am a new member on this list and after reading the posts from this list
> in the last few days, I don't even know if this is the apropriate place for
> asking questions about using mod_perl.

It is. :)

> in preload.pl:
> use Apache::DBI();
> use DBI ();
> DBI->install_driver("mysql");
> ###Apache::DBI->connect_on_init("DBI:mysql:database=presa;host=localhost",
> "ODBC", undef, {PrintError =>1, RaiseError=>0, AutoCommit => 1});

I assume from the later comment that the above was not supposed to be 
commented out in the first example.

> [Tue Dec 28 21:07:34 2004] [error] DBD::mysql::db prepare failed: handle 2
> is owned by thread 265c564 not current thread 14ce78c (handles can't be
> shared between threads and your driver may need a CLONE method added) at
> f:/web/presa/modules/GetCategories.pm line 18.\n

This means you've got a db handle before the thread was cloned (created by the 
connect_on_init - which connects when the process, not the thread, is 
created). The handle can't be shared between the parent and child thread, 
generating the above error. 

> So I have commented out the following line in preload.pl:
> ###Apache::DBI->connect_on_init("DBI:mysql:database=presa;host=localhost",
> "ODBC", undef, {PrintError =>1, RaiseError=>0, AutoCommit => 1});
>
> And in GetCategories.pm I have used only:
>
> use DBI;
> my $dbh = ...;
>
> Well, in this case the script works, but I don't know if this way of using
> DBI is the best way.

If you are using multiple threads, then yes. (Though I'm not sure how 
Apache::DBI's thread pool works with multiple threads, presumably it provides 
seperate pools for each thread, but I could be wrong there).



-- 
"We are grey. We stand between the candle and the stars"
- Babylon 5

-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html