title: Synchronization in booking reservation application
body: <p>I have a very fundamental doubt about synchronization vis-a-vis the classic Servlet -> Service -> DAO pattern. </p>
  
  <p>Let's say I have a ticket booking application that is accessed by several concurrent users. No two users should end up booking the same ticket. When two different users try to access the ticket booking, I think the following happens -</p>
  
  <ul>
  <li>the web container creates request/response objects for the two
  requests. Maps the request url to a servlet (let's say, BookingServlet). </li>
  <li>creates threads for each request in order to run the service method
  of the BookingServlet.</li>
  <li>the service -> doPost method creates a BookingService object and passes the seat information.</li>
  <li>BookingService object does some business logic and instantiates the
  BookingDAO object to connect with DB and do the updates.</li>
  </ul>
  
  <p>Now, in order to avoid two users booking the same ticket where must the synchronization happen? </p>
  
  <p>Is it at the BookingService or at the BookingDAO? </p>
  
  <p>But then, and this is my doubt, for each request we are essentially creating a separate BookingService and BookingDAO objects - wouldn't that defeat the purpose of putting synchronized in there?</p>