示例#1
0
  public InventoryRecord readItem() throws Exception {

    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet rs = null;

    try {
      connection = ConnectionHelper.getConnection(dataSource);

      statement = connection.prepareStatement(ConnectionHelper.SELECT_INVENTORY);
      statement.setInt(1, 1);
      rs = statement.executeQuery();

      int quantity = -1;
      while (rs.next()) {
        quantity = rs.getInt("quantity");
      }

      // If we run out of items we are done so stop processing orders
      if (quantity < 1) {
        return null;
      }

      // decrement the quantity and update the table

      InventoryRecord ir = new InventoryRecord(1, --quantity);
      decrementInventory(connection, ir);

      readerIndex++;
      this.inventoryCheckpoint.setInventoryCount(readerIndex);

      return new InventoryRecord(1, 1); // Every order only orders 1 item
    } catch (SQLException e) {
      throw e;
    } finally {
      ConnectionHelper.cleanupConnection(connection, rs, statement);
    }
  }