1 package DBIx::Class::Storage::DBI::Replication;
6 use DBIx::Class::Storage::DBI;
8 use base qw/Class::Accessor::Fast/;
10 __PACKAGE__->mk_accessors( qw/read_source write_source/ );
14 DBIx::Class::Storage::DBI::Replication - Replicated database support
18 # change storage_type in your schema class
19 $schema->storage_type( '::DBI::Replication' );
20 $schema->connect_info( [
21 [ "dbi:mysql:database=test;hostname=master", "username", "password", { AutoCommit => 1 } ], # master
22 [ "dbi:mysql:database=test;hostname=slave1", "username", "password", { priority => 10 } ], # slave1
23 [ "dbi:mysql:database=test;hostname=slave2", "username", "password", { priority => 10 } ], # slave2
29 This class implements replicated data store for DBI. Currently you can define one master and numerous slave database
30 connections. All write-type queries (INSERT, UPDATE, DELETE and even LAST_INSERT_ID) are routed to master database,
31 all read-type queries (SELECTs) go to the slave database.
33 For every slave database you can define a priority value, which controls data source usage pattern. It uses
34 L<DBD::Multi>, so first the lower priority data sources used (if they have the same priority, the are used
35 randomized), than if all low priority data sources fail, higher ones tried in order.
41 my $class = ref( $proto ) || $proto;
47 bless( $self, $class );
49 $self->write_source( DBIx::Class::Storage::DBI->new );
50 $self->read_source( DBIx::Class::Storage::DBI->new );
58 my @sources = ($self->{read_source}, $self->write_source);
60 return wantarray ? @sources : \@sources;
64 my( $self, $source_info ) = @_;
66 $self->write_source->connect_info( $source_info->[0] );
68 my @dsns = map { ($_->[3]->{priority} || 10) => $_ } @{$source_info}[1..@$source_info-1];
69 $self->read_source->connect_info( [ 'dbi:Multi:', undef, undef, { dsns => \@dsns } ] );
73 return shift->read_source()->select( @_ );
76 return shift->read_source()->select_single( @_ );
79 return shift->read_source()->throw_exception( @_ );
82 return shift->read_source()->sql_maker( @_ );
84 sub columns_info_for {
85 return shift->read_source()->columns_info_for( @_ );
88 return shift->read_source()->sqlt_type( @_ );
91 return shift->read_source()->create_ddl_dir( @_ );
93 sub deployment_statements {
94 return shift->read_source()->deployment_statements( @_ );
97 return shift->read_source()->datetime_parser( @_ );
99 sub datetime_parser_type {
100 return shift->read_source()->datetime_parser_type( @_ );
102 sub build_datetime_parser {
103 return shift->read_source()->build_datetime_parser( @_ );
108 $self->$_->limit_dialect( @_ ) for( $self->all_sources() );
112 $self->$_->quote_char( @_ ) for( $self->all_sources() );
116 $self->$_->quote_char( @_ ) for( $self->all_sources() );
120 $self->$_->disconnect( @_ ) for( $self->all_sources() );
125 $self->{write_source} = undef;
126 $self->{read_sources} = undef;
130 return shift->write_source()->last_insert_id( @_ );
133 return shift->write_source()->insert( @_ );
136 return shift->write_source()->update( @_ );
139 return shift->write_source()->update_all( @_ );
142 return shift->write_source()->delete( @_ );
145 return shift->write_source()->delete_all( @_ );
148 return shift->write_source()->create( @_ );
151 return shift->write_source()->find_or_create( @_ );
153 sub update_or_create {
154 return shift->write_source()->update_or_create( @_ );
157 return shift->write_source()->connected( @_ );
159 sub ensure_connected {
160 return shift->write_source()->ensure_connected( @_ );
163 return shift->write_source()->dbh( @_ );
166 return shift->write_source()->txn_begin( @_ );
169 return shift->write_source()->txn_commit( @_ );
172 return shift->write_source()->txn_rollback( @_ );
175 return shift->write_source()->sth( @_ );
178 return shift->write_source()->deploy( @_ );
182 sub debugfh { shift->_not_supported( 'debugfh' ) };
183 sub debugcb { shift->_not_supported( 'debugcb' ) };
186 my( $self, $method ) = @_;
188 die "This Storage does not support $method method.";
193 L<DBI::Class::Storage::DBI>, L<DBD::Multi>, L<DBI>
197 Norbert Csongrádi <bert@cpan.org>
199 Peter Siklósi <einon@ahq.hu>
203 You may distribute this code under the same terms as Perl itself.