简体   繁体   English

如何在laravel中使用数据库事务进行多个连接?

[英]how to use database transaction for multiple connection in laravel?

we are developing a multi tenant application using 2 separate connections for multiple databases.我们正在为多个数据库使用 2 个单独的连接开发多租户应用程序。 User model uses main connection and Role uses tenant connection .users table has many to many relationship to roles table with pivot table role_user having columns user_id and role_id .FK user_id references users table from main database.用户模型使用主连接,角色使用租户连接 .users 表与角色表具有多对多关系,透视表 role_user 具有列 user_id 和 role_id .FK user_id 引用来自主数据库的用户表。 using db transaction on user::create() and $newuser->roles->sync();在 user::create() 和 $newuser->roles->sync() 上使用数据库事务; throws error.抛出错误。 how to implement db transaction in multi-tenant system.如何在多租户系统中实现数据库事务。 Any help is appreciated.任何帮助表示赞赏。

class User extends Authenticatable implements JWTSubject
{   
    use Notifiable;

    //use main connection
    protected $connection = 'main';

     public function roles()
    {
        return $this->belongsToMany('App\Models\Tenant\Role');
    }
}


class Role extends Model
{
     protected $connection = 'tenant';
}

//in UserController.php
 DB::beginTransaction();

        try {
            $newUser = User::create([
                'name' => $request->name,
                'email' =>  $request->email,
                'password' => $request->password
            ]);

            $rolesArray =  $request->roles;
            $newUser->roles()->sync($rolesArray);

           DB::commit();

            return response()->json(['status' => true, 'message' => 'successfull!!']);
        } catch (Exception $e) {
            DB::rollBack();
            return response()->json(['status' => false, 'message' => 'internal Server Error!!']);
        }

Error message : "SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction (SQL: insert into role_user ( role_id , user_id ) values (2, 13))",错误消息:“SQLSTATE[HY000]:一般错误:1205 超出锁定等待超时;尝试重新启动事务(SQL:插入到role_user ( role_id , user_id ) 值 (2, 13))”,

You seem to be experiencing the issue described in https://github.com/laravel/framework/issues/23413 .您似乎遇到了https://github.com/laravel/framework/issues/23413 中描述的问题。

Unfortunately, there is no trivial "solution" beyond removing the foreign keys, although, I'm exploring workarounds when using PostgreSQL (instead of MySQL/MariaDB) and will update this post if I discover a better approach.不幸的是,除了删除外键之外,没有其他简单的“解决方案”,不过,我正在探索使用 PostgreSQL(而不是 MySQL/MariaDB)时的解决方法,如果我发现更好的方法,我会更新这篇文章。

This is something which is going beyond the normal Eloquent use-case, and Eloquent can become really nasty when you are going beyond normal usecases.这超出了正常的 Eloquent 用例,当你超出正常的用例时,Eloquent 会变得非常讨厌。

To solve this, first you want to create an user on the main database and create a service or repository to create and retrieve the roles on the tennant.为了解决这个问题,首先你想在主数据库上创建一个用户并创建一个服务或存储库来创建和检索租户上的角色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM