title: how to use database transaction for multiple connection in laravel?
body: <p>we are developing a multi tenant application using 2 separate connections for multiple databases. 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. using db transaction on user::create() and $newuser->roles->sync(); throws error. how to implement db transaction in multi-tenant system. Any help is appreciated.</p>
  
  <pre><code>class User extends Authenticatable implements JWTSubject
  {   
      use Notifiable;
  
      //use main connection
      protected $connection = 'main';
  
       public function roles()
      {
          return $this-&gt;belongsToMany('App\Models\Tenant\Role');
      }
  }
  
  
  class Role extends Model
  {
       protected $connection = 'tenant';
  }
  
  //in UserController.php
   DB::beginTransaction();
  
          try {
              $newUser = User::create([
                  'name' =&gt; $request-&gt;name,
                  'email' =&gt;  $request-&gt;email,
                  'password' =&gt; $request-&gt;password
              ]);
  
              $rolesArray =  $request-&gt;roles;
              $newUser-&gt;roles()-&gt;sync($rolesArray);
  
             DB::commit();
  
              return response()-&gt;json(['status' =&gt; true, 'message' =&gt; 'successfull!!']);
          } catch (Exception $e) {
              DB::rollBack();
              return response()-&gt;json(['status' =&gt; false, 'message' =&gt; 'internal Server Error!!']);
          }
  </code></pre>
  
  <p>Error message : "SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction (SQL: insert into <code>role_user</code> (<code>role_id</code>, <code>user_id</code>) values (2, 13))",</p>