$schema->storage->connect_replicants(
[$dsn1, $user, $pass, \%opts],
- [$dsn1, $user, $pass, \%opts],
- [$dsn1, $user, $pass, \%opts],
+ [$dsn2, $user, $pass, \%opts],
+ [$dsn3, $user, $pass, \%opts],
);
=head1 DESCRIPTION
isa=>'ClassName',
lazy_build=>1,
handles=>{
- 'create_pool' => 'new',
+ 'create_pool' => 'new',
},
);
isa=>'ClassName',
lazy_build=>1,
handles=>{
- 'create_balancer' => 'new',
+ 'create_balancer' => 'new',
},
);
update
delete
dbh
+ txn_do
txn_commit
txn_rollback
sth
deploy
schema
+ reload_row
/],
);
## Hate to do it this way, but can't seem to get advice on the attribute working right
## maybe we can do a type and coercion for it.
if( $storage_type_args->{balancer_type} && $storage_type_args->{balancer_type}=~m/^::/) {
- $storage_type_args->{balancer_type} = 'DBIx::Class::Storage::DBI::Replicated::Balancer'.$storage_type_args->{balancer_type};
- eval "require $storage_type_args->{balancer_type}";
+ $storage_type_args->{balancer_type} = 'DBIx::Class::Storage::DBI::Replicated::Balancer'.$storage_type_args->{balancer_type};
+ eval "require $storage_type_args->{balancer_type}";
}
return $class->meta->new_object(
=cut
sub _build_master {
- DBIx::Class::Storage::DBI->new;
+ DBIx::Class::Storage::DBI->new;
}
=head2 _build_pool_type
=cut
sub _build_pool {
- my $self = shift @_;
+ my $self = shift @_;
$self->create_pool(%{$self->pool_args});
}
$self->create_balancer(
pool=>$self->pool,
master=>$self->master,
- %{$self->balancer_args},);
+ %{$self->balancer_args},
+ );
}
=head2 _build_write_handler
=cut
around 'connect_replicants' => sub {
- my ($method, $self, @args) = @_;
- $self->$method($self->schema, @args);
+ my ($method, $self, @args) = @_;
+ $self->$method($self->schema, @args);
};
=head2 all_storages
=cut
sub all_storages {
- my $self = shift @_;
-
- return grep {defined $_ && blessed $_} (
- $self->master,
- $self->replicants,
- );
+ my $self = shift @_;
+
+ return grep {defined $_ && blessed $_} (
+ $self->master,
+ $self->replicants,
+ );
}
=head2 execute_reliably ($coderef, ?@args)
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.
sub execute_reliably {
my ($self, $coderef, @args) = @_;
-
+
unless( ref $coderef eq 'CODE') {
$self->throw_exception('Second argument must be a coderef');
}
$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
=cut
sub set_reliable_storage {
- my $self = shift @_;
- my $schema = $self->schema;
- my $write_handler = $self->schema->storage->write_handler;
-
- $schema->storage->read_handler($write_handler);
+ my $self = shift @_;
+ my $schema = $self->schema;
+ my $write_handler = $self->schema->storage->write_handler;
+
+ $schema->storage->read_handler($write_handler);
}
=head2 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<write_handler> is set to. We overload this in order to wrap in inside a
=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
=cut
sub connected {
- my $self = shift @_;
-
- return
- $self->master->connected &&
- $self->pool->connected_replicants;
+ my $self = shift @_;
+
+ return
+ $self->master->connected &&
+ $self->pool->connected_replicants;
}
=head2 ensure_connected
=cut
sub set_schema {
- my $self = shift @_;
- foreach my $source ($self->all_storages) {
- $source->set_schema(@_);
- }
+ my $self = shift @_;
+ foreach my $source ($self->all_storages) {
+ $source->set_schema(@_);
+ }
}
=head2 debug