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 - EXPERIMENTAL 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
25 { limit_dialect => 'LimitXY' } # If needed, see below
30 Warning: This class is marked EXPERIMENTAL. It works for the authors but does
31 not currently have automated tests so your mileage may vary.
33 This class implements replicated data store for DBI. Currently you can define
34 one master and numerous slave database connections. All write-type queries
35 (INSERT, UPDATE, DELETE and even LAST_INSERT_ID) are routed to master
36 database, all read-type queries (SELECTs) go to the slave database.
38 For every slave database you can define a priority value, which controls data
39 source usage pattern. It uses L<DBD::Multi>, so first the lower priority data
40 sources used (if they have the same priority, the are used randomized), than
41 if all low priority data sources fail, higher ones tried in order.
47 If you use LIMIT in your queries (effectively, if you use
48 SQL::Abstract::Limit), do not forget to set up limit_dialect (perldoc
49 SQL::Abstract::Limit) by passing it as an option in the (optional) hash
50 reference to connect_info. DBIC can not set it up automatically, since it can
51 not guess DBD::Multi connection types.
57 my $class = ref( $proto ) || $proto;
60 bless( $self, $class );
62 $self->write_source( DBIx::Class::Storage::DBI->new );
63 $self->read_source( DBIx::Class::Storage::DBI->new );
71 my @sources = ($self->read_source, $self->write_source);
73 return wantarray ? @sources : \@sources;
77 my( $self, $source_info ) = @_;
79 my( $info, $global_options, $options, @dsns );
81 $info = [ @$source_info ];
83 $global_options = ref $info->[-1] eq 'HASH' ? pop( @$info ) : {};
84 if( ref( $options = $info->[0]->[-1] ) eq 'HASH' ) {
85 # Local options present in dsn, merge them with global options
86 map { $global_options->{$_} = $options->{$_} } keys %$options;
90 # We need to copy-pass $global_options, since connect_info clears it while
92 $self->write_source->connect_info( @{$info->[0]}, { %$global_options } );
94 @dsns = map { ($_->[3]->{priority} || 10) => $_ } @{$info->[0]}[1..@{$info->[0]}-1];
95 $global_options->{dsns} = \@dsns;
97 $self->read_source->connect_info( [ 'dbi:Multi:', undef, undef, { %$global_options } ] );
101 shift->read_source->select( @_ );
104 shift->read_source->select_single( @_ );
106 sub throw_exception {
107 shift->read_source->throw_exception( @_ );
110 shift->read_source->sql_maker( @_ );
112 sub columns_info_for {
113 shift->read_source->columns_info_for( @_ );
116 shift->read_source->sqlt_type( @_ );
119 shift->read_source->create_ddl_dir( @_ );
121 sub deployment_statements {
122 shift->read_source->deployment_statements( @_ );
124 sub datetime_parser {
125 shift->read_source->datetime_parser( @_ );
127 sub datetime_parser_type {
128 shift->read_source->datetime_parser_type( @_ );
130 sub build_datetime_parser {
131 shift->read_source->build_datetime_parser( @_ );
134 sub limit_dialect { $_->limit_dialect( @_ ) for( shift->all_sources ) }
135 sub quote_char { $_->quote_char( @_ ) for( shift->all_sources ) }
136 sub name_sep { $_->quote_char( @_ ) for( shift->all_sources ) }
137 sub disconnect { $_->disconnect( @_ ) for( shift->all_sources ) }
138 sub set_schema { $_->set_schema( @_ ) for( shift->all_sources ) }
143 undef $self->{write_source};
144 undef $self->{read_sources};
148 shift->write_source->last_insert_id( @_ );
151 shift->write_source->insert( @_ );
154 shift->write_source->update( @_ );
157 shift->write_source->update_all( @_ );
160 shift->write_source->delete( @_ );
163 shift->write_source->delete_all( @_ );
166 shift->write_source->create( @_ );
169 shift->write_source->find_or_create( @_ );
171 sub update_or_create {
172 shift->write_source->update_or_create( @_ );
175 shift->write_source->connected( @_ );
177 sub ensure_connected {
178 shift->write_source->ensure_connected( @_ );
181 shift->write_source->dbh( @_ );
184 shift->write_source->txn_begin( @_ );
187 shift->write_source->txn_commit( @_ );
190 shift->write_source->txn_rollback( @_ );
193 shift->write_source->sth( @_ );
196 shift->write_source->deploy( @_ );
200 sub debugfh { shift->_not_supported( 'debugfh' ) };
201 sub debugcb { shift->_not_supported( 'debugcb' ) };
204 my( $self, $method ) = @_;
206 die "This Storage does not support $method method.";
211 L<DBI::Class::Storage::DBI>, L<DBD::Multi>, L<DBI>
215 Norbert Csongrádi <bert@cpan.org>
217 Peter Siklósi <einon@einon.hu>
221 You may distribute this code under the same terms as Perl itself.