framework: Integrity constraint violation when using database session driver
Experienced this in 5.0 and 5.1
MySQL version: 5.6 Forge provisioned server.
We use Galera Cluster for MySQL, but each site reads and writes from only one database. I don’t think that would have anything to do with this error. I used artisan session:table
to create the table so everything is standard schema-wise. Happens quite intermittently, about once per day.
Illuminate\Database\QueryExceptionGET /diaz/edit
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '4c1698dcfe5da87c5f77c90592855490817e575d' for key 'sessions_id_unique' (SQL: insert into `sessions` (`id`, `payload`, `last_activity`) values (4c1698dcfe5da87c5f77c90592855490817e575d, YTo1OntzOjY6Il90b2tlbiI7czo0MDoid1NLSjlmNGFxUml4RkowRVVkU0M3UjhNdldhWlZEY2hwTDhrbEU3YSI7czo3OiJtZXNzYWdlIjtzOjI2OiJMb2dpbiB0byBlZGl0IHlvdXIgcHJvZmlsZSI7czo1OiJmbGFzaCI7YToyOntzOjM6Im5ldyI7YTowOnt9czozOiJvbGQiO2E6MTp7aTowO3M6NzoibWVzc2FnZSI7fX1zOjk6Il9wcmV2aW91cyI7YToxOntzOjM6InVybCI7czozNzoiaHR0cHM6Ly9tYW5pbGEuZXNjb3J0cy5saWZlL2RpYXovZWRpdCI7fXM6OToiX3NmMl9tZXRhIjthOjM6e3M6MToidSI7aToxNDM0MTY4Njc0O3M6MToiYyI7aToxNDM0MTY4Njc0O3M6MToibCI7czoxOiIwIjt9fQ==, 1434168674))
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 1
- Comments: 64 (23 by maintainers)
Commits related to this issue
- Fix integrity constraints for database session driver. (#17301) Backporting fix of issue #9251 to 5.3 branch. Originally by Taylor Otwell <taylor@laravel.com>. Signed-off-by: Gleb Golubitsky <sec... — committed to laravel/framework by Sectoid 7 years ago
- Backporting fix of issue #9251 to 5.1 branch. Original commit by Taylor Otwell: d9e0a6a — committed to rspraymond/framework by deleted user 7 years ago
- Backporting fix of issue #9251 to 5.1 branch. Original commit by Taylor Otwell: d9e0a6a — committed to rspraymond/framework by deleted user 7 years ago
- Backporting fix of issue #9251 to 5.1 branch. Original commit by Taylor Otwell: d9e0a6a (#17686) — committed to laravel/framework by rspraymond 7 years ago
- This should solve - local.ERROR: 500 exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'XXXXXXX' for key 'sessions_id_unique'' in vendor/lar... — committed to web-engineer/framework by web-engineer 6 years ago
This fix isn’t in release v5.1.45 but is in 5.1 branch.
How often are new tags/releases created for the LTS releases? Would second a 5.1 release/tag assuming it’s good to go.
Issue is still present in Laravel v5.3.26.
Currently, we have anywhere between 3 and 40 instances serving our app, with a single read/write MariaDB database. Our sessions table typically contains 15k+ sessions at a time, expiring in 2 hour intervals.
Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'fLOk3VaO0TxW1vFxrXX6q3hcGg8DmGKGpb7QsIUZ' for key 'sessions_id_unique'
Just received this error in slack from one of my apps. If there is no PR in the pipeline, might have a look at this myself.
Can someone please tag a release in 5.1 for this fix ?
@taylorotwell - Any chance you could point me to where this was patched? I am hoping to put together a fix for 4.2 as well.
**Edit: Never mind, I found it.
For those interested: https://github.com/laravel/framework/commit/d9e0a6a03891d16ed6a71151354445fbdc9e6f50
The only other explanation I can fathom is that sometimes the loop back is too fast (which seems highly, highly unlikely) and starts a second request before the first one inserts, resulting in a duplicate session that has a false value for
$this->exists
.https://github.com/laravel/framework/blob/5.1/src/Illuminate/Session/DatabaseSessionHandler.php#L95
If
$this->exists
is not going to be reliable (for whatever reason), then the obvious fix is to query the database ahead of insert/update. I would be happy to fork and create a pull request for this, but maybe there’s a better option… ?Not in 3 hours.
Happened to me now after switched to database driver for session … any idea?
Thanks
ping @taylorotwell