Safeguard
Application Security•

CWE-1072 Database Access Without Connection Pooling

Opening a new database connection per request is slow and easy to exhaust. CWE-1072 explains why pools matter and how to adopt one.

Safeguard Security Team
2 min read

CWE-1072 is a product that accesses a data resource through a database without using a connection pooling capability. It concerns any service that talks to a database and handles more than a trickle of traffic.

How it happens

Each request opens a connection, runs a query and closes it. Opening a connection costs time. MITRE notes that connection pools allow connections to be reused without the overhead and time consumption of opening and closing a new connection.

# Pattern to avoid: a new connection for every call
def get_user(uid):
    conn = psycopg2.connect(DSN)
    cur = conn.cursor()
    cur.execute("SELECT name FROM users WHERE id = %s", (uid,))
    row = cur.fetchone()
    conn.close()
    return row
# Better: borrow from a pool created once at startup
from psycopg2.pool import SimpleConnectionPool
pool = SimpleConnectionPool(1, 10, DSN)

def get_user(uid):
    conn = pool.getconn()
    try:
        cur = conn.cursor()
        cur.execute("SELECT name FROM users WHERE id = %s", (uid,))
        return cur.fetchone()
    finally:
        pool.putconn(conn)

What an attacker gains

MITRE lists reduced performance. If an attacker can reach the code, the slowdown "might introduce a vulnerability." Many cheap requests that each open a connection can slow the service sharply.

How to find it

The record lists no detection methods. Search for direct connect calls in request handling code, and watch the database for a high rate of new connections.

How to fix it

  1. Create a pool once at startup and reuse it.
  2. Always return connections in a finally block.
  3. Set a maximum pool size below the database limit.

Quick check

What does a pool save?

The cost of opening and closing a connection for every operation.

Sources

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.