---
title: "CWE-245: J2EE Bad Practices: Direct Management of Connections"
url: https://radicalnotion.ai/cwe/cwe-245
type: cwe
description: "The J2EE application directly manages connections, instead of using the container's connection management facilities."
updated: 2026-11-10
source: RadicalNotion.AI
license: CC BY-NC 4.0 (https://creativecommons.org/licenses/by-nc/4.0/)
---

# CWE-245: J2EE Bad Practices: Direct Management of Connections

- **Abstraction:** Variant
- **Structure:** Simple
- **Status:** Draft
- **CVEs with this as primary weakness:** 0
- **Of those, in CISA KEV:** 0
- **New in 2026:** 0
- **CWE catalog version:** 4.21

## Description

The J2EE application directly manages connections, instead of using the container's connection management facilities.

## Background

The J2EE standard forbids the direct management of connections. It requires that applications use the container's resource management facilities to obtain connections to resources. Every major web application container provides pooled database connection management as part of its resource management framework. Duplicating this functionality in an application is difficult and error prone, which is part of the reason it is forbidden under the J2EE standard.

## Modes of introduction

| Phase | Note |
| --- | --- |
| Implementation | - |

## Applicable platforms

**Languages:** Java (Undetermined)

**Technologies:** Web Based (Undetermined), Web Server (Undetermined)

## Common consequences

| Scope | Impact | Note |
| --- | --- | --- |
| Other | Quality Degradation | - |

## Detection methods

### Automated Static Analysis

Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)

Effectiveness: High

## Demonstrative examples

### Example 1

In the following example, the class DatabaseConnection opens and manages a connection to a database for a J2EE application. The method openDatabaseConnection opens a connection to the database using a DriverManager to create the Connection object conn to the database specified in the string constant CONNECT\_STRING.

Bad code:

```java
public class DatabaseConnection {
    private static final String CONNECT_STRING = "jdbc:mysql://localhost:3306/mysqldb";
    private Connection conn = null;

    public DatabaseConnection() {
    }

    public void openDatabaseConnection() {
        try {
            conn = DriverManager.getConnection(CONNECT_STRING);
        } catch (SQLException ex) {...}
    }

    // Member functions for retrieving database connection and accessing database
    ...
}
```

Good code:

```java
public class DatabaseConnection {
    private static final String DB_DATASRC_REF = "jdbc:mysql://localhost:3306/mysqldb";
    private Connection conn = null;

    public DatabaseConnection() {
    }

    public void openDatabaseConnection() {
        try {
            InitialContext ctx = new InitialContext();
            DataSource datasource = (DataSource) ctx.lookup(DB_DATASRC_REF);
            conn = datasource.getConnection();
        } catch (NamingException ex) {...}
        } catch (SQLException ex) {...}
    }

    // Member functions for retrieving database connection and accessing database
    ...
}
```

The use of the DriverManager class to directly manage the connection to the database violates the J2EE restriction against the direct management of connections. The J2EE application should use the web application container's resource management facilities to obtain a connection to the database as shown in the following example.

## Related weaknesses

| Relationship | CWE | Name |
| --- | --- | --- |
| Parent | [CWE-695](https://radicalnotion.ai/cwe/cwe-695) | Use of Low-Level Functionality |

## Taxonomy mappings

| Taxonomy | Entry | Id | Fit |
| --- | --- | --- | --- |
| 7 Pernicious Kingdoms | J2EE Bad Practices: getConnection() | - | - |
| Software Fault Patterns | Use of an improper API | SFP3 | - |

## Frequently asked questions

### What is CWE-245?

The J2EE application directly manages connections, instead of using the container's connection management facilities.

### How is CWE-245 detected?

Automated Static Analysis: Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)

### What are the consequences of CWE-245?

Exploiting CWE-245 can lead to: Quality Degradation.

Primary source: [MITRE CWE](https://cwe.mitre.org/data/definitions/245.html)

---

Source: [RadicalNotion.AI](https://radicalnotion.ai/cwe/cwe-245). Licensed CC BY-NC 4.0; attribute RadicalNotion.AI and link the source page when you reuse this data.
