You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Feng He <sh...@gmail.com> on 2011/08/02 15:30:28 UTC

Apache::DBI

Hi,

I just want to develop a modperl application.
It's a handler, the database is Mysql.
Shall I use Apache::DBI, or DBI is just fine ?

Thank u.
Regards.

Re: Apache::DBI

Posted by Jerry Pereira <on...@gmail.com>.
How about DBIx::Connector?

On Tue, Aug 2, 2011 at 6:30 AM, Feng He <sh...@gmail.com> wrote:

> Hi,
>
> I just want to develop a modperl application.
> It's a handler, the database is Mysql.
> Shall I use Apache::DBI, or DBI is just fine ?
>
> Thank u.
> Regards.
>



-- 
Your clothes may be the latest in style but you aint completely dressed
until you wear a smile!
Keep smiling : )

Re: Apache::DBI

Posted by Torsten Förtsch <to...@gmx.net>.
On Tuesday, 02 August 2011 15:30:28 Feng He wrote:
> I just want to develop a modperl application.
> It's a handler, the database is Mysql.
> Shall I use Apache::DBI, or DBI is just fine ?

It depends. Apache::DBI provides a transparent connection cache. You can 
forget the DB handle acquired at some time at the end of the request. The 
next DBI::connect will then fetch the handle from the Apache::DBI cache.

On the other hand, all modern DBI versions have connect_cached() (only 
ancient version lack this function) which provides much the same 
functionality.

Further, applications that are implemented as modperl handlers often tend 
to use a singleton to store the DBI handle. In this case Apache::DBI is 
not necessary. However, in this scenario you'll have to take care of 
cases when the connection is dropped unexpectedly. Nevertheless, I'd 
prefer this strategy for new development because it gives you maximum 
control over what is going on. But that's just my humble opinion.

Torsten Förtsch

-- 
Need professional modperl support? Hire me! (http://foertsch.name)

Like fantasy? http://kabatinte.net

Re: Apache::DBI

Posted by Perrin Harkins <pe...@elem.com>.
On Wed, Aug 3, 2011 at 2:49 AM, Feng He <sh...@gmail.com> wrote:
> ab -n 100 -c 3 http://bizstatus.game.yy.com/upload/?arguments
>
>
> When the client number is larger than one, the items number in
> database is not correct.
> it's always larger than 100, for example, 101, 102, 103 etc.
>
> So, what's wrong with my program?

I think your program is fine.  I suspect ab is not that precise.

- Perrin

Re: Apache::DBI

Posted by Adam Prime <ad...@utoronto.ca>.
On 8/3/2011 2:49 AM, Feng He wrote:
> Hi,
>
> Thank you all for the info.
>
> I have finished writting the handler this morning, and have enabled
> Apache::DBI in httpd.conf.
> My handler is for this url:
> http://bizstatus.game.yy.com/upload/
>
> It accept client's uploaded data and write the data to database.
> The strange stuff is, when I run more than one process for test, the
> items number in database in not correct.
>
> for example:
>
> ab -n 100 -c 1 http://bizstatus.game.yy.com/upload/?arguments
>
> This insert 100 items into database, the select result is correct.
>
> But this one:
>
> ab -n 100 -c 2 http://bizstatus.game.yy.com/upload/?arguments
>
> or:
>
> ab -n 100 -c 3 http://bizstatus.game.yy.com/upload/?arguments
>
>
> When the client number is larger than one, the items number in
> database is not correct.
> it's always larger than 100, for example, 101, 102, 103 etc.
>
> So, what's wrong with my program?
>
> Thank you!

The first thing i'd suggest is looking at the access and error logs, and 
seeing if there are anything in there that will tell you what's wrong 
with it.

Nothing looks wrong at first glance to me, though you should check for 
errors on the prepare call.

Adam

Re: Apache::DBI

Posted by Feng He <sh...@gmail.com>.
Hi,

Thank you all for the info.

I have finished writting the handler this morning, and have enabled
Apache::DBI in httpd.conf.
My handler is for this url:
http://bizstatus.game.yy.com/upload/

It accept client's uploaded data and write the data to database.
The strange stuff is, when I run more than one process for test, the
items number in database in not correct.

for example:

ab -n 100 -c 1 http://bizstatus.game.yy.com/upload/?arguments

This insert 100 items into database, the select result is correct.

But this one:

ab -n 100 -c 2 http://bizstatus.game.yy.com/upload/?arguments

or:

ab -n 100 -c 3 http://bizstatus.game.yy.com/upload/?arguments


When the client number is larger than one, the items number in
database is not correct.
it's always larger than 100, for example, 101, 102, 103 etc.

So, what's wrong with my program?

The handler looks as:

package ABC;
use strict;
use Apache2::RequestIO ();
use Apache2::RequestRec ();
use Apache2::Connection ();
use Apache2::RequestUtil ();
use Apache2::Request ();
use DBI;


use Apache2::Const -compile => qw(OK NOT_FOUND);

sub handler {

    my $r = shift;
    my $q = Apache2::Request->new($r);
    my $ip = $r->connection->remote_ip;

    my $v1 = $q->param('arg1');
    my $v2 = $q->param('arg2');


    my $db = 'mydb';
    my $host = '127.0.0.1';
    my $port = 3306;
    my $user = '***';
    my $passwd = '***';
    my $dbh = DBI->connect("dbi:mysql:database=$db;host=$host;port=$port",
$user, $passwd)
                           or die $DBI::errstr;

    my $str = "insert into mytable (v1,v2,check_time)
               values(?,?,now())";

    my $sth = $dbh->prepare($str);
    $sth->execute($v1,$v2) or die $dbh->errstr;
    $sth->finish;

    $r->content_type('text/plain');
    $r->print('OK');

    return Apache2::Const::OK;
}

1;



Thank you!

Re: Apache::DBI

Posted by "David E. Wheeler" <da...@kineticode.com>.
On Aug 2, 2011, at 6:30 AM, Feng He wrote:

> I just want to develop a modperl application.
> It's a handler, the database is Mysql.
> Shall I use Apache::DBI, or DBI is just fine ?

I recommend DBIx::Connector.

Best,

David