Laravel读写连接不同步 [英] Laravel read and write connection not in sync

查看:108
本文介绍了Laravel读写连接不同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Laravel 5.2应用中使用读写MySQL连接设置:

I'm using read and write MySQL connection setup in my Laravel 5.2 app:

'mysql' => [
    'write' => ['host' => env('DB_HOST_WRITE', 'localhost'),],
    'read'  => ['host' => env('DB_HOST_READ', 'localhost'),],
    'driver' => 'mysql',
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix' => '',
    'strict' => false,
    'engine' => null
]

该网站使用负载均衡器在多台服务器上运行.

The website runs on multiple servers, using a load balancer.

现在,在应用程序中,有一些读写操作接连发生的情况,例如

Now, in the app, there are cases of read and write operations done one after another, e.g.

  1. 将新记录插入数据库
  2. 选择一些新插入的记录

使用当前的连接设置,即使正确插入了记录,选择也不会返回任何内容.

With the current connection setup, it can happen that the select will return nothing, even if the records were inserted correctly.

这可能是什么原因?

推荐答案

在Laravel 5.5中,我为此做了一个PR,引入了粘性"选项.

In Laravel 5.5 I've done a PR for this that introduces a "sticky" option.

这意味着您的应用程序将尽可能使用读取"连接,但是如果您执行写入"操作,则同一请求周期上的任何后续读取"也将来自写入连接.

It means your application will use the "read" connection whenever possible, but if you do a "write", then any subsequent "reads" on the same request cycle will also come from the write connection.

这可确保数据完整性.

https://github.com/laravel/framework/pull/20445

这篇关于Laravel读写连接不同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆