From: David Kamholz Date: Sat, 10 Dec 2005 16:54:34 +0000 (+0000) Subject: clean up transaction handling X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=1385b8e4e4aaf3f80210441bba5992756e56ae44;p=dbsrgits%2FDBIx-Class-Historic.git clean up transaction handling - tx_begin, tx_commit, tx_rollback (old methods are aliased) - DBI.pm uses counter to do nested transactions portably --- diff --git a/lib/DBIx/Class/DB.pm b/lib/DBIx/Class/DB.pm index c2326ef..08282df 100644 --- a/lib/DBIx/Class/DB.pm +++ b/lib/DBIx/Class/DB.pm @@ -5,6 +5,9 @@ use DBIx::Class::Storage::DBI; use DBIx::Class::ClassResolver::PassThrough; use DBI; +*dbi_commit = \&tx_commit; +*dbi_rollback = \&tx_rollback; + =head1 NAME DBIx::Class::DB - Simple DBIx::Class Database connection by class inheritance @@ -63,21 +66,29 @@ sub connection { $class->storage($storage); } -=head2 dbi_commit +=head2 tx_begin + +Begins a transaction (does nothing if AutoCommit is off). + +=cut + +sub tx_commit { $_[0]->storage->tx_begin } + +=head2 tx_commit -Issues a commit against the current dbh. +Commits the current transaction. =cut -sub dbi_commit { $_[0]->storage->commit; } +sub tx_commit { $_[0]->storage->tx_commit } -=head2 dbi_rollback +=head2 tx_rollback -Issues a rollback against the current dbh. +Rolls back the current transaction. =cut -sub dbi_rollback { $_[0]->storage->rollback; } +sub tx_rollback { $_[0]->storage->tx_rollback } sub resolve_class { return shift->class_resolver->class(@_); } diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index b1e06b6..8b68667 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -86,6 +86,8 @@ __PACKAGE__->load_components(qw/Exception AccessorGroup/); __PACKAGE__->mk_group_accessors('simple' => qw/connect_info _dbh _sql_maker debug cursor/); +our $TRANSACTION = 0; + sub new { my $new = bless({}, ref $_[0] || $_[0]); $new->cursor("DBIx::Class::Storage::DBI::Cursor"); @@ -135,25 +137,35 @@ sub _connect { return DBI->connect(@info); } -=head2 commit - - $class->commit; +=head2 tx_begin -Issues a commit again the current dbh +Calls begin_work on the current dbh. =cut -sub commit { $_[0]->dbh->commit; } +sub tx_begin { + $_[0]->dbh->begin_work if $TRANSACTION++ == 0 and $_[0]->dbh->{AutoCommit}; +} -=head2 rollback +=head2 tx_commit - $class->rollback; +Issues a commit against the current dbh. -Issues a rollback again the current dbh +=cut + +sub tx_commit { + $_[0]->dbh->commit if --$TRANSACTION == 0; +} + +=head2 tx_rollback + +Issues a rollback against the current dbh. =cut -sub rollback { $_[0]->dbh->rollback; } +sub tx_rollback { + --$TRANSACTION == 0 ? $_[0]->dbh->rollback : die $@; +} sub _execute { my ($self, $op, $extra_bind, $ident, @args) = @_;