I'm considering...

package KQI::Utils::SQLiteWrapper;
use strict;
use warnings;
use Data::Dumper;
use DBI;
use Carp qw(confess);

sub new {
    my ( $pkg, $path, $attr, $count) = @_;
    $pkg = ref($pkg) if ref($pkg);

    my %attr = (
        'RaiseError' => 1,
        'AutoCommit' => 1,
        %{ $attr or { } }
    );

    return bless( {
        'path'  => $path,
        'dbh'   => DBI->connect( "dbi:SQLite:dbname=$path", "", "", \%attr),
        'count' => ( $count or 100)
    }, $pkg );
}

sub do {
    my ($self, @args) = @_;

    foreach (0..$self->{'count'}) {
        my $result = eval { $self->{'dbh'}->do(@args) };
        if ( $@ ) {
            if( $self->{'dbh'}->err == 5 ){  # If got a locked code, try again
                sleep 1;
                next;
            }
            confess $@;
        }
        return $result;
    }
    confess $self->{'dbh'}->errstr;
}
...
1;

But please.

Since I will need to use C++ later, tell me:
Isn't 'busy_handler' supposed to do that?

Thanks
Marcos Rebelo




On Mon, Mar 16, 2009 at 1:54 PM, David Westbrook <[email protected]> wrote:
> At the app level you can do something like this to check for the
> "database is locked(5)" error. Note the sleep and max ~1000 attempts
> functionality as well.
>
> my $ct = 0;
> while( $ct++ < 1000 ){
>  $dbh->do($sql, {}, @bind);
>  if( $dbh->err == 5 ){  # If got a locked code, try again
>    sleep 1;
>    next;
>  }
>  ...
> }
>
>
> On Mon, Mar 16, 2009 at 7:05 AM, marcos rebelo <[email protected]> wrote:
>> Hi all
>>
>> I'm a Perl programmer using SQLite
>>
>> I want to retry to execute every command automatically, until the DB
>> is not locked. In C seems that I need to set the busy_handler.
>>
>> How do I do this with DBD in Perl?
>>
>> Thanks for any help
>>
>> Best Regards
>> Marcos Rebelo
>>
>> --
>> Marcos Rebelo
>> http://oleber.freehostia.com
>> Milan Perl Mongers leader http://milan.pm.org
>> _______________________________________________
>> sqlite-users mailing list
>> [email protected]
>> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
>>
> _______________________________________________
> sqlite-users mailing list
> [email protected]
> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
>



-- 
Marcos Rebelo
http://oleber.freehostia.com
Milan Perl Mongers leader http://milan.pm.org
_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to