0 Replies Latest reply on Jan 8, 2008 3:30 AM by keithbannister

    Can I lock a table in a session bean?

    keithbannister

      Hi,

      I'm trying to create a session bean that checks the contents of a potential new record against a table before adding that record to the table. The table is a list of (funnily enough) hotel bookings. I want to make sure that the booking dates don't clash for a particular room. Because a booking has a start date and end date, it's not sufficient to have a compound primary key based on dates.

      I'm using EJB3 on JBOSS 4

      If I were doing it in normal java it would look something like:

      List<Booking> bookings = ....
      Object bookingLock = new Object();
      
      
      public void create(Boooking booking) {
       synchronized(bookingLock) {
       if (!roomBookedForInterval(booking)) {
       saveBooking(booking)
       }
       }
      }
      
      private boolean roomBookedForInterval(Booking booking) {
       for(Booking b : bookings) {
       if (b.endDate() > booking.startDate() ||
       b.startDate() < booking.endDate() ) {
      
       return true;
       }
       }
      
       return false;
      }
      
      private void saveBooking(Booking booking) {
       bookings.add(booking);
      }
      
      
      


      My question is, what is the EJB3 equivalent of the
      synchronized{}
      block? IS it a transaction? If so, how does it work?