[CDBI] using different DSNs for reads/writes

Brian Duggan bduggan at matatu.org
Thu Dec 20 01:50:31 GMT 2007


On Wednesday, December 19, Michael G Schwern wrote: 
> I could have sworn there were separate "read" vs "write" database handles
> because I've dealt with this sort of problem before both to handle your issue
> with master/slave databases and also to just have a separate "reader" database
> user with lowered permissions to tighten security.  But I can't seem to find it.

To handle the latter case in a maypole application, this is what I do :

__PACKAGE__->Ima::DBI::set_db(
    Read => # ... read params
);
__PACKAGE__->Ima::DBI::set_db(
    Write => # ... write params
);
__PACKAGE__->mk_classdata(useWriteDBH => 0); # Toggled during authorization phase to determine access.

Then, just

sub db_Main {
    my $class  = shift;
    return $class->useWriteDBH ? $class->db_Write(@_) : $class->db_Read(@_);
}

Brian




More information about the ClassDBI mailing list