0
0
Flaskframework~20 mins

Connection pooling in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Connection Pooling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
What is the main benefit of using connection pooling in Flask apps?

Connection pooling helps Flask apps by:

AIncreasing the number of simultaneous database connections beyond server limits
BAutomatically encrypting all data sent to the database
CReducing the time to establish database connections by reusing existing ones
DReplacing the need for a database entirely
Attempts:
2 left
💡 Hint

Think about what happens when you open and close database connections repeatedly.

component_behavior
intermediate
1:30remaining
What happens if the connection pool is exhausted in a Flask app?

Consider a Flask app using a connection pool with a max size of 5. What occurs when 6 requests try to get a connection simultaneously?

AThe 6th request waits until a connection is released back to the pool
BThe 6th request creates a new connection ignoring the pool limit
CThe 6th request immediately fails with a connection error
DAll requests share the same single connection
Attempts:
2 left
💡 Hint

Think about how pools manage limited resources.

📝 Syntax
advanced
2:00remaining
Which code snippet correctly sets up a SQLAlchemy connection pool in Flask?

Choose the code that properly configures a connection pool with max 10 connections using SQLAlchemy in Flask.

Flask
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
# Configure pool here

db = SQLAlchemy(app)
Aapp.config['SQLALCHEMY_MAX_POOL'] = 10
Bapp.config['SQLALCHEMY_POOL_SIZE'] = 10
Capp.config['SQLALCHEMY_POOL_MAX'] = 10
Dapp.config['SQLALCHEMY_POOL_LIMIT'] = 10
Attempts:
2 left
💡 Hint

Look for the official SQLAlchemy pool size config key.

🔧 Debug
advanced
2:00remaining
Why does this Flask app raise a 'TimeoutError' when using connection pooling?

Given this Flask app snippet, why might a 'TimeoutError' occur?

Flask
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
app.config['SQLALCHEMY_POOL_SIZE'] = 2
app.config['SQLALCHEMY_POOL_TIMEOUT'] = 1

db = SQLAlchemy(app)

@app.route('/')
def index():
    conn1 = db.engine.connect()
    conn2 = db.engine.connect()
    conn3 = db.engine.connect()  # This line causes TimeoutError
    return 'Hello'
AThe app forgot to close the first two connections before opening the third
BSQLite does not support connection pooling, so all connections fail
CThe pool timeout is too high, causing delays
DThe pool size is 2, so the third connection request times out waiting for a free connection
Attempts:
2 left
💡 Hint

Check the pool size and how many connections are requested.

state_output
expert
2:30remaining
What is the output count of active connections after these Flask requests?

Assuming a Flask app with SQLAlchemy connection pool size 3, what is the number of active connections after these steps?

  1. Request 1 opens a connection and holds it.
  2. Request 2 opens a connection and holds it.
  3. Request 3 opens a connection and holds it.
  4. Request 4 tries to open a connection but waits.
  5. Request 1 releases its connection.
  6. Request 4 acquires the released connection.
A3 active connections
B4 active connections
C2 active connections
D1 active connection
Attempts:
2 left
💡 Hint

Remember the pool size limits simultaneous active connections.