Front page | perl.dbd.pg |
Postings from August 2011
Re: Transaction wrapper
Thread Previous
|
Thread Next
From:
Darren Duncan
Date:
August 24, 2011 00:38
Subject:
Re: Transaction wrapper
Message ID:
4E54AA58.7080706@darrenduncan.net
Florian Weimer wrote:
> Is there are ready-to-use transaction wrapper for DBD:Pg? I'm looking
> for somehting which can be used like this:
Oh, absolutely there is.
See http://search.cpan.org/dist/DBIx-Connector/ which David Wheeler put out a
few years ago. See the txn() method.
Besides doing exactly what you want (it also does the begin_work/commit for
you), DBIx::Connector also makes it easier to write things that behave correctly
in different contexts such as persisting DBI connections in Apache versus
non-persisting ones in scripts. And it is DBMS-portable.
The module takes care of just a small number of things that are tricky to get
right and that most people don't want to have to do themselves, and it otherwise
stays out of your way.
-- Darren Duncan
>
> my $connect_args = [$data_source, $username, $auth, \%attr];
> transact $connect_args, sub {
> my ($dbh) = @_;
> $dbh->begin_work;
> ...
> $dbh->commit;
> };
>
> The transact subroutine should abort the transaction if the passed sub
> dies. For certain exceptions, the sub should be called again to retry
> the transaction. The database handle should be cached.
>
> This is somewhat PostgreSQL-specific because there are three cases:
> permanent errors (such as SQL syntax errors, or non-SQL exceptions),
> transient errors which will go away when the transaction is retried
> within the same connection (such as serialization failures and certain
> types of UNIQUE constraint violations), and errors which will likely go
> away when the transaction is retried with a fresh connection (due to
> server restart, for example).
>
> This is not very difficult to write as such, but getting the error code
> list right is a bit delicate.
>
Thread Previous
|
Thread Next