Skip to content

Multiple Connections

Xyranaut edited this page Jun 1, 2026 · 1 revision

Multiple connections & databases

Most servers need exactly one database connection. This page is for the cases where you want more than one — and explains why you usually don't.

You probably only need one connection

mysql_connect gives you a handle that runs queries asynchronously on its own worker thread. One connection can handle a busy server's queries because they're queued and run in order without blocking your game. Start with one.

new MySQL:g_DB;
// ... connect once in OnGameModeInit, use g_DB everywhere ...

When more than one makes sense

  • Two different databases/servers — e.g. your gamemode DB and a separate website/forum DB. Open one handle per server:

    new MySQL:g_GameDB;
    new MySQL:g_WebDB;
    
    g_GameDB = mysql_connect("127.0.0.1", "game", "${GAME_PW}", "gamedb", cfg);
    g_WebDB  = mysql_connect("10.0.0.9",  "web",  "${WEB_PW}",  "forum",  cfg2);
    
    mysql_execute(g_GameDB, "SELECT ...", "OnGame");
    mysql_execute(g_WebDB,  "SELECT ...", "OnWeb");

    Each handle is independent — its own worker, its own queue, its own TLS session.

  • Isolating a heavy background job — if you run occasional slow analytics that would clog the main queue, a second handle to the same database keeps everyday queries snappy. (Each handle is a separate connection/worker.)

"Connection pool" — what omp-MySQL does and doesn't do

In web frameworks a "pool" means many parallel connections sharing load. omp-MySQL's model is one worker thread per handle, queries serialized per handle. That's the right fit for a game server (ordered, predictable, no locking surprises). If you genuinely need parallelism, open multiple handles — each is effectively one more "lane."

There's a POOL_SIZE option in the config enum for future tuning, but the practical way to get more parallelism today is multiple mysql_connect handles.

Caveats with multiple handles

  • Each handle is a real DB connection — don't open dozens. A handful is plenty.
  • Results don't cross handles — a query on g_WebDB is read in its callback as usual; handles don't share an active result set.
  • Close what you openmysql_close(handle) when done (or at shutdown; they're cleaned up on gamemode restart anyway).
  • Order is per-handle — two queries on the same handle run in order; queries on different handles may finish in any order.

Picking the right pattern

Situation Do this
One database, normal server One handle.
Game DB + website/forum DB One handle per database/server.
Rare heavy analytics on the game DB A second handle to the same DB for the heavy job.
"I need 50 parallel queries" You almost certainly don't — queue them on one handle.

Next: Performance · Configuration

Clone this wiki locally