Premium

Railway Reservation System

Two threads, one seat: the double-booking race condition proven broken, then proven fixed, with real thread counts both times.

  • Java
  • JDBC
  • MySQL

About this project

List trains, check availability, book a seat, cancel a booking, and a waiting list that promotes automatically when a confirmed seat opens up. Plain Java with JDBC against MySQL — no Spring, no build tool.

The project exists to prove one guarantee: a seat sold to one passenger must never be sold to another, even when two people book in the same instant. Run Reservation --race and two threads race for the last seat on a disposable test train, over and over. The naive version — SELECT, decide in Java, UPDATE, no lock, no transaction — double-books close to every single trial, measured across thousands of real runs at roughly 99 to 100 percent. The fixed version — one atomic UPDATE ... WHERE seats_available > 0 inside a transaction — has never double-booked once across the same scale of testing.

Both numbers are presented as honest, measured ranges, not a single suspiciously round claim: an earlier draft of this README overstated the naive version as failing "every single trial" and the fixed version’s win-split as bounded tighter than measurement actually showed. Both were caught by rerunning the demonstration and rewritten to match what the program actually does — which is itself a small lesson in why you verify a claim about a race condition by running it many times, not once.

What you will learn

  • Reproduce a real double-booking race condition with two threads and one seat, measured, not simulated
  • Fix it with a single atomic UPDATE ... WHERE guarded by an affected-row check, inside a transaction
  • Understand why READ COMMITTED is the isolation level that matters here, and what it guarantees
  • Build a waiting list that promotes automatically and correctly on cancellation
  • Treat a race condition as a statistical claim to measure across many trials, not a one-off demo to stage

Before you start

  • Java basics — classes, exceptions, and a little threading
  • MySQL 8 and the MySQL Connector/J JDBC driver — the README gives the one-file download

New to Java? The free Java course covers everything this project assumes.

Academic integrity notice

These projects are provided for learning, experimentation and reference. Understand the code, modify it for your own requirements, and acknowledge external work according to your institution’s academic policies. Priodemy does not supply work to be submitted as your own.