SilverStripe Split Database module
This module contains an implementation of Read/Write splitting for database
queries to allow for master/slave database implementations, (*1)
Basic Usage
For using aspects, which is effectively database agnostic, read the
'ReadWriteSplitterAspect' section. For using a custom database class against
MySQL, read the 'ReadWriteMySQLDatabase' section., (*2)
ReadWriteSplitterAspect
Use this aspect to direct READ queries to a particular database, and WRITE
queries (ie queries that modify) to a specific master server (that is
replicating to those slaves). It is assumed replication is managed externally
to SilverStripe., (*3)
Add configuration in your project along the lines of, (*4)
Injector:
WriteMySQLDatabase:
class: MySQLDatabase
constructor:
- type: MySQLDatabase
server: write.master.database.hostname
username: user
password: pass
database: project_database
ProxiedMySQLDatabase:
class: MySQLDatabase
constructor:
- type: MySQLDatabase
server: readonly.slavecluster.hostname
username: user
password: pass
database: project_database
MySQLWriteDbQueryAspect:
class: \SilverStripe\Aspects\Database\ReadWriteSplitterAspect
properties:
writeDb: %$WriteMySQLDatabase
MySQLDatabase:
class: AopProxyService
properties:
proxied: %$ProxiedMySQLDatabase
beforeCall:
query:
- %$MySQLWriteDbQueryAspect
manipulate:
- %$MySQLWriteDbQueryAspect
getGeneratedID:
- %$MySQLWriteDbQueryAspect
affectedRows:
- %$MySQLWriteDbQueryAspect
ReadWriteMySQLDatabase
Similar to the previous ReadWrite aspect, the ReadWriteMySQLDatabase relies on
setting up a separate write database connection for directing queries to. So
the initial database configuration is the same as usual, however instead of
using MySQLDatabase, use ReadWriteMySQLDatabase
(or ReadWriteSQLiteDatabase). This configuration must point to the readonly
database., (*5)
Then, via YAML config, create configuration for the write specific database
(note that the configured Injector object MUST be called
SplitterWriteDatabase), (*6)
Injector:
SplitterWriteDatabase:
class: MySQLDatabase
constructor:
- type: MySQLDatabase
server: write.master.database.hostname
username: user
password: pass
database: project_database
Maintainer Contacts
Requirements
License
This module is licensed under the BSD license at http://silverstripe.org/BSD-license, (*7)