X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FStorage%2FDBI%2FReplicated.pm;h=a21c413cf5547fbf4ab61c93c754bb2c9c940c5f;hb=6834cc1d0449dfc1f528a1b9ecaf4f1a98ae0794;hp=c95a0848b199204354157dec648dc030f235bd44;hpb=c4d3fae2d68b67050e6f4a04031608506e9a18e8;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Storage/DBI/Replicated.pm b/lib/DBIx/Class/Storage/DBI/Replicated.pm index c95a084..a21c413 100644 --- a/lib/DBIx/Class/Storage/DBI/Replicated.pm +++ b/lib/DBIx/Class/Storage/DBI/Replicated.pm @@ -224,11 +224,13 @@ has 'write_handler' => ( update delete dbh + txn_do txn_commit txn_rollback sth deploy schema + reload_row /], ); @@ -385,9 +387,10 @@ Example: my $name = shift @_; $schema->resultset('User')->create({name=>$name}); my $user_rs = $schema->resultset('User')->find({name=>$name}); + return $user_rs; }; - $schema->storage->execute_reliably($reliably, 'John'); + my $user_rs = $schema->storage->execute_reliably($reliably, 'John'); Use this when you must be certain of your database state, such as when you just inserted something and need to get a resultset including it, etc. @@ -411,16 +414,31 @@ sub execute_reliably { $self->read_handler($master); ## do whatever the caller needs + my @result; + my $want_array = wantarray; + eval { - $coderef->(@args); + if($want_array) { + @result = $coderef->(@args); + } + elsif(defined $want_array) { + ($result[0]) = ($coderef->(@args)); + } else { + $coderef->(@args); + } }; + ##Reset to the original state + $self->read_handler($current); + + ##Exception testing has to come last, otherwise you might leave the + ##read_handler set to master. + if($@) { $self->throw_exception("coderef returned an error: $@"); + } else { + return $want_array ? @result : $result[0]; } - - ##Reset to the original state - $self->schema->storage->read_handler($current); } =head2 set_reliable_storage @@ -453,7 +471,7 @@ sub set_balanced_storage { $schema->storage->read_handler($write_handler); } -=head2 txn_do ($coderef) +=head2 around: txn_do ($coderef) Overload to the txn_do method, which is delegated to whatever the L is set to. We overload this in order to wrap in inside a @@ -461,10 +479,24 @@ L method. =cut -sub txn_do { - my($self, $coderef, @args) = @_; - $self->execute_reliably($coderef, @args); -} +around 'txn_do' => sub { + my($txn_do, $self, $coderef, @args) = @_; + $self->execute_reliably(sub {$self->$txn_do($coderef, @args)}); +}; + +=head2 reload_row ($row) + +Overload to the reload_row method so that the reloading is always directed to +the master storage. + +=cut + +around 'reload_row' => sub { + my ($reload_row, $self, $row) = @_; + return $self->execute_reliably(sub { + return $self->$reload_row(shift); + }, $row); +}; =head2 connected