Raised when a MySQL client tries to open a new session with a connection handle that is already connected.
MySQL Error 2058: CR_ALREADY_CONNECTED occurs when your code reuses a connection handle that still has an open session. Create a fresh MYSQL object or close the existing connection before calling mysql_real_connect to resolve the issue.
This handle is already connected. Use a separate handle for each connection.
MySQL throws error 2058, condition CR_ALREADY_CONNECTED, when the client API detects that mysql_real_connect is being called on a handle that is already associated with an active session.
The check prevents memory corruption and packet-mix-ups that arise from using the same MYSQL structure for multiple concurrent connections. Fixing the error keeps pools healthy and avoids unexpected disconnects.
Handle reuse is the primary trigger.
A program allocates one MYSQL pointer, connects, and then attempts to connect again without first calling mysql_close or allocating a new handle.
Race conditions in connection pools can return a live handle to another thread, causing the second thread to hit CR_ALREADY_CONNECTED instantly.
Allocate a new MYSQL handle with mysql_init for every concurrent connection.
Never share one handle across threads or async tasks.
If you truly need to reconnect on the same variable, always call mysql_close to release resources and reset internal state before attempting mysql_real_connect again.
In multi-threaded C++ code, protect connection pools with mutexes so each thread receives a unique, idle handle.
In PHP or Python, disable persistent connections if your framework unexpectedly reuses handles between requests that call connect again.
Adopt a one-handle-per-connection rule.
Treat MYSQL* as non-shareable and non-reentrant.
Implement connection-pool libraries that track handle state and never return a busy handle.
CR_COMMANDS_OUT_OF_SYNC (2014) appears when results are not fully read before issuing the next command. Always fetch or discard remaining rows.
CR_SERVER_GONE_ERROR (2006) signals that the server closed the socket. Reconnect with a new handle and enable reconnect flags.
.
You are likely calling mysql_real_connect twice inside the same function or loop. The first call succeeds; the second triggers the error.
No. MYSQL pointers are not thread-safe. Give each thread its own handle or use a properly synchronized pool.
No. Automatic reconnect only helps after disconnections. It does not allow multiple active connections on one handle.
Galaxy allocates a fresh connection per editor tab and closes it cleanly after execution, so handle reuse cannot occur.