大者在途

  • 我的gitee仓库
大者在途
高山仰止 景行行止
  1. 首页
  2. 后端开发
  3. 正文

laravel 设置数据库隔离级别

2022年5月12日 255点热度 0人点赞 0条评论
连接器
Illuminate\Database\Connectors\MySqlConnector

class MySqlConnector extends Connector implements ConnectorInterface
{
    /**
     * Establish a database connection.
     *
     * @param  array  $config
     * @return \PDO
     */
    public function connect(array $config)
    {
        $dsn = $this->getDsn($config);

        $options = $this->getOptions($config);

        // We need to grab the PDO options that should be used while making the brand
        // new connection instance. The PDO options control various aspects of the
        // connection's behavior, and some might be specified by the developers.
        $connection = $this->createConnection($dsn, $config, $options);

        if (! empty($config['database'])) {
            $connection->exec("use `{$config['database']}`;");
        }

        $this->configureIsolationLevel($connection, $config);

        $this->configureEncoding($connection, $config);

        // Next, we will check to see if a timezone has been specified in this config
        // and if it has we will issue a statement to modify the timezone with the
        // database. Setting this DB timezone is an optional configuration item.
        $this->configureTimezone($connection, $config);

        $this->setModes($connection, $config);

        return $connection;
    }
}

protected function configureIsolationLevel($connection, array $config)
    {
        if (! isset($config['isolation_level'])) {
            return;
        }

        $connection->prepare(
            "SET SESSION TRANSACTION ISOLATION LEVEL {$config['isolation_level']}"
        )->execute();
    }

显示只要在数据库配置中配置这个字段isolation_level就行,具体值如下

隔离级别 脏读 不可重复读 幻读
READ UNCOMMITTED 读未提交 √ √ √
READ COMMITTED 不可重复读 × √ √
REPEATABLE READ 可重复读 × × √
SERIALIZABLE 串行化 × × ×
标签: 暂无
最后更新:2022年5月12日

猫铃儿

一个成长中的程序员

打赏 点赞
< 上一篇
下一篇 >

文章评论

取消回复

COPYRIGHT © 2021 大者在途. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS

冀ICP备18021118号-1