I am using ActiveRecord model to save data.
Unique Validator works very perfect. But when i insert data so fast, it no longer works perfectly. In some request i get error that it can not catch by Yii.
Integrity constraint violation – yii\db\IntegrityException SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '***'...
Does we have any solution to handle this problem without adding another service ?
Thanks !
Summing up in the comments...
Most probably what you need is to lock table manually before validation and release lock after it. Yii2 provides optimistic locks mechanism but it is not suitable for your case. Optimistic locks are supported only inside update and delete methods:
Moreover what does optimistic lock is just raise exception when update fails due conflict (no actual table locking).
The solution will depends on your DB engine. Yii2 provides mutex mechanism for manual locking. Out of the box Yii2 supported Mysql and Postgres. See components description at following pages of Yii2 manual :
So, after you configured your mutex in config (example for pgsql from official guide):
you'll need to do something like that
Or, for sure you can do it manually using SQL syntax of your RDBMS.
MySQL:
Pqsql:
Be aware that Pgsql lock works only inside transaction.