Error
Error Code:
2058
MySQL Error 2058: Handle Already Connected
Description
This error occurs when a client program attempts to establish a new MySQL connection using a connection handle or object that is already actively connected to a database server. It signifies an improper reuse of connection resources, as each distinct database connection requires its own unique handle.
Error Message
This handle is already connected. Use a separate handle for each connection.
Known Causes
3 known causesAttempting to Reconnect Active Handle
The client application attempts to call a connection function (e.g., `mysql_real_connect`) on a handle that is already successfully connected to a MySQL server.
Flawed Connection Pooling
A custom connection pooling mechanism might be returning an active, in-use connection handle for a new request instead of an available, idle one.
Unclosed or Unreset Connections
The application logic fails to properly close or reset a connection handle before attempting to reuse it for a subsequent connection attempt.
Solutions
3 solutions available1. Ensure Connection Handles are Disposed easy
Close existing connections before attempting to reuse a handle.
1
In your application code, locate where database connections are established and managed. Ensure that you are explicitly closing or disposing of connection objects when they are no longer needed.
Example in Python (using `mysql.connector`):
python
import mysql.connector
try:
cnx = mysql.connector.connect(user='user', password='password', host='host', database='db')
cursor = cnx.cursor()
# ... perform database operations ...
except mysql.connector.Error as err:
print(f"Error: {err}")
finally:
if 'cursor' in locals() and cursor:
cursor.close()
if 'cnx' in locals() and cnx.is_connected():
cnx.close()
2
If you are using a connection pool, ensure that connections are returned to the pool after use. Most connection pooling libraries handle this automatically if you use them correctly, but verify your implementation.
Example in Node.js (using `mysql` package):
javascript
const mysql = require('mysql');
const pool = mysql.createPool({
host: 'localhost',
user: 'user',
password: 'password',
database: 'db'
});
pool.getConnection((err, connection) => {
if (err) throw err;
// ... perform database operations ...
connection.release(); // Release the connection back to the pool
});
2. Implement Proper Connection Management in Application Logic medium
Structure your application to use distinct connection handles for concurrent operations.
1
Review your application's architecture. If you are performing multiple operations concurrently (e.g., in different threads or asynchronous tasks) and they all attempt to use the same connection object, you will encounter this error. Design your application to create a new connection object (or acquire a new one from a pool) for each independent operation.
Consider a scenario where a web server handles multiple requests. Each request should ideally get its own database connection from a pool.
2
If you are reusing a connection object across multiple, unrelated operations, ensure that all previous operations on that connection are fully completed and the connection is released or closed before attempting to use it for a new, independent operation.
Avoid patterns like:
java
Connection conn = getConnection();
// Operation 1
conn.prepareStatement("...").execute();
// Operation 2 (without releasing/reconnecting)
conn.prepareStatement("...").execute(); // This might cause 2058 if Operation 1 didn't fully release its hold.
Instead, prefer:
java
Connection conn1 = getConnection();
// Operation 1
conn1.prepareStatement("...").execute();
conn1.close(); // Or return to pool
Connection conn2 = getConnection();
// Operation 2
conn2.prepareStatement("...").execute();
conn2.close(); // Or return to pool
3. Restart Application or Service easy
A quick restart can resolve transient state issues.
1
If this error is occurring intermittently and you suspect a transient issue within your application's connection management, a simple restart of the application or the service that manages the database connections can often clear the problematic state.
Example for a systemd service:
bash
sudo systemctl restart your_application_service_name
2
For desktop applications or scripts, simply stopping and re-running them can resolve the issue.
N/A