vendredi 22 mai 2020

Laravel/Lumen problems with immediately reading records

I am having this problem reading data that has just been written. I'm using Lumen 5.7 and I have two connections in database config file. Connection 1 and Connection 2. Data in the Connection 2 database is written by an another application.

When the other application writes the data in the database it triggers my Lumen API call where I try to simply read the newly added/updated records. But it does not find the the newly created/updated record. However if the API call is triggered again it can read the data fine the second time.

Although the data is instantly readable if I try to do query in the DB. There are no read/write replicas just 2 DBs.

this is my db config

'connections' => [
        'migrations' => 'migrations',
        'connection_1' => [
            'driver' => env('DB_CONNECTION'),
            'host' => env('DB_HOST'),
            'database' => env('DB_DATABASE'),
            'username' => env('DB_USERNAME'),
            'password' => env('DB_PASSWORD'),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
        ],
        'connection_2' => [
            'sticky' => true,
            'driver' => env('DB2_CONNECTION'),
            'host' => env('DB2_HOST'),
            'database' => env('DB2_DATABASE'),
            'username' => env('DB2_USERNAME'),
            'password' => env('DB2_PASSWORD'),
        ],
    ]

This is how my model looks like -

use Illuminate\Database\Eloquent\Model;

class SomeModel extends Model
{
    protected $connection = 'connection_2';
    protected $table = 'table_name';
    protected $primaryKey = 'primary_key';
}

I'm doing a simple select query on the above connection_2 db model like this -

$this->model->where('user_id', $id)->get();

In the above model that I'm using I have defined the connection to use for the Model as connection_2

Has anyone faced an issue like this before ? I have been trying to figure this out the whole day.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire