Skip to content
Advertisement

sql prevent double booking with trigger

I am stumbled on constructing a DB table for hotel reservations.

In my reservation db, there is a reservation start_datetime and end_datetime attribute to indicate booked period and I tried to prevent double booking with a trigger statemen, but I am certain that this method would be vulnerable to race condition.

How can I prevent race condition in such case?

Trigger:

Table:

Thanks

ps. The DB at play is MariaDB 10.3.31.

Advertisement

Answer

If the timeslots cannot be adjacent then use strict unequiations instead of soft one.

For to prevent concurrent processes cross-influence lock the table before insertion. Or insert in the transaction of according isolation level.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement