Quiz

  1. The routing still seem to be very confusing for example could you [explain] problem 2 from this quiz more.

    Here's the quiz question:

  2. Can you use dbi.connect() once at the beginning of your website, or do you need to call it in each function which might access the database?

    Great question! It seems like you could just connect once and use that same connection for each request.

    But don't. The reason has to do with concurrency: multiple requests overlapping in time. We'll discuss this later in the course, but here's a preview:

    Remember the last_insert_id() function that you used in getting your staff id? That returns the most recent ID for this connection. And so we want a new connection for each request.

    A more sophisticated approach would be to keep a pool of active connections and use one from the pool for each request and return it to the pool when done. I'll leave that as an exercise for the reader.

    Another issue is that connections can time out if they are unused for a while, so it can be easier to get a fresh one.

  3. Why is (1) not a tuple but (1,) is?

    Consider an expression like:

    
    val = (3-2)*4
    
    

    That's the same as:

    
    val = (1)*4
    
    

    and so we need parenthesized expressions to be just values, not tuples.

    But

    
    val = (1,2)
    
    

    is a two-element tuple.

    Therefore:

    
    val = (1,)
    
    

    is a one-element tuple

    Yes, this is a little weird-looking. But it's necessary to avoid ambiguity.

  4. Maybe no questions, but I think I need to practice more!

    Let's do it!