Skip to main content
Version: 6.4

读取副本连接

用户可以通过 replicas 选项指定多个读取连接。你只能提供与主连接不同的字段,其余字段将从中获取。

¥Users can specify multiple read connections via replicas option. You can provide only fields that differ from master connection, rest will be taken from it.

解析读取连接时,默认策略是为不在事务内运行的所有读取操作(SELECT、COUNT)分配随机读取副本。

¥When resolving read connections, the default strategy is to assign random read replicas for all read operations (SELECT, COUNT) that are not running inside a transaction.

你可以通过在相应的选项参数(即 FindOptionsCountOptions)上使用 connectionType 属性来为查找和计数操作指定显式连接类型。

¥You can specify an explicit connection type for find and count operations by using the connectionType property on the corresponding Options argument (i.e. FindOptions, CountOptions).

连接解析策略也可以通过将 preferReadReplicas 配置属性设置为 false 来反转,这样默认连接将始终是写入连接,除非明确请求读取(在读取副本可用的应用中很有用,但只能用于特定用例)。

¥The connection resolution strategy can be also inverted by setting the preferReadReplicas configuration property to false so that the default connection will always be a write connection, unless explicitly requested to be read (can be useful in applications where read-replicas are available but should only be used for specific use-cases).

const orm = await MikroORM.init({
entities: [Author, ...],
dbName: `my_database`,
user: 'master_user',
host: 'master_host',
preferReadReplicas: true, // optional property, defaults to true
replicas: [
{ name: 'read-1', host: 'read_host_1', user: 'read_user' },
{ name: 'read-2', host: 'read_host_2' }, // user omitted, will be taken from master connection
],
});

默认情况下,如果不在事务内,选择查询将使用随机读取连接。你可以在 em.getConnection(type: 'read' | 'write') 中明确指定连接类型。

¥By default, select queries will use random read connection if not inside transaction. You can specify the connection type explicitly in em.getConnection(type: 'read' | 'write').

const connection = em.getConnection(); // write connection
const readConnection = em.getConnection('read'); // random read connection

const qb1 = em.createQueryBuilder(Author);
const res1 = await qb1.select('*').execute(); // random read connection

const qb2 = em.createQueryBuilder(Author, 'a', 'write');
const res2 = await qb2.select('*').execute(); // write connection

const qb3 = em.createQueryBuilder(Author);
const res3 = await qb3.update(...).where(...).execute(); // write connection

const res4 = await em.findOne(Author, 1, { connectionType: 'write' }); // explicit write connection

// all queries inside a transaction will use write connection
await em.transactional(async em => {
const a = await em.findOne(Author, 1); // write connection
const b = await em.findOne(Author, 1, { connectionType: 'read' }); // still a write connection - we are in a transaction
a.name = 'test'; // will trigger update on write connection once flushed
});

// given a configuration where preferReadReplicas: false
const res5 = await em.findOne(Author, 1); // write connection - even for a read operation
const res6 = await em.findOne(Author, 1, { connectionType: 'read' }); // unless explicitly asking for a read replica