-
Notifications
You must be signed in to change notification settings - Fork 0
Multiple Connections
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.
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 ...-
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.)
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_SIZEoption in the config enum for future tuning, but the practical way to get more parallelism today is multiplemysql_connecthandles.
- Each handle is a real DB connection — don't open dozens. A handful is plenty.
-
Results don't cross handles — a query on
g_WebDBis read in its callback as usual; handles don't share an active result set. -
Close what you open —
mysql_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.
| 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
Understand
Use
- Installing MySQL
- Docker Compose
- Getting started
- Configuration
- SQL crash course
- Designing your tables
- Storing game data
- Dates & times
- First queries
- Async patterns
- Reading results
- Prepared statements
- Passwords & hashing
- Transactions
- Models (active-record)
- Tutorial: login system
- mysql-admin demo
Deeper
Reference